expr icon indicating copy to clipboard operation
expr copied to clipboard

function Dot for . operator does not have a correct signature

Open garrettlove8 opened this issue 3 years ago • 3 comments

Hello,

I am trying to use the operator override feature to add a custom operator (this error is thrown when trying to override an existing operator as well).

I've tried referencing both examples (Go Docs and Readme), however I am getting the following error: function Dot for . operator does not have a correct signature

Here is my code:

func run(code string, variables map[string]interface{}) (interface{}, error) {
	options := []expr.Option{
		expr.Env(variables),
		expr.Env(Env{}),
		expr.Operator(".", "Dot"),
	}

	program, err := expr.Compile(code, options...) // Throws error
	if err != nil {
		return nil, err
	}

	env := Env{}

	output, err := expr.Run(program, env)
	if err != nil {
		return nil, err
	}

	return nil, nil
}

type Env struct{}

func (Env) Dot() { fmt.Println("FOUND A DOT") }

Additionally, I found issue #175 which looked like they had come across the same problem and solved it, unfortunately they didn't post their solution.

Any help would be greatly appreciated. Assuming this is doable, I'm happy to submit a PR with updates to the docs for future use!

garrettlove8 avatar Jun 15 '22 15:06 garrettlove8

Sorry, I’m busy with other projects right now.

antonmedv avatar Jun 15 '22 15:06 antonmedv

According to the docs, expr allows you to define new functions or override (existing) binary operators. Specifically, there is no API to define new operators (yet). Such an API would at least need some precedence specification, which is currently all done internally.

Anyway, . is not an operator but rather a reserved word that you cannot override.

For a list of overridable operators: https://github.com/antonmedv/expr/blob/d71dab79bac52529bce7f38771d69cbeefaf1a32/parser/parser.go#L38

xio812 avatar Jun 17 '22 06:06 xio812

Yes, true. The syntax is bot overwriteable right now.

antonmedv avatar Jun 17 '22 19:06 antonmedv

You can use ast.Patch to override dot behavior.

https://github.com/antonmedv/expr/blob/master/docs/Visitor-and-Patch.md

antonmedv avatar Nov 05 '22 18:11 antonmedv