Repository navigation
question about overload condition operator == #818
Copy link
Copy link
Closed
Description
Activity
You need to have
statusin 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/#examplesI 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 usen.Left.Type().AssignableTo()like https://expr-lang.org/docs/patch#advanced-example mentioned but it seems doesn't work- locked and limited conversation to collaborators
on Sep 18, 2025
Metadata
Metadata
Assignees
Labels
No labels
I was trying to overload operator == to support json.Number. And I wrote this:
and I got this: