-
-
Notifications
You must be signed in to change notification settings - Fork 463
question about overload condition operator == #838
-
I was trying to overload operator == to support json.Number. And I wrote this:
type Filter interface { // ... } type exprFilter struct { p *vm.Program } type FilterEnv struct { JsonNumEq func(a json.Number, b any) bool } // NewFilter creates a filter func NewFilter(config string) (Filter, error) { f := exprFilter{} if config == "" { return f, nil } opts := []expr.Option{ expr.Env(FilterEnv{ JsonNumEq: func(a json.Number, b any) bool { return fmt.Sprint(a) == fmt.Sprint(b) }, }), expr.Operator("==", "JsonNumEq"), } p, err := expr.Compile(config, opts...) if err != nil { return f, err } f.p = p return f, nil } // ... filter, err := NewFilter("status==4") if err != nil { log.Fatal(err) } // ...
and I got this:
unknown name status (1:1)
| status==4
| ^
- is it possible to overload operator == with two operands with different types?
- what wrong leads to my filter creation failure?
Beta Was this translation helpful? Give feedback.
All reactions
I think your solution is correct. Only for operator overloading to work, compiler needs to know the types of variables on compile time.
You can do this by collecting all variable names from expressions and setting correct types, and by setting expr.AllowUndefinedVariables()
Replies: 3 comments
-
You need to have status in your environment to reference it in the expression;
type FilterEnv struct {
+ Status json.Number `expr:"status"`
JsonNumEq func(a json.Number, b any) bool
}See; https://go.dev/play/p/gKd6LxdiVLN
Second example here with a struct environment might be useful; https://expr-lang.org/#examples
Beta Was this translation helpful? Give feedback.
All reactions
-
I need to process dynamic fields by using map in project.
And because of that, Vistor-Patch seems more compatible to my code.
type jsonNumEqPatch struct{} func (jsonNumEqPatch) Visit(node *ast.Node) { if n, ok := (*node).(*ast.BinaryNode); ok && n.Operator == "==" { callNode := &ast.CallNode{ Callee: &ast.IdentifierNode{Value: "jsonNumEq"}, Arguments: []ast.Node{n.Left, n.Right}, } ast.Patch(node, callNode) (*node).SetType(reflect.TypeOf(true)) // set return value type } } var customFilterFunc = []expr.Option{ // jsonNumEq overloads operator == expr.Function( "jsonNumEq", func(params ...any) (any, error) { return fmt.Sprint(params[0]) == fmt.Sprint(params[1]), nil }), } // Filter api type Filter interface { Run(map[string]any) (bool, error) } type exprFilter struct { p *vm.Program } // NewFilter builds a expr filter func NewFilter(config string) (Filter, error) { f := exprFilter{} if config == "" { return f, nil } opts := []expr.Option{expr.Patch(jsonNumEqPatch{})} opts = append(opts, customFilterFunc...) p, err := expr.Compile(config, opts...) if err != nil { return f, err } f.p = p return f, nil } // Run runs the expr expression in exprFilter. It returns true if input passes the filter. func (f exprFilter) Run(input map[string]any) (bool, error) { if f.p == nil { return true, nil } v, err := expr.Run(f.p, input) if err != nil { return false, err } res, ok := v.(bool) if !ok { return false, errors.New("filter result is invalid") } return res, nil }
This seems to call the overloaded function for all == operators. Although this does not affect my business, I still wonder if there is a way to overload just for the json.Number type
By the way, I tried to use n.Left.Type().AssignableTo() like https://expr-lang.org/docs/patch#advanced-example mentioned but it seems doesn't work
Beta Was this translation helpful? Give feedback.
All reactions
-
I think your solution is correct. Only for operator overloading to work, compiler needs to know the types of variables on compile time.
You can do this by collecting all variable names from expressions and setting correct types, and by setting expr.AllowUndefinedVariables()
Beta Was this translation helpful? Give feedback.