From 5ff68c4144adcf46618e46b936a8ceb5969d96e4 Mon Sep 17 00:00:00 2001 From: Denis Arh Date: Fri, 25 Feb 2022 16:13:39 +0100 Subject: [PATCH] Better handling of unresolved (type any) values --- pkg/expr/expr_types.go | 2 +- pkg/expr/vars_test.go | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/pkg/expr/expr_types.go b/pkg/expr/expr_types.go index 592f8baae..e3366de39 100644 --- a/pkg/expr/expr_types.go +++ b/pkg/expr/expr_types.go @@ -189,7 +189,7 @@ func (t *Unresolved) Assign(interface{}) (err error) { } func CastToAny(val interface{}) (interface{}, error) { - return val, nil + return UntypedValue(val), nil } func CastToArray(val interface{}) (out []TypedValue, err error) { diff --git a/pkg/expr/vars_test.go b/pkg/expr/vars_test.go index 93c5ee7be..fd050e4f2 100644 --- a/pkg/expr/vars_test.go +++ b/pkg/expr/vars_test.go @@ -2,8 +2,9 @@ package expr import ( "encoding/json" - "github.com/stretchr/testify/require" "testing" + + "github.com/stretchr/testify/require" ) // extract typed-value @@ -282,6 +283,19 @@ func TestVars_Omit(t *testing.T) { req.Equal(expected, out) } +func TestVarsTypeResolving(t *testing.T) { + var ( + req = require.New(t) + v = &Vars{} + ) + + req.NoError(v.Set("unr", &Unresolved{value: "wrapped any", typ: "Any"})) + req.NoError(v.Set("any", &Any{value: "raw any"})) + req.NoError(v.ResolveTypes(func(typ string) Type { return Any{} })) + req.Equal("wrapped any", v.value["unr"].Get()) + req.Equal("raw any", v.value["any"].Get()) +} + func TestVars_UnmarshalJSON(t *testing.T) { cases := []struct { name string