From 54fb111040658088c04080c998e2a85a73f137f4 Mon Sep 17 00:00:00 2001 From: Tim Voronov Date: Fri, 5 Oct 2018 20:29:42 -0400 Subject: [PATCH] Added custom type as a context key --- pkg/runtime/core/param.go | 4 +++- pkg/runtime/core/param_test.go | 16 +++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/pkg/runtime/core/param.go b/pkg/runtime/core/param.go index 41bb3642..19729c96 100644 --- a/pkg/runtime/core/param.go +++ b/pkg/runtime/core/param.go @@ -2,7 +2,9 @@ package core import "context" -const paramsKey = "params" +type key int + +const paramsKey key = 0 func ParamsWith(ctx context.Context, params map[string]Value) context.Context { return context.WithValue(ctx, paramsKey, params) diff --git a/pkg/runtime/core/param_test.go b/pkg/runtime/core/param_test.go index 646f1eb0..ce778636 100644 --- a/pkg/runtime/core/param_test.go +++ b/pkg/runtime/core/param_test.go @@ -16,9 +16,12 @@ func TestParamsWith(t *testing.T) { p["val2"] = values.NewString("test") pc := core.ParamsWith(context.Background(), p) - So(pc, ShouldNotBeNil) - So(pc.Value("params"), ShouldEqual, p) + + out, err := core.ParamsFrom(pc) + + So(err, ShouldBeNil) + So(out, ShouldEqual, p) }) } @@ -36,10 +39,17 @@ func TestParamsFrom(t *testing.T) { pf, err := core.ParamsFrom(ctx) So(err, ShouldNotBeNil) + So(pf, ShouldBeNil) ctx = context.WithValue(context.Background(), "params", p) pf, err = core.ParamsFrom(ctx) + So(err, ShouldNotBeNil) + So(pf, ShouldBeNil) + + ctx = core.ParamsWith(context.Background(), p) + pf, err = core.ParamsFrom(ctx) + So(err, ShouldBeNil) So(pf, ShouldEqual, p) }) @@ -60,7 +70,7 @@ func TestParamFrom(t *testing.T) { So(err, ShouldNotBeNil) - ctx = context.WithValue(context.Background(), "params", p) + ctx = core.ParamsWith(context.Background(), p) v, err := core.ParamFrom(ctx, "val1") So(err, ShouldBeNil)