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

Recommendations for traversing a parsed expression #9

Closed
DagW opened this issue Feb 17, 2021 · 2 comments
Closed

Recommendations for traversing a parsed expression #9

DagW opened this issue Feb 17, 2021 · 2 comments

Comments

@DagW
Copy link
Contributor

DagW commented Feb 17, 2021

I'm not sure how to traverse a parsed expression with version 2.1.1. For version 1.0.0 we could recurse through the Expression by

func walkExpression(e fp.Expression) error {
    switch stmt := e.(type) {
	case fp.UnaryExpression: 
		ex, err := walkExpression(stmt.X)
	case fp.BinaryExpression:
		ex, err := walkExpression(stmt.X)
		ey, err := walkExpression(stmt.Y)
	case fp.AttributeExpression:
		// verify/use the attribute
	case nil:
	default:
	}
}

Im not sure how to perform a similar operation with v2.1.1,
A similar technique with 2.1.1 gives me an error

	switch stmt := (e).(type) {
	case fp.LogicalExpression: <-

impossible type switch case: e (type filter.Expression) cannot have dynamic type filter.LogicalExpression (filter.exprNode method has pointer receiver)

Is there a recommended way to traverse the expression?

And thanks for the release earlier!

@q-uint
Copy link
Collaborator

q-uint commented Feb 17, 2021

Hey @DagW!

It works exactly the same way as before:

func Example_walk() {
expression, _ := ParseFilter([]byte("emails[type eq \"work\" and value co \"@example.com\"] or ims[type eq \"xmpp\" and value co \"@foo.com\"]"))
var walk func(e Expression) error
walk = func(e Expression) error {
switch v := e.(type) {
case *LogicalExpression:
_ = walk(v.Left)
_ = walk(v.Right)
case *ValuePath:
_ = walk(v.ValueFilter)
case *AttributeExpression:
fmt.Printf("%s %s %q\n", v.AttributePath, v.Operator, v.CompareValue)
default:
// etc...
}
return nil
}
_ = walk(expression)
// Output:
// type eq "work"
// value co "@example.com"
// type eq "xmpp"
// value co "@foo.com"
}

Let me know if there are more questions/problems.

@DagW
Copy link
Contributor Author

DagW commented Feb 17, 2021

Thanks! The dereference was what was missing :-)

@DagW DagW closed this as completed Feb 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants