Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The error comes from WithContext doesn't take into consideration that we can inject function through the Compile options with expr.Function #524

Closed
antonmedv opened this issue Jan 14, 2024 · 0 comments · Fixed by #526

Comments

@antonmedv
Copy link
Member

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant