Skip to content

Commit 7b69d6c

Browse files
committed
feat(filtering): minor changes based on review of #361
1 parent f9461fa commit 7b69d6c

File tree

5 files changed

+432
-14
lines changed

5 files changed

+432
-14
lines changed

filtering/declarations.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,9 @@ func (d *Declarations) declare(decl *expr.Decl) error {
225225
// merge merges the given declarations into the current declarations.
226226
// Given declarations take precedence over current declarations in case
227227
// of conflicts (such as same identifier name but different type or same function name but different definition).
228-
func (d *Declarations) merge(decl *Declarations) error {
228+
func (d *Declarations) merge(decl *Declarations) {
229229
if decl == nil {
230-
return nil
230+
return
231231
}
232232
for name, ident := range decl.idents {
233233
d.idents[name] = ident
@@ -238,6 +238,4 @@ func (d *Declarations) merge(decl *Declarations) error {
238238
for name, enum := range decl.enums {
239239
d.enums[name] = enum
240240
}
241-
242-
return nil
243241
}

filtering/filter.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,20 @@ type Filter struct {
1313
// ApplyMacros modifies the filter by applying the provided macros.
1414
// EXPERIMENTAL: This method is experimental and may be changed or removed in the future.
1515
func (f *Filter) ApplyMacros(macros ...Macro) error {
16-
declarationOptions := applyMacros(f.CheckedExpr.GetExpr(), f.CheckedExpr.GetSourceInfo(), f.declarations, macros...)
17-
newDeclarations, err := NewDeclarations(declarationOptions...)
16+
declarationOptions, err := applyMacros(
17+
f.CheckedExpr.GetExpr(),
18+
f.CheckedExpr.GetSourceInfo(),
19+
f.declarations,
20+
macros...,
21+
)
1822
if err != nil {
1923
return err
2024
}
21-
err = f.declarations.merge(newDeclarations)
25+
newDeclarations, err := NewDeclarations(declarationOptions...)
2226
if err != nil {
2327
return err
2428
}
29+
f.declarations.merge(newDeclarations)
2530
var checker Checker
2631
checker.Init(f.CheckedExpr.GetExpr(), f.CheckedExpr.GetSourceInfo(), f.declarations)
2732
checkedExpr, err := checker.Check()

filtering/macro.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package filtering
22

33
import (
4+
"fmt"
5+
46
expr "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
57
)
68

@@ -10,7 +12,10 @@ type Macro func(*Cursor)
1012
// ApplyMacros applies the provided macros to the filter and type-checks the result against the provided declarations.
1113
func ApplyMacros(filter Filter, declarations *Declarations, macros ...Macro) (Filter, error) {
1214
// We ignore the return value as we validate against the given declarations instead.
13-
_ = applyMacros(filter.CheckedExpr.GetExpr(), filter.CheckedExpr.GetSourceInfo(), filter.declarations, macros...)
15+
_, err := applyMacros(filter.CheckedExpr.GetExpr(), filter.CheckedExpr.GetSourceInfo(), filter.declarations, macros...)
16+
if err != nil {
17+
return Filter{}, err
18+
}
1419
var checker Checker
1520
checker.Init(filter.CheckedExpr.GetExpr(), filter.CheckedExpr.GetSourceInfo(), declarations)
1621
checkedExpr, err := checker.Check()
@@ -26,29 +31,37 @@ func applyMacros(
2631
sourceInfo *expr.SourceInfo,
2732
declarations *Declarations,
2833
macros ...Macro,
29-
) []DeclarationOption {
34+
) ([]DeclarationOption, error) {
3035
declarationOptions := make([]DeclarationOption, 0, len(macros))
3136
nextID := maxID(exp) + 1
37+
decl := declarations
38+
if declarations == nil {
39+
var err error
40+
decl, err = NewDeclarations()
41+
if err != nil {
42+
return nil, fmt.Errorf("failed to create new declarations: %w", err)
43+
}
44+
}
3245
Walk(func(currExpr, parentExpr *expr.Expr) bool {
3346
cursor := &Cursor{
3447
sourceInfo: sourceInfo,
3548
currExpr: currExpr,
3649
parentExpr: parentExpr,
3750
nextID: nextID,
38-
exprDeclarations: declarations,
51+
exprDeclarations: decl,
3952
}
4053
for _, macro := range macros {
4154
macro(cursor)
4255
nextID = cursor.nextID
43-
declarationOptions = append(declarationOptions, cursor.replaceDeclOptions...)
4456
if cursor.replaced {
57+
declarationOptions = append(declarationOptions, cursor.replaceDeclOptions...)
4558
// Don't traverse children of replaced expr.
4659
return false
4760
}
4861
}
4962
return true
5063
}, exp)
51-
return declarationOptions
64+
return declarationOptions, nil
5265
}
5366

5467
// A Cursor describes an expression encountered while applying a Macro.
@@ -77,6 +90,9 @@ func (c *Cursor) Expr() *expr.Expr {
7790
// LookupIdentType looks up the type of an ident in the filter declarations.
7891
// EXPERIMENTAL: This method is experimental and may be changed or removed in the future.
7992
func (c *Cursor) LookupIdentType(name string) (*expr.Type, bool) {
93+
if c.exprDeclarations == nil {
94+
return nil, false
95+
}
8096
ident, ok := c.exprDeclarations.LookupIdent(name)
8197
if !ok {
8298
return nil, false
@@ -100,7 +116,7 @@ func (c *Cursor) Replace(newExpr *expr.Expr) {
100116
c.replaced = true
101117
}
102118

103-
// ReplaceWithType replaces the current expression with a new expression and type.
119+
// ReplaceWithDeclarations replaces the current expression with a new expression and type.
104120
// EXPERIMENTAL: This method is experimental and may be changed or removed in the future.
105121
func (c *Cursor) ReplaceWithDeclarations(newExpr *expr.Expr, opts []DeclarationOption) {
106122
Walk(func(childExpr, _ *expr.Expr) bool {

0 commit comments

Comments
 (0)