Skip to content

expr.WithContext requires expr.Env? #753

Open
@swdunlop

Description

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()
	 | ^

Activity

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

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions