-
-
Notifications
You must be signed in to change notification settings - Fork 481
fix(patcher): ctx into nested custom funcs and Env #883
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,11 +4,16 @@ import ( | |
| "reflect" | ||
|
|
||
| "github.com/expr-lang/expr/ast" | ||
| "github.com/expr-lang/expr/checker/nature" | ||
| "github.com/expr-lang/expr/conf" | ||
| ) | ||
|
|
||
| // WithContext adds WithContext.Name argument to all functions calls with a context.Context argument. | ||
| type WithContext struct { | ||
| Name string | ||
| Name string | ||
| Functions conf.FunctionsTable // Optional: used to look up function types when callee type is unknown. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A new function can be also defined as a method of Env. Should it be checked there too?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, good point! I'll check this out. And lets add a regression test for it too.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Functions table and |
||
| Env *nature.Nature // Optional: used to look up method types when callee type is unknown. | ||
| NtCache *nature.Cache // Optional: cache for nature lookups. | ||
| } | ||
|
|
||
| // Visit adds WithContext.Name argument to all functions calls with a context.Context argument. | ||
|
|
@@ -19,6 +24,24 @@ func (w WithContext) Visit(node *ast.Node) { | |
| if fn == nil { | ||
| return | ||
| } | ||
| // If callee type is interface{} (unknown), look up the function type from | ||
| // the Functions table or Env. This handles cases where the checker returns early | ||
| // without visiting nested call arguments (e.g., Date2() in Now2().After(Date2())) | ||
| // because the outer call's type is unknown due to missing context arguments. | ||
| if fn.Kind() == reflect.Interface { | ||
| if ident, ok := call.Callee.(*ast.IdentifierNode); ok { | ||
| if w.Functions != nil { | ||
| if f, ok := w.Functions[ident.Value]; ok { | ||
| fn = f.Type() | ||
| } | ||
| } | ||
| if fn.Kind() == reflect.Interface && w.Env != nil { | ||
| if m, ok := w.Env.MethodByName(w.NtCache, ident.Value); ok { | ||
| fn = m.Type | ||
| } | ||
| } | ||
| } | ||
| } | ||
| if fn.Kind() != reflect.Func { | ||
| return | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we just pass the whole *Config?