Debian Codegraph @

debian/missing-sources/github.com/google/cel-go/test/expr.go

raw symbols SCIP
1 // Copyright 2018 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package test
16
17 import (
18 "github.com/google/cel-go/common/operators"
19
20 "google.golang.org/protobuf/proto"
21
22 exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
23 structpb "google.golang.org/protobuf/types/known/structpb"
24 )
25
26 // TestExpr packages an Expr with SourceInfo, for testing.
27 type TestExpr struct {
28 Expr *exprpb.Expr
29 SourceInfo *exprpb.SourceInfo
30 }
31
32 // Info returns a copy of the SourceInfo with the given location.
33 func (t *TestExpr) Info(location string) *exprpb.SourceInfo {
34 info := proto.Clone(t.SourceInfo).(*exprpb.SourceInfo)
35 info.Location = location
36 return info
37 }
38
39 var (
40 // Empty generates a program with no instructions.
41 Empty = &TestExpr{
42 Expr: &exprpb.Expr{},
43
44 SourceInfo: &exprpb.SourceInfo{
45 LineOffsets: []int32{},
46 Positions: map[int64]int32{}}}
47
48 // Exists generates "[1, 1u, 1.0].exists(x, type(x) == uint)".
49 Exists = &TestExpr{
50 Expr: ExprComprehension(1,
51 "x",
52 ExprList(8,
53 ExprLiteral(2, int64(0)),
54 ExprLiteral(3, int64(1)),
55 ExprLiteral(4, int64(2)),
56 ExprLiteral(5, int64(3)),
57 ExprLiteral(6, int64(4)),
58 ExprLiteral(7, uint64(5))),
59 "__result__",
60 ExprLiteral(9, false),
61 ExprCall(12,
62 operators.NotStrictlyFalse,
63 ExprCall(10,
64 operators.LogicalNot,
65 ExprIdent(11, "__result__"))),
66 ExprCall(13,
67 operators.LogicalOr,
68 ExprIdent(14, "__result__"),
69 ExprCall(15,
70 operators.Equals,
71 ExprCall(16,
72 "type",
73 ExprIdent(17, "x")),
74 ExprIdent(18, "uint"))),
75 ExprIdent(19, "__result__")),
76
77 SourceInfo: &exprpb.SourceInfo{
78 LineOffsets: []int32{0},
79 Positions: map[int64]int32{
80 0: 12,
81 1: 0,
82 2: 1,
83 3: 4,
84 4: 8,
85 5: 0,
86 6: 18,
87 7: 18,
88 8: 18,
89 9: 18,
90 10: 18,
91 11: 20,
92 12: 20,
93 13: 28,
94 14: 28,
95 15: 28,
96 16: 28,
97 17: 28,
98 18: 28,
99 19: 28}}}
100
101 // ExistsWithInput generates "elems.exists(x, type(x) == uint)".
102 ExistsWithInput = &TestExpr{
103 Expr: ExprComprehension(1,
104 "x",
105 ExprIdent(2, "elems"),
106 "__result__",
107 ExprLiteral(3, false),
108 ExprCall(4,
109 operators.LogicalNot,
110 ExprIdent(5, "__result__")),
111 ExprCall(6,
112 operators.Equals,
113 ExprCall(7,
114 "type",
115 ExprIdent(8, "x")),
116 ExprIdent(9, "uint")),
117 ExprIdent(10, "__result__")),
118
119 SourceInfo: &exprpb.SourceInfo{
120 LineOffsets: []int32{0},
121 Positions: map[int64]int32{
122 0: 12,
123 1: 0,
124 2: 1,
125 3: 4,
126 4: 8,
127 5: 0,
128 6: 18,
129 7: 18,
130 8: 18,
131 9: 18,
132 10: 18}}}
133
134 // DynMap generates a map literal:
135 // {"hello": "world".size(),
136 // "dur": duration.Duration{10},
137 // "ts": timestamp.Timestamp{1000},
138 // "null": null,
139 // "bytes": b"bytes-string"}
140 DynMap = &TestExpr{
141 Expr: ExprMap(17,
142 ExprEntry(2,
143 ExprLiteral(1, "hello"),
144 ExprMemberCall(3,
145 "size",
146 ExprLiteral(4, "world"))),
147 ExprEntry(12,
148 ExprLiteral(11, "null"),
149 ExprLiteral(13, structpb.NullValue_NULL_VALUE)),
150 ExprEntry(15,
151 ExprLiteral(14, "bytes"),
152 ExprLiteral(16, []byte("bytes-string")))),
153
154 SourceInfo: &exprpb.SourceInfo{
155 LineOffsets: []int32{},
156 Positions: map[int64]int32{}}}
157
158 // LogicalAnd generates "a && {c: true}.c".
159 LogicalAnd = &TestExpr{
160 ExprCall(2, operators.LogicalAnd,
161 ExprIdent(1, "a"),
162 ExprSelect(8,
163 ExprMap(5,
164 ExprEntry(4, ExprLiteral(6, "c"), ExprLiteral(7, true))),
165 "c")),
166 &exprpb.SourceInfo{
167 LineOffsets: []int32{},
168 Positions: map[int64]int32{}}}
169
170 // LogicalOr generates "{c: false}.c || a".
171 LogicalOr = &TestExpr{
172 ExprCall(2, operators.LogicalOr,
173 ExprSelect(8,
174 ExprMap(5,
175 ExprEntry(4, ExprLiteral(6, "c"), ExprLiteral(7, false))),
176 "c"),
177 ExprIdent(1, "a")),
178 &exprpb.SourceInfo{
179 LineOffsets: []int32{},
180 Positions: map[int64]int32{}}}
181
182 // LogicalOrEquals generates "a || b == 'b'".
183 LogicalOrEquals = &TestExpr{
184 ExprCall(5, operators.LogicalOr,
185 ExprIdent(1, "a"),
186 ExprCall(4, operators.Equals,
187 ExprIdent(2, "b"),
188 ExprLiteral(3, "b"))),
189 &exprpb.SourceInfo{
190 LineOffsets: []int32{},
191 Positions: map[int64]int32{}}}
192
193 // LogicalAndMissingType generates "a && TestProto{c: true}.c" where the
194 // type 'TestProto' is undefined.
195 LogicalAndMissingType = &TestExpr{
196 ExprCall(2, operators.LogicalAnd,
197 ExprIdent(1, "a"),
198 ExprSelect(7,
199 ExprType(5, "TestProto",
200 ExprField(4, "c", ExprLiteral(6, true))),
201 "c")),
202 &exprpb.SourceInfo{
203 LineOffsets: []int32{},
204 Positions: map[int64]int32{}}}
205
206 // Conditional generates "a ? b < 1.0 : c == ["hello"]".
207 Conditional = &TestExpr{
208 Expr: ExprCall(9, operators.Conditional,
209 ExprIdent(1, "a"),
210 ExprCall(3,
211 operators.Less,
212 ExprIdent(2, "b"),
213 ExprLiteral(4, 1.0)),
214 ExprCall(6,
215 operators.Equals,
216 ExprIdent(5, "c"),
217 ExprList(8, ExprLiteral(7, "hello")))),
218 SourceInfo: &exprpb.SourceInfo{
219 LineOffsets: []int32{},
220 Positions: map[int64]int32{}}}
221
222 // Select generates "a.b.c".
223 Select = &TestExpr{
224 Expr: ExprSelect(3,
225 ExprSelect(2,
226 ExprIdent(1, "a"),
227 "b"),
228 "c"),
229 SourceInfo: &exprpb.SourceInfo{
230 LineOffsets: []int32{},
231 Positions: map[int64]int32{}}}
232
233 // Equality generates "a == 42".
234 Equality = &TestExpr{
235 Expr: ExprCall(2,
236 operators.Equals,
237 ExprIdent(1, "a"),
238 ExprLiteral(3, int64(42))),
239 SourceInfo: &exprpb.SourceInfo{
240 LineOffsets: []int32{},
241 Positions: map[int64]int32{}}}
242
243 // TypeEquality generates "type(a) == uint".
244 TypeEquality = &TestExpr{
245 Expr: ExprCall(4,
246 operators.Equals,
247 ExprCall(1, "type",
248 ExprIdent(2, "a")),
249 ExprIdent(3, "uint")),
250 SourceInfo: &exprpb.SourceInfo{
251 LineOffsets: []int32{},
252 Positions: map[int64]int32{}}}
253 )
254
255 // ExprIdent creates an ident (variable) Expr.
256 func ExprIdent(id int64, name string) *exprpb.Expr {
257 return &exprpb.Expr{Id: id, ExprKind: &exprpb.Expr_IdentExpr{
258 IdentExpr: &exprpb.Expr_Ident{Name: name}}}
259 }
260
261 // ExprSelect creates a select Expr.
262 func ExprSelect(id int64, operand *exprpb.Expr, field string) *exprpb.Expr {
263 return &exprpb.Expr{Id: id,
264 ExprKind: &exprpb.Expr_SelectExpr{
265 SelectExpr: &exprpb.Expr_Select{
266 Operand: operand,
267 Field: field,
268 TestOnly: false}}}
269 }
270
271 // ExprLiteral creates a literal (constant) Expr.
272 func ExprLiteral(id int64, value interface{}) *exprpb.Expr {
273 var literal *exprpb.Constant
274 switch value.(type) {
275 case bool:
276 literal = &exprpb.Constant{ConstantKind: &exprpb.Constant_BoolValue{
277 BoolValue: value.(bool)}}
278 case int64:
279 literal = &exprpb.Constant{ConstantKind: &exprpb.Constant_Int64Value{
280 Int64Value: value.(int64)}}
281 case uint64:
282 literal = &exprpb.Constant{ConstantKind: &exprpb.Constant_Uint64Value{
283 Uint64Value: value.(uint64)}}
284 case float64:
285 literal = &exprpb.Constant{ConstantKind: &exprpb.Constant_DoubleValue{
286 DoubleValue: value.(float64)}}
287 case string:
288 literal = &exprpb.Constant{ConstantKind: &exprpb.Constant_StringValue{
289 StringValue: value.(string)}}
290 case structpb.NullValue:
291 literal = &exprpb.Constant{ConstantKind: &exprpb.Constant_NullValue{
292 NullValue: value.(structpb.NullValue)}}
293 case []byte:
294 literal = &exprpb.Constant{ConstantKind: &exprpb.Constant_BytesValue{
295 BytesValue: value.([]byte)}}
296 default:
297 panic("literal type not implemented")
298 }
299 return &exprpb.Expr{Id: id, ExprKind: &exprpb.Expr_ConstExpr{ConstExpr: literal}}
300 }
301
302 // ExprCall creates a call Expr.
303 func ExprCall(id int64, function string, args ...*exprpb.Expr) *exprpb.Expr {
304 return &exprpb.Expr{Id: id,
305 ExprKind: &exprpb.Expr_CallExpr{
306 CallExpr: &exprpb.Expr_Call{Target: nil, Function: function, Args: args}}}
307 }
308
309 // ExprMemberCall creates a receiver-style call Expr.
310 func ExprMemberCall(id int64, function string, target *exprpb.Expr, args ...*exprpb.Expr) *exprpb.Expr {
311 return &exprpb.Expr{Id: id,
312 ExprKind: &exprpb.Expr_CallExpr{
313 CallExpr: &exprpb.Expr_Call{Target: target, Function: function, Args: args}}}
314 }
315
316 // ExprList creates a create list Expr.
317 func ExprList(id int64, elements ...*exprpb.Expr) *exprpb.Expr {
318 return &exprpb.Expr{Id: id,
319 ExprKind: &exprpb.Expr_ListExpr{
320 ListExpr: &exprpb.Expr_CreateList{Elements: elements}}}
321 }
322
323 // ExprMap creates a create struct Expr for a map.
324 func ExprMap(id int64, entries ...*exprpb.Expr_CreateStruct_Entry) *exprpb.Expr {
325 return &exprpb.Expr{Id: id, ExprKind: &exprpb.Expr_StructExpr{
326 StructExpr: &exprpb.Expr_CreateStruct{Entries: entries}}}
327 }
328
329 // ExprType creates creates a create struct Expr for a message.
330 func ExprType(id int64, messageName string,
331 entries ...*exprpb.Expr_CreateStruct_Entry) *exprpb.Expr {
332 return &exprpb.Expr{Id: id, ExprKind: &exprpb.Expr_StructExpr{
333 StructExpr: &exprpb.Expr_CreateStruct{
334 MessageName: messageName, Entries: entries}}}
335 }
336
337 // ExprEntry creates a map entry for a create struct Expr.
338 func ExprEntry(id int64, key *exprpb.Expr,
339 value *exprpb.Expr) *exprpb.Expr_CreateStruct_Entry {
340 return &exprpb.Expr_CreateStruct_Entry{Id: id,
341 KeyKind: &exprpb.Expr_CreateStruct_Entry_MapKey{MapKey: key},
342 Value: value}
343 }
344
345 // ExprField creates a field entry for a create struct Expr.
346 func ExprField(id int64, field string,
347 value *exprpb.Expr) *exprpb.Expr_CreateStruct_Entry {
348 return &exprpb.Expr_CreateStruct_Entry{Id: id,
349 KeyKind: &exprpb.Expr_CreateStruct_Entry_FieldKey{FieldKey: field},
350 Value: value}
351 }
352
353 // ExprComprehension returns a comprehension Expr.
354 func ExprComprehension(id int64,
355 iterVar string, iterRange *exprpb.Expr,
356 accuVar string, accuInit *exprpb.Expr,
357 loopCondition *exprpb.Expr, loopStep *exprpb.Expr,
358 resultExpr *exprpb.Expr) *exprpb.Expr {
359 return &exprpb.Expr{Id: id,
360 ExprKind: &exprpb.Expr_ComprehensionExpr{
361 ComprehensionExpr: &exprpb.Expr_Comprehension{
362 IterVar: iterVar,
363 IterRange: iterRange,
364 AccuVar: accuVar,
365 AccuInit: accuInit,
366 LoopCondition: loopCondition,
367 LoopStep: loopStep,
368 Result: resultExpr}}}
369 }