11package filtering
22
33import (
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.
1113func 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.
7992func (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.
105121func (c * Cursor ) ReplaceWithDeclarations (newExpr * expr.Expr , opts []DeclarationOption ) {
106122 Walk (func (childExpr , _ * expr.Expr ) bool {
0 commit comments