Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

question about overload condition operator == #838

Answered by antonmedv
ForeverCzz asked this question in Q&A
Discussion options

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
 | ^
  1. is it possible to overload operator == with two operands with different types?
  2. what wrong leads to my filter creation failure?
You must be logged in to vote

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

Comment options

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

You must be logged in to vote
0 replies
Comment options

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

You must be logged in to vote
0 replies
Comment options

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()

You must be logged in to vote
0 replies
Answer selected by ForeverCzz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
Converted from issue

This discussion was converted from issue #818 on September 18, 2025 12:16.

AltStyle によって変換されたページ (->オリジナル) /