Skip to content

question about overload condition operator == #818

Description

@ForeverCzz

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?

Activity

  1. seruman commented on Aug 7, 2025

    @seruman

    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

  2. ForeverCzz commented on Aug 8, 2025

    @ForeverCzz
    Author

    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

  3. locked and limited conversation to collaborators on Sep 18, 2025
  4. converted this issue into a discussion #838 on Sep 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions