expr-lang/expr

How to recognize aliases?

Closed this issue · 4 comments

type ServiceType int64

const (
	Service_FLY ServiceType = 1
	Service_BUS ServiceType = 2
)

func TestAAA(t *testing.T) {

	m1 := make(map[string]interface{})
	m2 := make(map[string]interface{})
	m2["BBB"] = []ServiceType{32}
	m1["AAA"] = m2

	//boolExprStr := "len(arr) > 0 && (any(arr,# == 4) || any(arr,# == 3))"
	//getExprStr := "filter(arr,# == 1 or # == 2)"

	exprStr := "let arr = filter(AAA.BBB, int(#) == 32) ; len(arr) > 1"
	program, err := expr.Compile(exprStr, expr.Env(m1))
	if err != nil {
		panic(err)
	}

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

	fmt.Println(output)

}

When I run the code , it crash:
--- FAIL: TestAAA (0.00s)
panic: invalid operation: int(main.ServiceType) (1:27)
| let arr = filter(AAA.BBB, int(#) == 32) ; len(arr) > 1
| ..........................^ [recovered]
panic: invalid operation: int(main.ServiceType) (1:27)
| let arr = filter(AAA.BBB, int(#) == 32) ; len(arr) > 1
| ..........................^

I want to be able to force the type of an array into an int type for me to recognize, how should I write the expression?

I see the problem. In Go, custom int types are separate types. I think we can modify int() builtin to be able to convert to a regular int.

I see the problem. In Go, custom int types are separate types. I think we can modify int() builtin to be able to convert to a regular int.

Sounds like it will take a while to support ?

I don’t know 🤷 you can boost speed with github donations.

We can try this but actually you can add your constants into env like this:

env := map[string]any{
    "Service_FLY": Service_FLY,
    "Service_BUS": Service_BUS,
    // ...
}
// ...
out, err := expr.Compile(`Value == Service_FLY`)
// ...