Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to recognize aliases? #596

Closed
KenLoong opened this issue Mar 10, 2024 · 5 comments
Closed

How to recognize aliases? #596

KenLoong opened this issue Mar 10, 2024 · 5 comments
Labels

Comments

@KenLoong
Copy link

KenLoong commented Mar 10, 2024

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?

@antonmedv
Copy link
Member

antonmedv commented Mar 10, 2024

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.

@KenLoong
Copy link
Author

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 ?

@antonmedv
Copy link
Member

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

@bizywizy
Copy link
Contributor

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`)
// ...

@Cassius0924
Copy link

Cassius0924 commented Nov 20, 2024

What if the alias is a pointer type?

type ModeEnum int

const (
	ModeEnumA ModeEnum = 1
	ModeEnumB ModeEnum = 2
)

type Env struct {
	Mode *ModeEnum
}

If I use int(Mode), I got an error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants