Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions patcher/value/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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),
)
17 changes: 17 additions & 0 deletions patcher/value/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Loading