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:
|
Beta Was this translation helpful? Give feedback.
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. 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.
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()