...
1 package exprs
2
3 import (
4 expr "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
5 )
6
7
8 type Matcher func(exp *expr.Expr) bool
9
10
11
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
21
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
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
46
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
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
71
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
87
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
97
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
110
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
120
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
136
137
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
147
148
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
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