Debian Codegraph @

ext/README.md

raw
1 # Extensions
2
3 CEL extensions are a related set of constants, functions, macros, or other
4 features which may not be covered by the core CEL spec.
5
6 ## Bindings
7
8 Returns a cel.EnvOption to configure support for local variable bindings
9 in expressions.
10
11 ### Cel.Bind
12
13 Binds a simple identifier to an initialization expression which may be used
14 in a subsequent result expression. Bindings may also be nested within each
15 other.
16
17 cel.bind(<varName>, <initExpr>, <resultExpr>)
18
19 Examples:
20
21 cel.bind(a, 'hello',
22 cel.bind(b, 'world', a + b + b + a)) // "helloworldworldhello"
23
24 // Avoid a list allocation within the exists comprehension.
25 cel.bind(valid_values, [a, b, c],
26 [d, e, f].exists(elem, elem in valid_values))
27
28 Local bindings are not guaranteed to be evaluated before use.
29
30 ## Encoders
31
32 Encoding utilities for marshalling data into standardized representations.
33
34 ### Base64.Decode
35
36 Decodes base64-encoded string to bytes.
37
38 This function will return an error if the string input is not
39 base64-encoded.
40
41 base64.decode(<string>) -> <bytes>
42
43 Examples:
44
45 base64.decode('aGVsbG8=') // return b'hello'
46 base64.decode('aGVsbG8') // error
47
48 ### Base64.Encode
49
50 Encodes bytes to a base64-encoded string.
51
52 base64.encode(<bytes>) -> <string>
53
54 Example:
55
56 base64.encode(b'hello') // return 'aGVsbG8='
57
58 ## Math
59
60 Math helper macros and functions.
61
62 Note, all macros use the 'math' namespace; however, at the time of macro
63 expansion the namespace looks just like any other identifier. If you are
64 currently using a variable named 'math', the macro will likely work just as
65 intended; however, there is some chance for collision.
66
67 ### Math.Greatest
68
69 Returns the greatest valued number present in the arguments to the macro.
70
71 Greatest is a variable argument count macro which must take at least one
72 argument. Simple numeric and list literals are supported as valid argument
73 types; however, other literals will be flagged as errors during macro
74 expansion. If the argument expression does not resolve to a numeric or
75 list(numeric) type during type-checking, or during runtime then an error
76 will be produced. If a list argument is empty, this too will produce an
77 error.
78
79 math.greatest(<arg>, ...) -> <double|int|uint>
80
81 Examples:
82
83 math.greatest(1) // 1
84 math.greatest(1u, 2u) // 2u
85 math.greatest(-42.0, -21.5, -100.0) // -21.5
86 math.greatest([-42.0, -21.5, -100.0]) // -21.5
87 math.greatest(numbers) // numbers must be list(numeric)
88
89 math.greatest() // parse error
90 math.greatest('string') // parse error
91 math.greatest(a, b) // check-time error if a or b is non-numeric
92 math.greatest(dyn('string')) // runtime error
93
94 ### Math.Least
95
96 Returns the least valued number present in the arguments to the macro.
97
98 Least is a variable argument count macro which must take at least one
99 argument. Simple numeric and list literals are supported as valid argument
100 types; however, other literals will be flagged as errors during macro
101 expansion. If the argument expression does not resolve to a numeric or
102 list(numeric) type during type-checking, or during runtime then an error
103 will be produced. If a list argument is empty, this too will produce an
104 error.
105
106 math.least(<arg>, ...) -> <double|int|uint>
107
108 Examples:
109
110 math.least(1) // 1
111 math.least(1u, 2u) // 1u
112 math.least(-42.0, -21.5, -100.0) // -100.0
113 math.least([-42.0, -21.5, -100.0]) // -100.0
114 math.least(numbers) // numbers must be list(numeric)
115
116 math.least() // parse error
117 math.least('string') // parse error
118 math.least(a, b) // check-time error if a or b is non-numeric
119 math.least(dyn('string')) // runtime error
120
121 ### Math.BitOr
122
123 Introduced at version: 1
124
125 Performs a bitwise-OR operation over two int or uint values.
126
127 math.bitOr(<int>, <int>) -> <int>
128 math.bitOr(<uint>, <uint>) -> <uint>
129
130 Examples:
131
132 math.bitOr(1u, 2u) // returns 3u
133 math.bitOr(-2, -4) // returns -2
134
135 ### Math.BitAnd
136
137 Introduced at version: 1
138
139 Performs a bitwise-AND operation over two int or uint values.
140
141 math.bitAnd(<int>, <int>) -> <int>
142 math.bitAnd(<uint>, <uint>) -> <uint>
143
144 Examples:
145
146 math.bitAnd(3u, 2u) // return 2u
147 math.bitAnd(3, 5) // returns 3
148 math.bitAnd(-3, -5) // returns -7
149
150 ### Math.BitXor
151
152 Introduced at version: 1
153
154 math.bitXor(<int>, <int>) -> <int>
155 math.bitXor(<uint>, <uint>) -> <uint>
156
157 Performs a bitwise-XOR operation over two int or uint values.
158
159 Examples:
160
161 math.bitXor(3u, 5u) // returns 6u
162 math.bitXor(1, 3) // returns 2
163
164 ### Math.BitNot
165
166 Introduced at version: 1
167
168 Function which accepts a single int or uint and performs a bitwise-NOT
169 ones-complement of the given binary value.
170
171 math.bitNot(<int>) -> <int>
172 math.bitNot(<uint>) -> <uint>
173
174 Examples
175
176 math.bitNot(1) // returns -1
177 math.bitNot(-1) // return 0
178 math.bitNot(0u) // returns 18446744073709551615u
179
180 ### Math.BitShiftLeft
181
182 Introduced at version: 1
183
184 Perform a left shift of bits on the first parameter, by the amount of bits
185 specified in the second parameter. The first parameter is either a uint or
186 an int. The second parameter must be an int.
187
188 When the second parameter is 64 or greater, 0 will be always be returned
189 since the number of bits shifted is greater than or equal to the total bit
190 length of the number being shifted. Negative valued bit shifts will result
191 in a runtime error.
192
193 math.bitShiftLeft(<int>, <int>) -> <int>
194 math.bitShiftLeft(<uint>, <int>) -> <uint>
195
196 Examples
197
198 math.bitShiftLeft(1, 2) // returns 4
199 math.bitShiftLeft(-1, 2) // returns -4
200 math.bitShiftLeft(1u, 2) // return 4u
201 math.bitShiftLeft(1u, 200) // returns 0u
202
203 ### Math.BitShiftRight
204
205 Introduced at version: 1
206
207 Perform a right shift of bits on the first parameter, by the amount of bits
208 specified in the second parameter. The first parameter is either a uint or
209 an int. The second parameter must be an int.
210
211 When the second parameter is 64 or greater, 0 will always be returned since
212 the number of bits shifted is greater than or equal to the total bit length
213 of the number being shifted. Negative valued bit shifts will result in a
214 runtime error.
215
216 The sign bit extension will not be preserved for this operation: vacant bits
217 on the left are filled with 0.
218
219 math.bitShiftRight(<int>, <int>) -> <int>
220 math.bitShiftRight(<uint>, <int>) -> <uint>
221
222 Examples
223
224 math.bitShiftRight(1024, 2) // returns 256
225 math.bitShiftRight(1024u, 2) // returns 256u
226 math.bitShiftRight(1024u, 64) // returns 0u
227
228 ### Math.Ceil
229
230 Introduced at version: 1
231
232 Compute the ceiling of a double value.
233
234 math.ceil(<double>) -> <double>
235
236 Examples:
237
238 math.ceil(1.2) // returns 2.0
239 math.ceil(-1.2) // returns -1.0
240
241 ### Math.Floor
242
243 Introduced at version: 1
244
245 Compute the floor of a double value.
246
247 math.floor(<double>) -> <double>
248
249 Examples:
250
251 math.floor(1.2) // returns 1.0
252 math.floor(-1.2) // returns -2.0
253
254 ### Math.Round
255
256 Introduced at version: 1
257
258 Rounds the double value to the nearest whole number with ties rounding away
259 from zero, e.g. 1.5 -> 2.0, -1.5 -> -2.0.
260
261 math.round(<double>) -> <double>
262
263 Examples:
264
265 math.round(1.2) // returns 1.0
266 math.round(1.5) // returns 2.0
267 math.round(-1.5) // returns -2.0
268
269 ### Math.Trunc
270
271 Introduced at version: 1
272
273 Truncates the fractional portion of the double value.
274
275 math.trunc(<double>) -> <double>
276
277 Examples:
278
279 math.trunc(-1.3) // returns -1.0
280 math.trunc(1.3) // returns 1.0
281
282 ### Math.Abs
283
284 Introduced at version: 1
285
286 Returns the absolute value of the numeric type provided as input. If the
287 value is NaN, the output is NaN. If the input is int64 min, the function
288 will result in an overflow error.
289
290 math.abs(<double>) -> <double>
291 math.abs(<int>) -> <int>
292 math.abs(<uint>) -> <uint>
293
294 Examples:
295
296 math.abs(-1) // returns 1
297 math.abs(1) // returns 1
298 math.abs(-9223372036854775808) // overlflow error
299
300 ### Math.Sign
301
302 Introduced at version: 1
303
304 Returns the sign of the numeric type, either -1, 0, 1 as an int, double, or
305 uint depending on the overload. For floating point values, if NaN is
306 provided as input, the output is also NaN. The implementation does not
307 differentiate between positive and negative zero.
308
309 math.sign(<double>) -> <double>
310 math.sign(<int>) -> <int>
311 math.sign(<uint>) -> <uint>
312
313 Examples:
314
315 math.sign(-42) // returns -1
316 math.sign(0) // returns 0
317 math.sign(42) // returns 1
318
319 ### Math.IsInf
320
321 Introduced at version: 1
322
323 Returns true if the input double value is -Inf or +Inf.
324
325 math.isInf(<double>) -> <bool>
326
327 Examples:
328
329 math.isInf(1.0/0.0) // returns true
330 math.isInf(1.2) // returns false
331
332 ### Math.IsNaN
333
334 Introduced at version: 1
335
336 Returns true if the input double value is NaN, false otherwise.
337
338 math.isNaN(<double>) -> <bool>
339
340 Examples:
341
342 math.isNaN(0.0/0.0) // returns true
343 math.isNaN(1.2) // returns false
344
345 ### Math.IsFinite
346
347 Introduced at version: 1
348
349 Returns true if the value is a finite number. Equivalent in behavior to:
350 !math.isNaN(double) && !math.isInf(double)
351
352 math.isFinite(<double>) -> <bool>
353
354 Examples:
355
356 math.isFinite(0.0/0.0) // returns false
357 math.isFinite(1.2) // returns true
358
359 ### Math.Sqrt
360
361 Introduced at version: 2
362
363 Returns the square root of the given input as double
364 Throws error for negative or non-numeric inputs
365
366 math.sqrt(<double>) -> <double>
367 math.sqrt(<int>) -> <double>
368 math.sqrt(<uint>) -> <double>
369
370 Examples:
371
372 math.sqrt(81) // returns 9.0
373 math.sqrt(985.25) // returns 31.388692231439016
374 math.sqrt(-15) // returns NaN
375
376 ## Protos
377
378 Protos configure extended macros and functions for proto manipulation.
379
380 Note, all macros use the 'proto' namespace; however, at the time of macro
381 expansion the namespace looks just like any other identifier. If you are
382 currently using a variable named 'proto', the macro will likely work just as
383 you intend; however, there is some chance for collision.
384
385 ### Protos.GetExt
386
387 Macro which generates a select expression that retrieves an extension field
388 from the input proto2 syntax message. If the field is not set, the default
389 value forthe extension field is returned according to safe-traversal semantics.
390
391 proto.getExt(<msg>, <fully.qualified.extension.name>) -> <field-type>
392
393 Example:
394
395 proto.getExt(msg, google.expr.proto2.test.int32_ext) // returns int value
396
397 ### Protos.HasExt
398
399 Macro which generates a test-only select expression that determines whether
400 an extension field is set on a proto2 syntax message.
401
402 proto.hasExt(<msg>, <fully.qualified.extension.name>) -> <bool>
403
404 Example:
405
406 proto.hasExt(msg, google.expr.proto2.test.int32_ext) // returns true || false
407
408 ## Lists
409
410 Extended functions for list manipulation. As a general note, all indices are
411 zero-based.
412
413 ### Distinct
414
415 **Introduced in version 2 (cost support in version 3)**
416
417 Returns the distinct elements of a list.
418
419 <list(T)>.distinct() -> <list(T)>
420
421 Examples:
422
423 [1, 2, 2, 3, 3, 3].distinct() // return [1, 2, 3]
424 ["b", "b", "c", "a", "c"].distinct() // return ["b", "c", "a"]
425 [1, "b", 2, "b"].distinct() // return [1, "b", 2]
426
427 ### Flatten
428
429 **Introduced in version 1 (cost support in version 3)**
430
431 Flattens a list recursively.
432 If an optional depth is provided, the list is flattened to a the specificied level.
433 A negative depth value will result in an error.
434
435 <list>.flatten(<list>) -> <list>
436 <list>.flatten(<list>, <int>) -> <list>
437
438 Examples:
439
440 [1,[2,3],[4]].flatten() // return [1, 2, 3, 4]
441 [1,[2,[3,4]]].flatten() // return [1, 2, [3, 4]]
442 [1,2,[],[],[3,4]].flatten() // return [1, 2, 3, 4]
443 [1,[2,[3,[4]]]].flatten(2) // return [1, 2, 3, [4]]
444 [1,[2,[3,[4]]]].flatten(-1) // error
445
446 ### Range
447
448 **Introduced in version 2 (cost support in version 3)**
449
450 Returns a list of integers from 0 to n-1.
451
452 lists.range(<int>) -> <list(int)>
453
454 Examples:
455
456 lists.range(5) -> [0, 1, 2, 3, 4]
457
458
459 ### Reverse
460
461 **Introduced in version 2 (cost support in version 3)**
462
463 Returns the elements of a list in reverse order.
464
465 <list(T)>.reverse() -> <list(T)>
466
467 Examples:
468
469 [5, 3, 1, 2].reverse() // return [2, 1, 3, 5]
470
471
472 ### Slice
473
474 **Introduced in version 0 (cost support in version 3)**
475
476 Returns a new sub-list using the indexes provided.
477
478 <list>.slice(<int>, <int>) -> <list>
479
480 Examples:
481
482 [1,2,3,4].slice(1, 3) // return [2, 3]
483 [1,2,3,4].slice(2, 4) // return [3, 4]
484
485 ### Sort
486
487 **Introduced in version 2 (cost support in version 3)**
488
489 Sorts a list with comparable elements. If the element type is not comparable
490 or the element types are not the same, the function will produce an error.
491
492 <list(T)>.sort() -> <list(T)>
493 T in {int, uint, double, bool, duration, timestamp, string, bytes}
494
495 Examples:
496
497 [3, 2, 1].sort() // return [1, 2, 3]
498 ["b", "c", "a"].sort() // return ["a", "b", "c"]
499 [1, "b"].sort() // error
500 [[1, 2, 3]].sort() // error
501
502 ### SortBy
503
504 **Introduced in version 2 (cost support in version 3)**
505
506 Sorts a list by a key value, i.e., the order is determined by the result of
507 an expression applied to each element of the list.
508
509 <list(T)>.sortBy(<bindingName>, <keyExpr>) -> <list(T)>
510 keyExpr returns a value in {int, uint, double, bool, duration, timestamp, string, bytes}
511
512 Examples:
513
514 [
515 Player { name: "foo", score: 0 },
516 Player { name: "bar", score: -10 },
517 Player { name: "baz", score: 1000 },
518 ].sortBy(e, e.score).map(e, e.name)
519 == ["bar", "foo", "baz"]
520
521 ### Last
522
523 **Introduced in the OptionalTypes library version 2**
524
525 Returns an optional with the last value from the list or `optional.None` if the
526 list is empty.
527
528 <list(T)>.last() -> <Optional(T)>
529
530 Examples:
531
532 [1, 2, 3].last().value() == 3
533 [].last().orValue('test') == 'test'
534
535 This is syntactic sugar for list[list.size()-1].
536
537 ### First
538
539 **Introduced in the OptionalTypes library version 2**
540
541 Returns an optional with the first value from the list or `optional.None` if the
542 list is empty.
543
544 <list(T)>.first() -> <Optional(T)>
545
546 Examples:
547
548 [1, 2, 3].first().value() == 1
549 [].first().orValue('test') == 'test'
550
551 ## Sets
552
553 Sets provides set relationship tests.
554
555 There is no set type within CEL, and while one may be introduced in the
556 future, there are cases where a `list` type is known to behave like a set.
557 For such cases, this library provides some basic functionality for
558 determining set containment, equivalence, and intersection.
559
560 ### Sets.Contains
561
562 Returns whether the first list argument contains all elements in the second
563 list argument. The list may contain elements of any type and standard CEL
564 equality is used to determine whether a value exists in both lists. If the
565 second list is empty, the result will always return true.
566
567 sets.contains(list(T), list(T)) -> bool
568
569 Examples:
570
571 sets.contains([], []) // true
572 sets.contains([], [1]) // false
573 sets.contains([1, 2, 3, 4], [2, 3]) // true
574 sets.contains([1, 2.0, 3u], [1.0, 2u, 3]) // true
575
576 ### Sets.Equivalent
577
578 Returns whether the first and second list are set equivalent. Lists are set
579 equivalent if for every item in the first list, there is an element in the
580 second which is equal. The lists may not be of the same size as they do not
581 guarantee the elements within them are unique, so size does not factor into
582 the computation.
583
584 sets.equivalent(list(T), list(T)) -> bool
585
586 Examples:
587
588 sets.equivalent([], []) // true
589 sets.equivalent([1], [1, 1]) // true
590 sets.equivalent([1], [1u, 1.0]) // true
591 sets.equivalent([1, 2, 3], [3u, 2.0, 1]) // true
592
593 ### Sets.Intersects
594
595 Returns whether the first list has at least one element whose value is equal
596 to an element in the second list. If either list is empty, the result will
597 be false.
598
599 sets.intersects(list(T), list(T)) -> bool
600
601 Examples:
602
603 sets.intersects([1], []) // false
604 sets.intersects([1], [1, 2]) // true
605 sets.intersects([[1], [2, 3]], [[1, 2], [2, 3.0]]) // true
606
607 ## Strings
608
609 Extended functions for string manipulation. As a general note, all indices are
610 zero-based.
611
612 ### CharAt
613
614 Returns the character at the given position. If the position is negative, or
615 greater than the length of the string, the function will produce an error:
616
617 <string>.charAt(<int>) -> <string>
618
619 Examples:
620
621 'hello'.charAt(4) // return 'o'
622 'hello'.charAt(5) // return ''
623 'hello'.charAt(-1) // error
624
625 ### IndexOf
626
627 Returns the integer index of the first occurrence of the search string. If the
628 search string is not found the function returns -1.
629
630 The function also accepts an optional position from which to begin the
631 substring search. If the substring is the empty string, the index where the
632 search starts is returned (zero or custom).
633
634 <string>.indexOf(<string>) -> <int>
635 <string>.indexOf(<string>, <int>) -> <int>
636
637 Examples:
638
639 'hello mellow'.indexOf('') // returns 0
640 'hello mellow'.indexOf('ello') // returns 1
641 'hello mellow'.indexOf('jello') // returns -1
642 'hello mellow'.indexOf('', 2) // returns 2
643 'hello mellow'.indexOf('ello', 2) // returns 7
644 'hello mellow'.indexOf('ello', 20) // returns -1
645 'hello mellow'.indexOf('ello', -1) // error
646
647 ### Join
648
649 Returns a new string where the elements of string list are concatenated.
650
651 The function also accepts an optional separator which is placed between
652 elements in the resulting string.
653
654 <list<string>>.join() -> <string>
655 <list<string>>.join(<string>) -> <string>
656
657 Examples:
658
659 ['hello', 'mellow'].join() // returns 'hellomellow'
660 ['hello', 'mellow'].join(' ') // returns 'hello mellow'
661 [].join() // returns ''
662 [].join('/') // returns ''
663
664 ### LastIndexOf
665
666 Returns the integer index of the last occurrence of the search string. If the
667 search string is not found the function returns -1.
668
669 The function also accepts an optional position which represents the last index
670 to be considered as the beginning of the substring match. If the substring is
671 the empty string, the index where the search starts is returned (string length
672 or custom).
673
674 <string>.lastIndexOf(<string>) -> <int>
675 <string>.lastIndexOf(<string>, <int>) -> <int>
676
677 Examples:
678
679 'hello mellow'.lastIndexOf('') // returns 12
680 'hello mellow'.lastIndexOf('ello') // returns 7
681 'hello mellow'.lastIndexOf('jello') // returns -1
682 'hello mellow'.lastIndexOf('ello', 6) // returns 1
683 'hello mellow'.lastIndexOf('ello', 20) // returns -1
684 'hello mellow'.lastIndexOf('ello', -1) // error
685
686 ### LowerAscii
687
688 Returns a new string where all ASCII characters are lower-cased.
689
690 This function does not perform Unicode case-mapping for characters outside the
691 ASCII range.
692
693 <string>.lowerAscii() -> <string>
694
695 Examples:
696
697 'TacoCat'.lowerAscii() // returns 'tacocat'
698 'TacoCÆt Xii'.lowerAscii() // returns 'tacocÆt xii'
699
700 ### Quote
701
702 **Introduced in version 1**
703
704 Takes the given string and makes it safe to print (without any formatting due to escape sequences).
705 If any invalid UTF-8 characters are encountered, they are replaced with \uFFFD.
706
707 strings.quote(<string>)
708
709 Examples:
710
711 strings.quote('single-quote with "double quote"') // returns '"single-quote with \"double quote\""'
712 strings.quote("two escape sequences \a\n") // returns '"two escape sequences \\a\\n"'
713
714 ### Replace
715
716 Returns a new string based on the target, which replaces the occurrences of a
717 search string with a replacement string if present. The function accepts an
718 optional limit on the number of substring replacements to be made.
719
720 When the replacement limit is 0, the result is the original string. When the
721 limit is a negative number, the function behaves the same as replace all.
722
723 <string>.replace(<string>, <string>) -> <string>
724 <string>.replace(<string>, <string>, <int>) -> <string>
725
726 Examples:
727
728 'hello hello'.replace('he', 'we') // returns 'wello wello'
729 'hello hello'.replace('he', 'we', -1) // returns 'wello wello'
730 'hello hello'.replace('he', 'we', 1) // returns 'wello hello'
731 'hello hello'.replace('he', 'we', 0) // returns 'hello hello'
732
733 ### Split
734
735 Returns a list of strings split from the input by the given separator. The
736 function accepts an optional argument specifying a limit on the number of
737 substrings produced by the split.
738
739 When the split limit is 0, the result is an empty list. When the limit is 1,
740 the result is the target string to split. When the limit is a negative
741 number, the function behaves the same as split all.
742
743 <string>.split(<string>) -> <list<string>>
744 <string>.split(<string>, <int>) -> <list<string>>
745
746 Examples:
747
748 'hello hello hello'.split(' ') // returns ['hello', 'hello', 'hello']
749 'hello hello hello'.split(' ', 0) // returns []
750 'hello hello hello'.split(' ', 1) // returns ['hello hello hello']
751 'hello hello hello'.split(' ', 2) // returns ['hello', 'hello hello']
752 'hello hello hello'.split(' ', -1) // returns ['hello', 'hello', 'hello']
753
754 ### Substring
755
756 Returns the substring given a numeric range corresponding to character
757 positions. Optionally may omit the trailing range for a substring from a given
758 character position until the end of a string.
759
760 Character offsets are 0-based with an inclusive start range and exclusive end
761 range. It is an error to specify an end range that is lower than the start
762 range, or for either the start or end index to be negative or exceed the string
763 length.
764
765 <string>.substring(<int>) -> <string>
766 <string>.substring(<int>, <int>) -> <string>
767
768 Examples:
769
770 'tacocat'.substring(4) // returns 'cat'
771 'tacocat'.substring(0, 4) // returns 'taco'
772 'tacocat'.substring(-1) // error
773 'tacocat'.substring(2, 1) // error
774
775 ### Trim
776
777 Returns a new string which removes the leading and trailing whitespace in the
778 target string. The trim function uses the Unicode definition of whitespace
779 which does not include the zero-width spaces. See:
780 https://en.wikipedia.org/wiki/Whitespace_character#Unicode
781
782 <string>.trim() -> <string>
783
784 Examples:
785
786 ' \ttrim\n '.trim() // returns 'trim'
787
788 ### UpperAscii
789
790 Returns a new string where all ASCII characters are upper-cased.
791
792 This function does not perform Unicode case-mapping for characters outside the
793 ASCII range.
794
795 <string>.upperAscii() -> <string>
796
797 Examples:
798
799 'TacoCat'.upperAscii() // returns 'TACOCAT'
800 'TacoCÆt Xii'.upperAscii() // returns 'TACOCÆT XII'
801
802 ### Reverse
803
804 Returns a new string whose characters are the same as the target string, only formatted in
805 reverse order.
806 This function relies on converting strings to rune arrays in order to reverse.
807 It can be located in Version 3 of strings.
808
809 <string>.reverse() -> <string>
810
811 Examples:
812
813 'gums'.reverse() // returns 'smug'
814 'John Smith'.reverse() // returns 'htimS nhoJ'
815
816 ## TwoVarComprehensions
817
818 TwoVarComprehensions introduces support for two-variable comprehensions.
819
820 The two-variable form of comprehensions looks similar to the one-variable
821 counterparts. Where possible, the same macro names were used and additional
822 macro signatures added. The notable distinction for two-variable comprehensions
823 is the introduction of `transformList`, `transformMap`, and `transformMapEntry`
824 support for list and map types rather than the more traditional `map` and
825 `filter` macros.
826
827 ### All
828
829 Comprehension which tests whether all elements in the list or map satisfy a
830 given predicate. The `all` macro evaluates in a manner consistent with logical
831 AND and will short-circuit when encountering a `false` value.
832
833 <list>.all(indexVar, valueVar, <predicate>) -> bool
834 <map>.all(keyVar, valueVar, <predicate>) -> bool
835
836 Examples:
837
838 [1, 2, 3].all(i, j, i < j) // returns true
839 {'hello': 'world', 'taco': 'taco'}.all(k, v, k != v) // returns false
840
841 // Combines two-variable comprehension with single variable
842 {'h': ['hello', 'hi'], 'j': ['joke', 'jog']}
843 .all(k, vals, vals.all(v, v.startsWith(k))) // returns true
844
845 ### Exists
846
847 Comprehension which tests whether any element in a list or map exists which
848 satisfies a given predicate. The `exists` macro evaluates in a manner consistent
849 with logical OR and will short-circuit when encountering a `true` value.
850
851 <list>.exists(indexVar, valueVar, <predicate>) -> bool
852 <map>.exists(keyVar, valueVar, <predicate>) -> bool
853
854 Examples:
855
856 {'greeting': 'hello', 'farewell': 'goodbye'}
857 .exists(k, v, k.startsWith('good') || v.endsWith('bye')) // returns true
858 [1, 2, 4, 8, 16].exists(i, v, v == 1024 && i == 10) // returns false
859
860 ### ExistsOne
861
862 Comprehension which tests whether exactly one element in a list or map exists
863 which satisfies a given predicate expression. This comprehension does not
864 short-circuit in keeping with the one-variable exists one macro semantics.
865
866 <list>.existsOne(indexVar, valueVar, <predicate>)
867 <map>.existsOne(keyVar, valueVar, <predicate>)
868
869 This macro may also be used with the `exists_one` function name, for
870 compatibility with the one-variable macro of the same name.
871
872 Examples:
873
874 [1, 2, 1, 3, 1, 4].existsOne(i, v, i == 1 || v == 1) // returns false
875 [1, 1, 2, 2, 3, 3].existsOne(i, v, i == 2 && v == 2) // returns true
876 {'i': 0, 'j': 1, 'k': 2}.existsOne(i, v, i == 'l' || v == 1) // returns true
877
878 ### TransformList
879
880 Comprehension which converts a map or a list into a list value. The output
881 expression of the comprehension determines the contents of the output list.
882 Elements in the list may optionally be filtered according to a predicate
883 expression, where elements that satisfy the predicate are transformed.
884
885 <list>.transformList(indexVar, valueVar, <transform>)
886 <list>.transformList(indexVar, valueVar, <filter>, <transform>)
887 <map>.transformList(keyVar, valueVar, <transform>)
888 <map>.transformList(keyVar, valueVar, <filter>, <transform>)
889
890 Examples:
891
892 [1, 2, 3].transformList(indexVar, valueVar,
893 (indexVar * valueVar) + valueVar) // returns [1, 4, 9]
894 [1, 2, 3].transformList(indexVar, valueVar, indexVar % 2 == 0
895 (indexVar * valueVar) + valueVar) // returns [1, 9]
896 {'greeting': 'hello', 'farewell': 'goodbye'}
897 .transformList(k, _, k) // returns ['greeting', 'farewell']
898 {'greeting': 'hello', 'farewell': 'goodbye'}
899 .transformList(_, v, v) // returns ['hello', 'goodbye']
900
901 ### TransformMap
902
903 Comprehension which converts a map or a list into a map value. The output
904 expression of the comprehension determines the value of the output map entry;
905 however, the key remains fixed. Elements in the map may optionally be filtered
906 according to a predicate expression, where elements that satisfy the predicate
907 are transformed.
908
909 <list>.transformMap(indexVar, valueVar, <transform>)
910 <list>.transformMap(indexVar, valueVar, <filter>, <transform>)
911 <map>.transformMap(keyVar, valueVar, <transform>)
912 <map>.transformMap(keyVar, valueVar, <filter>, <transform>)
913
914 Examples:
915
916 [1, 2, 3].transformMap(indexVar, valueVar,
917 (indexVar * valueVar) + valueVar) // returns {0: 1, 1: 4, 2: 9}
918 [1, 2, 3].transformMap(indexVar, valueVar, indexVar % 2 == 0
919 (indexVar * valueVar) + valueVar) // returns {0: 1, 2: 9}
920 {'greeting': 'hello'}.transformMap(k, v, v + '!') // returns {'greeting': 'hello!'}
921
922 ### TransformMapEntry
923
924 Comprehension which converts a map or a list into a map value; however, this
925 transform expects the entry expression be a map literal. If the transform
926 produces an entry which duplicates a key in the target map, the comprehension
927 will error. Note, that key equality is determined using CEL equality which
928 asserts that numeric values which are equal, even if they don't have the same
929 type will cause a key collision.
930
931 Elements in the map may optionally be filtered according to a predicate
932 expression, where elements that satisfy the predicate are transformed.
933
934 <list>.transformMapEntry(indexVar, valueVar, <transform>)
935 <list>.transformMapEntry(indexVar, valueVar, <filter>, <transform>)
936 <map>.transformMapEntry(keyVar, valueVar, <transform>)
937 <map>.transformMapEntry(keyVar, valueVar, <filter>, <transform>)
938
939 Examples:
940
941 // returns {'hello': 'greeting'}
942 {'greeting': 'hello'}.transformMapEntry(keyVar, valueVar, {valueVar: keyVar})
943 // reverse lookup, require all values in list be unique
944 [1, 2, 3].transformMapEntry(indexVar, valueVar, {valueVar: indexVar})
945
946 {'greeting': 'aloha', 'farewell': 'aloha'}
947 .transformMapEntry(keyVar, valueVar, {valueVar: keyVar}) // error, duplicate key
948
949 ## Regex
950
951 Regex introduces functions for regular expressions in CEL.
952
953 Note: Please ensure that the cel.OptionalTypes() is enabled when using regex
954 extensions. All functions use the 'regex' namespace. If you are currently
955 using a variable named 'regex', the functions will likely work as intended.
956 However, there is some chance for collision.
957
958 ### Replace
959
960 The `regex.replace` function replaces all non-overlapping substring of a regex
961 pattern in the target string with a replacement string. Optionally, you can
962 limit the number of replacements by providing a count argument. When the count
963 is a negative number, the function acts as replace all. Only numeric (\N)
964 capture group references are supported in the replacement string, with
965 validation for correctness. Backslashed-escaped digits (\1 to \9) within the
966 replacement argument can be used to insert text matching the corresponding
967 parenthesized group in the regexp pattern. An error will be thrown for invalid
968 regex or replace string.
969
970
971 regex.replace(target: string, pattern: string, replacement: string) -> string
972 regex.replace(target: string, pattern: string, replacement: string, count: int) -> string
973
974
975 Examples:
976
977 regex.replace('hello world hello', 'hello', 'hi') == 'hi world hi'
978 regex.replace('banana', 'a', 'x', 0) == 'banana'
979 regex.replace('banana', 'a', 'x', 1) == 'bxnana'
980 regex.replace('banana', 'a', 'x', 2) == 'bxnxna'
981 regex.replace('banana', 'a', 'x', -12) == 'bxnxnx'
982 regex.replace('foo bar', '(fo)o (ba)r', '\\2 \\1') == 'ba fo'
983
984 regex.replace('test', '(.)', '$2') \\ Runtime Error invalid replace string
985 regex.replace('foo bar', '(', '$2 $1') \\ Runtime Error invalid regex string
986 regex.replace('id=123', 'id=(?P<value>\\\\d+)', 'value: \\values') \\ Runtime Error invalid replace string
987
988 ### Extract
989
990 The `regex.extract` function returns the first match of a regex pattern as an
991 `optional` string. If no match is found, it returns an optional none value.
992 An error will be thrown for invalid regex or for multiple capture groups.
993
994 regex.extract(target: string, pattern: string) -> optional<string>
995
996 Examples:
997
998 regex.extract('hello world', 'hello(.*)') == optional.of(' world')
999 regex.extract('item-A, item-B', 'item-(\\w+)') == optional.of('A')
1000 regex.extract('HELLO', 'hello') == optional.none()
1001
1002 regex.extract('testuser@testdomain', '(.*)@([^.]*)')) \\ Runtime Error multiple extract group
1003
1004 ### Extract All
1005
1006 The `regex.extractAll` function returns a `list` of all matches of a regex
1007 pattern in a target string. If no matches are found, it returns an empty list.
1008 An error will be thrown for invalid regex or for multiple capture groups.
1009
1010 regex.extractAll(target: string, pattern: string) -> list<string>
1011
1012 Examples:
1013
1014 regex.extractAll('id:123, id:456', 'id:\\d+') == ['id:123', 'id:456']
1015 regex.extractAll('id:123, id:456', 'assa') == []
1016
1017 regex.extractAll('testuser@testdomain', '(.*)@([^.]*)') \\ Runtime Error multiple capture group