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

Fix ID collision between partial eval and AST pruner #700

Merged
merged 1 commit into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion interpreter/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,19 @@ type astPruner struct {
// the overloads accordingly.
func PruneAst(expr *exprpb.Expr, macroCalls map[int64]*exprpb.Expr, state EvalState) *exprpb.ParsedExpr {
pruneState := NewEvalState()
maxID := int64(1)
for _, id := range state.IDs() {
v, _ := state.Value(id)
pruneState.SetValue(id, v)
if id > maxID {
maxID = id + 1
}
}
pruner := &astPruner{
expr: expr,
macroCalls: macroCalls,
state: pruneState,
nextExprID: 1}
nextExprID: maxID}
newExpr, _ := pruner.maybePrune(expr)
return &exprpb.ParsedExpr{
Expr: newExpr,
Expand Down
5 changes: 5 additions & 0 deletions interpreter/prune_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ var testCases = []testInfo{
expr: `test in {'a': 1, 'field': [test, 3]}.field`,
out: `test in {"a": 1, "field": [test, 3]}.field`,
},
{
in: partialActivation(map[string]any{"foo": "bar"}, "r.attr"),
expr: `foo == "bar" && r.attr.loc in ["GB", "US"]`,
out: `r.attr.loc in ["GB", "US"]`,
},
// TODO: the output of an expression like this relies on either
// a) doing replacements on the original macro call, or
// b) mutating the macro call tracking data rather than the core
Expand Down