question about overload condition operator == #838
Replies: 3 comments
-
You need to have type FilterEnv struct {
+ Status json.Number `expr:"status"`
JsonNumEq func(a json.Number, b any) bool
} See; https://go.dev/play/p/gKd6LxdiVLN |
Beta Was this translation helpful? Give feedback.
-
I need to process dynamic fields by using map in project. 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 |
Beta Was this translation helpful? Give feedback.
-
I think your solution is correct. Only for operator overloading to work, compiler needs to know the types of variables on compile time. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I was trying to overload operator == to support json.Number. And I wrote this:
and I got this:
Beta Was this translation helpful? Give feedback.
All reactions