...

Source file src/go.einride.tech/aip/filtering/exprs/match.go

Documentation: go.einride.tech/aip/filtering/exprs

     1  package exprs
     2  
     3  import (
     4  	expr "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
     5  )
     6  
     7  // Matcher returns true if the expr matches the predicate.
     8  type Matcher func(exp *expr.Expr) bool
     9  
    10  // MatchString matches an expr.Constant_StringValue with an
    11  // exact value.
    12  func MatchString(s string) Matcher {
    13  	var s2 string
    14  	m := MatchAnyString(&s2)
    15  	return func(exp *expr.Expr) bool {
    16  		return m(exp) && s == s2
    17  	}
    18  }
    19  
    20  // MatchAnyString matches an expr.Constant_StringValue with any
    21  // value. The value of the expr is populated in argument value.
    22  func MatchAnyString(value *string) Matcher {
    23  	return func(exp *expr.Expr) bool {
    24  		cons := exp.GetConstExpr()
    25  		if cons == nil {
    26  			return false
    27  		}
    28  		if _, ok := cons.GetConstantKind().(*expr.Constant_StringValue); !ok {
    29  			return false
    30  		}
    31  		*value = cons.GetStringValue()
    32  		return true
    33  	}
    34  }
    35  
    36  // MatchFloat matches an expr.Constant_DoubleValue with an exact value.
    37  func MatchFloat(value float64) Matcher {
    38  	var f2 float64
    39  	m := MatchAnyFloat(&f2)
    40  	return func(exp *expr.Expr) bool {
    41  		return m(exp) && value == f2
    42  	}
    43  }
    44  
    45  // MatchAnyFloat matches an expr.Constant_DoubleValue with any value.
    46  // The value of the expr is populated in argument value.
    47  func MatchAnyFloat(value *float64) Matcher {
    48  	return func(exp *expr.Expr) bool {
    49  		cons := exp.GetConstExpr()
    50  		if cons == nil {
    51  			return false
    52  		}
    53  		if _, ok := cons.GetConstantKind().(*expr.Constant_DoubleValue); !ok {
    54  			return false
    55  		}
    56  		*value = cons.GetDoubleValue()
    57  		return true
    58  	}
    59  }
    60  
    61  // MatchInt matches an expr.Constant_Int64Value with an exact value.
    62  func MatchInt(value int64) Matcher {
    63  	var i2 int64
    64  	m := MatchAnyInt(&i2)
    65  	return func(exp *expr.Expr) bool {
    66  		return m(exp) && value == i2
    67  	}
    68  }
    69  
    70  // MatchAnyInt matches an expr.Constant_Int64Value with any value.
    71  // The value of the expr is populated in argument value.
    72  func MatchAnyInt(value *int64) Matcher {
    73  	return func(exp *expr.Expr) bool {
    74  		cons := exp.GetConstExpr()
    75  		if cons == nil {
    76  			return false
    77  		}
    78  		if _, ok := cons.GetConstantKind().(*expr.Constant_Int64Value); !ok {
    79  			return false
    80  		}
    81  		*value = cons.GetInt64Value()
    82  		return true
    83  	}
    84  }
    85  
    86  // MatchText matches an expr.Expr_Ident where the name
    87  // of the ident matches an exact value.
    88  func MatchText(text string) Matcher {
    89  	var t2 string
    90  	m := MatchAnyText(&t2)
    91  	return func(exp *expr.Expr) bool {
    92  		return m(exp) && text == t2
    93  	}
    94  }
    95  
    96  // MatchAnyText matches an expr.Expr_Ident with any name.
    97  // The name of the expr is populated in argument text.
    98  func MatchAnyText(text *string) Matcher {
    99  	return func(exp *expr.Expr) bool {
   100  		ident := exp.GetIdentExpr()
   101  		if ident == nil {
   102  			return false
   103  		}
   104  		*text = ident.GetName()
   105  		return true
   106  	}
   107  }
   108  
   109  // MatchMember matches an expr.Expr_Select where the operand matches
   110  // the argument operand, and the field matches argument field.
   111  func MatchMember(operand Matcher, field string) Matcher {
   112  	var f2 string
   113  	m := MatchAnyMember(operand, &f2)
   114  	return func(exp *expr.Expr) bool {
   115  		return m(exp) && field == f2
   116  	}
   117  }
   118  
   119  // MatchAnyMember matches an expr.Expr_Select where the operand matches
   120  // the argument operand. The field of the expr is populated in argument field.
   121  func MatchAnyMember(operand Matcher, field *string) Matcher {
   122  	return func(exp *expr.Expr) bool {
   123  		sel := exp.GetSelectExpr()
   124  		if sel == nil {
   125  			return false
   126  		}
   127  		if !operand(sel.GetOperand()) {
   128  			return false
   129  		}
   130  		*field = sel.GetField()
   131  		return true
   132  	}
   133  }
   134  
   135  // MatchFunction matches an expr.Expr_Call where the name of the
   136  // expr matches argument name, and arguments of the function matches
   137  // the provided args (length must match).
   138  func MatchFunction(name string, args ...Matcher) Matcher {
   139  	var n2 string
   140  	m := MatchAnyFunction(&n2, args...)
   141  	return func(exp *expr.Expr) bool {
   142  		return m(exp) && name == n2
   143  	}
   144  }
   145  
   146  // MatchAnyFunction matches an expr.Expr_Call where the provided args
   147  // matches the function arguments. The name of the function is populated
   148  // in argument name.
   149  func MatchAnyFunction(name *string, args ...Matcher) Matcher {
   150  	return func(exp *expr.Expr) bool {
   151  		call := exp.GetCallExpr()
   152  		if call == nil {
   153  			return false
   154  		}
   155  		if len(call.GetArgs()) != len(args) {
   156  			return false
   157  		}
   158  		for i, a := range call.GetArgs() {
   159  			if !args[i](a) {
   160  				return false
   161  			}
   162  		}
   163  		*name = call.GetFunction()
   164  		return true
   165  	}
   166  }
   167  
   168  // MatchAny matches any expr.Expr. The expr is populated in argument e.
   169  func MatchAny(e **expr.Expr) Matcher {
   170  	return func(exp *expr.Expr) bool {
   171  		*e = exp
   172  		return true
   173  	}
   174  }
   175  

View as plain text