Closed
Description
The error comes from WithContext doesn't take into consideration that we can inject function through the Compile options with expr.Function
with the change https://github.com/expr-lang/expr/pull/504/files we loose track of what the arguments should be with the function and the patcher can only see the function as having the arguments []any
instead of the arguments we want the function to have.
Here is a test that is in the same style as TestWithContext but using expr.Function instead of applying the function to the env
func TestWithContextWithFunction(t *testing.T) {
env := map[string]any{
"ctx": context.TODO(),
}
withFunction := expr.Function("fn",
func(params ...any) (any, error) {
ctx := params[0].(context.Context)
a := params[1].(int)
return ctx.Value("value").(int) + a, nil
},
new(func(context.Context, int) int),
)
withContext := patcher.WithContext{Name: "ctx"}
program, err := expr.Compile(
`fn(40)`,
expr.Env(env),
expr.Patch(withContext),
withFunction,
)
require.NoError(t, err)
ctx := context.WithValue(context.Background(), "value", 2)
env["ctx"] = ctx
output, err := expr.Run(program, env)
require.NoError(t, err)
require.Equal(t, 42, output)
}
Originally posted by @tw1nk in #514 (reply in thread)