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

expr.WithContext requires expr.Env? #753

Open
swdunlop opened this issue Feb 8, 2025 · 1 comment
Open

expr.WithContext requires expr.Env? #753

swdunlop opened this issue Feb 8, 2025 · 1 comment

Comments

@swdunlop
Copy link

swdunlop commented Feb 8, 2025

When using the expr.WithContext option, the named variable must also be provided at compilation time using expr.Env, otherwise the patch seems to fail to correctly update calls to functions that require a context. (I'm not exactly sure why this happens, but if it's necessary, perhaps we should document it?)

Reproduction:

env := map[string]any{
	`cancelled`: func(ctx context.Context) bool {
		return ctx.Err() != nil
	},
	`ctx`: context.Background(),
}
program, err := expr.Compile(`cancelled()`,
	expr.WithContext(`ctx`),
	// expr.Env(env),
)
if err != nil {
	panic(err)
}
output, err := expr.Run(program, env)
if err != nil {
	panic(err)
}
fmt.Println(output)

With the expr.Env line commented out, this will fail (see playground), and with it uncommented, it succeeds (see playground). The error produced at runtime is:

reflect: Call with too few input arguments (1:1)
	 | cancelled()
	 | ^
@swdunlop swdunlop changed the title expr.WithContext Requires expr.Env expr.WithContext requires expr.Env? Feb 8, 2025
@swdunlop
Copy link
Author

swdunlop commented Feb 8, 2025

Since this error also happens if cancelled is not bound at compilation but is bound at run, playground, I assume the problem is that Patch cannot figure out the argument of the function at compilation time.

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

No branches or pull requests

1 participant