diff --git a/patcher/value/value.go b/patcher/value/value.go index 28f52be27..2ca306d4e 100644 --- a/patcher/value/value.go +++ b/patcher/value/value.go @@ -25,6 +25,7 @@ import ( var ValueGetter = expr.Option(func(c *conf.Config) { c.Visitors = append(c.Visitors, patcher{}) getValueFunc(c) + getAnyValueFunc(c) }) // A AnyValuer provides a generic function for a custom type to return standard go values. @@ -136,6 +137,15 @@ func (patcher) Visit(node *ast.Node) { switch id := (*node).(type) { case *ast.IdentifierNode, *ast.MemberNode: nodeType := id.Type() + if nodeType != nil && nodeType.Kind() == reflect.Interface && nodeType.NumMethod() == 0 { + if _, ok := id.(*ast.MemberNode); ok { + ast.Patch(node, &ast.CallNode{ + Callee: &ast.IdentifierNode{Value: "$patcher_value_getter_any"}, + Arguments: []ast.Node{id}, + }) + return + } + } for _, t := range supportedInterfaces { if nodeType.Implements(t) { ast.Patch(node, &ast.CallNode{ @@ -214,3 +224,7 @@ var getValueFunc = expr.Function("$patcher_value_getter", getValue, new(func(MapValuer) map[string]any), new(func(any) any), ) + +var getAnyValueFunc = expr.Function("$patcher_value_getter_any", getValue, + new(func(any) any), +) diff --git a/patcher/value/value_test.go b/patcher/value/value_test.go index d04a4120a..1736477bf 100644 --- a/patcher/value/value_test.go +++ b/patcher/value/value_test.go @@ -169,3 +169,20 @@ func Test_valueTypedMap(t *testing.T) { require.NoError(t, err) require.Equal(t, 3, out.(int)) } + +func Test_valueNestedMap(t *testing.T) { + env := map[string]any{ + "params": map[string]any{ + "ValueOne": &customInt{1}, + "ValueTwo": &customInt{2}, + }, + } + + program, err := expr.Compile("params.ValueOne + params.ValueTwo", expr.Env(env), ValueGetter) + require.NoError(t, err) + + out, err := vm.Run(program, env) + + require.NoError(t, err) + require.Equal(t, 3, out.(int)) +}