Documentation
¶
Overview ¶
Package celext copies cel-go's Strings, Encoders, Lists, Math, and Sets extension libraries. It preserves upstream behavior, including cost models, by default. Callers that never estimate cost can use StringsWithoutCostModels, EncodersWithoutCostModels, ListsWithoutCostModels, MathWithoutCostModels, and SetsWithoutCostModels to avoid compiling overload cost models on every environment configuration.
Regenerate with make celext after changing the cel-go dependency. Remove this copy when cel-go compiles cost models once per model set.
Package ext contains CEL extension libraries where each library defines a related set of constants, functions, macros, or other configuration settings which may not be covered by the core CEL spec.
Index ¶
- Constants
- func Base64Decode(str string) ([]byte, error)
- func Base64Encode(bytes []byte) string
- func Encoders(options ...EncodersOption) cel.EnvOption
- func FormatString(arg ref.Val, locale string) (string, error)
- func JSONEncode(val ref.Val) (string, error)
- func JSONParse(adapter types.Adapter, str string) (ref.Val, error)
- func JSONParseWithType(adapter types.Adapter, provider types.Provider, str string, ...) (ref.Val, error)
- func Lists(options ...ListsOption) cel.EnvOption
- func Math(options ...MathOption) cel.EnvOption
- func NewSetMembershipOptimizer() (cel.ASTOptimizer, error)
- func Sets(options ...SetsOption) cel.EnvOption
- func Strings(options ...StringsOption) cel.EnvOption
- func YAMLEncode(val ref.Val) (string, error)
- func YAMLParse(adapter types.Adapter, str string) (ref.Val, error)
- func YAMLParseWithType(adapter types.Adapter, provider types.Provider, str string, ...) (ref.Val, error)
- type EncodersOption
- type ListsOption
- type MathOption
- type SetsOption
- type StringsOption
Constants ¶
const SourceVersion = "v0.33.2"
SourceVersion is the cel-go version used to generate this package.
Variables ¶
This section is empty.
Functions ¶
func Base64Decode ¶
Base64Decode decodes a base64 encoded string into a byte slice.
func Base64Encode ¶
Base64Encode encodes a byte slice into a standard base64 string.
func Encoders ¶
func Encoders(options ...EncodersOption) cel.EnvOption
Encoders returns a cel.EnvOption to configure extended functions for string, byte, and object encodings.
Note: all functions use the 'base64', 'json', and 'yaml' namespaces.
Version 2 of this library depends on the CEL optional type. Please ensure that the cel.OptionalTypes() is enabled when using encoder extensions at version 2 or greater.
Base64.Decode ¶
Decodes base64-encoded string to bytes.
This function will return an error if the string input is not base64-encoded.
base64.decode(<string>) -> <bytes>
Examples:
base64.decode('aGVsbG8=') // return b'hello'
base64.decode('aGVsbG8') // return b'hello'
Base64.DecodeUrl ¶
Introduced at version: 2
Decodes a base64url-encoded string to bytes.
This function will return an error if the string input is not base64url-encoded.
base64.decodeUrl(<string>) -> <bytes>
Examples:
base64.decodeUrl('aGVsbG8=') // return b'hello'
base64.decodeUrl('aGVsbG8') // return b'hello'
base64.decodeUrl('____') // return b'\xff\xff\xff'
Base64.Encode ¶
Encodes bytes to a base64-encoded string.
base64.encode(<bytes>) -> <string>
Examples:
base64.encode(b'hello') // return b'aGVsbG8='
Base64.EncodeUrl ¶
Introduced at version: 2
Encodes bytes to a base64url-encoded string.
base64.encodeUrl(<bytes>) -> <string>
Examples:
base64.encodeUrl(b'hello') // return 'aGVsbG8=' base64.encodeUrl(b'\xff\xff\xff') // return '____'
JSON.Encode ¶
Introduced at version: 1
Encodes a CEL value to a JSON string.
json.encode(<dyn>) -> <string>
Examples:
json.encode({'hello': 'world'}) // return '{"hello":"world"}'
JSON.Parse ¶
Introduced at version: 2
Parses a JSON string to a CEL value or a specific type.
json.parse(<string>) -> <optional_type(dyn)> json.parse(<string>, <type(T)>) -> <optional_type(T)>
Examples:
json.parse('{"hello":"world"}') // return optional.of({'hello': 'world'})
json.parse('123', int) // return optional.of(123)
YAML.Encode ¶
Introduced at version: 2
Encodes a CEL value to a YAML string.
yaml.encode(<dyn>) -> <string>
Examples:
yaml.encode({'hello': 'world'}) // return 'hello: world\n'
YAML.Parse ¶
Introduced at version: 2
Parses a YAML string to a CEL value or a specific type.
yaml.parse(<string>) -> <optional_type(dyn)> yaml.parse(<string>, <type(T)>) -> <optional_type(T)>
Examples:
yaml.parse('hello: world') // return optional.of({'hello': 'world'})
yaml.parse('123', int) // return optional.of(123)
func FormatString ¶
FormatString returns the string representation of a CEL value.
It is used to implement the %s specifier in the (string).format() extension function.
func JSONEncode ¶
JSONEncode encodes a CEL ref.Val to its JSON string representation.
func JSONParse ¶
JSONParse parses a JSON string into a dynamic CEL ref.Val using the given type adapter. If adapter is nil, types.DefaultTypeAdapter is used.
func JSONParseWithType ¶
func JSONParseWithType(adapter types.Adapter, provider types.Provider, str string, targetType ref.Type) (ref.Val, error)
JSONParseWithType parses a JSON string into a typed CEL ref.Val conforming to targetType. If adapter is nil, types.DefaultTypeAdapter is used. If provider implements types.Adapter, it will be used as the adapter.
func Lists ¶
func Lists(options ...ListsOption) cel.EnvOption
Lists returns a cel.EnvOption to configure extended functions for list manipulation. As a general note, all indices are zero-based.
Distinct ¶
Introduced in version: 2 (cost support in version 3)
Returns the distinct elements of a list.
<list(T)>.distinct() -> <list(T)>
Examples:
[1, 2, 2, 3, 3, 3].distinct() // return [1, 2, 3] ["b", "b", "c", "a", "c"].distinct() // return ["b", "c", "a"] [1, "b", 2, "b"].distinct() // return [1, "b", 2]
Range ¶
Introduced in version: 2 (cost support in version 3)
Returns a list of integers from 0 to n-1.
lists.range(<int>) -> <list(int)>
Examples:
lists.range(5) -> [0, 1, 2, 3, 4]
Reverse ¶
Introduced in version: 2 (cost support in version 3)
Returns the elements of a list in reverse order.
<list(T)>.reverse() -> <list(T)>
Examples:
[5, 3, 1, 2].reverse() // return [2, 1, 3, 5]
Slice ¶
Introduced in version: 0 (cost support in version 3)
Returns a new sub-list using the indexes provided.
<list>.slice(<int>, <int>) -> <list>
Examples:
[1,2,3,4].slice(1, 3) // return [2, 3] [1,2,3,4].slice(2, 4) // return [3 ,4]
Flatten ¶
Introduced in version: 1 (cost support in version 3)
Flattens a list recursively. If an optional depth is provided, the list is flattened to a the specified level. A negative depth value will result in an error.
<list>.flatten() -> <list> <list>.flatten(<int>) -> <list>
Examples:
[1,[2,3],[4]].flatten() // return [1, 2, 3, 4] [1,[2,[3,4]]].flatten() // return [1, 2, [3, 4]] [1,2,[],[],[3,4]].flatten() // return [1, 2, 3, 4] [1,[2,[3,[4]]]].flatten(2) // return [1, 2, 3, [4]] [1,[2,[3,[4]]]].flatten(-1) // error
Sort ¶
Introduced in version: 2 (cost support in version 3)
Sorts a list with comparable elements. If the element type is not comparable or the element types are not the same, the function will produce an error.
<list(T)>.sort() -> <list(T)>
T in {int, uint, double, bool, duration, timestamp, string, bytes}
Examples:
[3, 2, 1].sort() // return [1, 2, 3] ["b", "c", "a"].sort() // return ["a", "b", "c"] [1, "b"].sort() // error [[1, 2, 3]].sort() // error
SortBy ¶
Introduced in version: 2 (cost support in version 3)
Sorts a list by a key value, i.e., the order is determined by the result of an expression applied to each element of the list. The output of the key expression must be a comparable type, otherwise the function will return an error.
<list(T)>.sortBy(<bindingName>, <keyExpr>) -> <list(T)>
keyExpr returns a value in {int, uint, double, bool, duration, timestamp, string, bytes}
Examples:
[
Player { name: "foo", score: 0 },
Player { name: "bar", score: -10 },
Player { name: "baz", score: 1000 },
].sortBy(e, e.score).map(e, e.name)
== ["bar", "foo", "baz"]
HasOnly ¶
Introduced in version: 5
Returns whether every element of the target list is contained within the argument list, i.e. whether the target is a subset of the argument. Standard CEL equality is used to determine whether two elements are equal, and neither list is required to contain unique elements. An empty target list always returns true.
<list(T)>.hasOnly(<list(T)>) -> <bool>
Examples:
[].hasOnly([1, 2]) // return true [1, 1, 2].hasOnly([1, 2, 3]) // return true [1, 2.0, 3u].hasOnly([1.0, 2u, 3]) // return true [1, 4].hasOnly([1, 2, 3]) // return false [1].hasOnly([]) // return false
HasAny ¶
Introduced in version: 5
Returns whether the target list has at least one element which is equal to an element of the argument list. If either list is empty, the result is false.
<list(T)>.hasAny(<list(T)>) -> <bool>
Examples:
[1, 2, 3].hasAny([3, 4]) // return true [[1], [2, 3]].hasAny([[1, 2], [2, 3.0]]) // return true [1, 2].hasAny([3, 4]) // return false [1].hasAny([]) // return false
HasAll ¶
Introduced in version: 5
Returns whether every element of the argument list is contained within the target list, i.e. whether the target is a superset of the argument. An empty argument list always returns true.
<list(T)>.hasAll(<list(T)>) -> <bool>
Examples:
[1, 2, 3].hasAll([]) // return true [1, 2, 3, 4].hasAll([2, 3]) // return true [1, 2.0, 3u].hasAll([1.0, 2u, 3]) // return true [1, 2, 3].hasAll([2, 4]) // return false
HasExactly ¶
Introduced in version: 5
Returns whether the target and argument lists are set equivalent, i.e. every element of the target list is equal to an element of the argument list and vice versa. The lists may differ in size since neither is guaranteed to hold unique elements, so size does not factor into the computation.
<list(T)>.hasExactly(<list(T)>) -> <bool>
Examples:
[].hasExactly([]) // return true [1].hasExactly([1, 1]) // return true [1].hasExactly([1u, 1.0]) // return true [1, 2, 3].hasExactly([3u, 2.0, 1]) // return true [1, 2].hasExactly([1, 2, 3]) // return false
func Math ¶
func Math(options ...MathOption) cel.EnvOption
Math returns a cel.EnvOption to configure namespaced math helper macros and functions.
Note, all macros use the 'math' namespace; however, at the time of macro expansion the namespace looks just like any other identifier. If you are currently using a variable named 'math', the macro will likely work just as intended; however, there is some chance for collision.
Math.Greatest ¶
Returns the greatest valued number present in the arguments to the macro.
Greatest is a variable argument count macro which must take at least one argument. Simple numeric and list literals are supported as valid argument types; however, other literals will be flagged as errors during macro expansion. If the argument expression does not resolve to a numeric or list(numeric) type during type-checking, or during runtime then an error will be produced. If a list argument is empty, this too will produce an error.
math.greatest(<arg>, ...) -> <double|int|uint>
Examples:
math.greatest(1) // 1
math.greatest(1u, 2u) // 2u
math.greatest(-42.0, -21.5, -100.0) // -21.5
math.greatest([-42.0, -21.5, -100.0]) // -21.5
math.greatest(numbers) // numbers must be list(numeric)
math.greatest() // parse error
math.greatest('string') // parse error
math.greatest(a, b) // check-time error if a or b is non-numeric
math.greatest(dyn('string')) // runtime error
Math.Least ¶
Returns the least valued number present in the arguments to the macro.
Least is a variable argument count macro which must take at least one argument. Simple numeric and list literals are supported as valid argument types; however, other literals will be flagged as errors during macro expansion. If the argument expression does not resolve to a numeric or list(numeric) type during type-checking, or during runtime then an error will be produced. If a list argument is empty, this too will produce an error.
math.least(<arg>, ...) -> <double|int|uint>
Examples:
math.least(1) // 1
math.least(1u, 2u) // 1u
math.least(-42.0, -21.5, -100.0) // -100.0
math.least([-42.0, -21.5, -100.0]) // -100.0
math.least(numbers) // numbers must be list(numeric)
math.least() // parse error
math.least('string') // parse error
math.least(a, b) // check-time error if a or b is non-numeric
math.least(dyn('string')) // runtime error
Math.BitOr ¶
Introduced at version: 1
Performs a bitwise-OR operation over two int or uint values.
math.bitOr(<int>, <int>) -> <int> math.bitOr(<uint>, <uint>) -> <uint>
Examples:
math.bitOr(1u, 2u) // returns 3u math.bitOr(-2, -4) // returns -2
Math.BitAnd ¶
Introduced at version: 1
Performs a bitwise-AND operation over two int or uint values.
math.bitAnd(<int>, <int>) -> <int> math.bitAnd(<uint>, <uint>) -> <uint>
Examples:
math.bitAnd(3u, 2u) // return 2u math.bitAnd(3, 5) // returns 3 math.bitAnd(-3, -5) // returns -7
Math.BitXor ¶
Introduced at version: 1
math.bitXor(<int>, <int>) -> <int> math.bitXor(<uint>, <uint>) -> <uint>
Performs a bitwise-XOR operation over two int or uint values.
Examples:
math.bitXor(3u, 5u) // returns 6u math.bitXor(1, 3) // returns 2
Math.BitNot ¶
Introduced at version: 1
Function which accepts a single int or uint and performs a bitwise-NOT ones-complement of the given binary value.
math.bitNot(<int>) -> <int> math.bitNot(<uint>) -> <uint>
Examples
math.bitNot(1) // returns -1 math.bitNot(-1) // return 0 math.bitNot(0u) // returns 18446744073709551615u
Math.BitShiftLeft ¶
Introduced at version: 1
Perform a left shift of bits on the first parameter, by the amount of bits specified in the second parameter. The first parameter is either a uint or an int. The second parameter must be an int.
When the second parameter is 64 or greater, 0 will be always be returned since the number of bits shifted is greater than or equal to the total bit length of the number being shifted. Negative valued bit shifts will result in a runtime error.
math.bitShiftLeft(<int>, <int>) -> <int> math.bitShiftLeft(<uint>, <int>) -> <uint>
Examples
math.bitShiftLeft(1, 2) // returns 4 math.bitShiftLeft(-1, 2) // returns -4 math.bitShiftLeft(1u, 2) // return 4u math.bitShiftLeft(1u, 200) // returns 0u
Math.BitShiftRight ¶
Introduced at version: 1
Perform a right shift of bits on the first parameter, by the amount of bits specified in the second parameter. The first parameter is either a uint or an int. The second parameter must be an int.
When the second parameter is 64 or greater, 0 will always be returned since the number of bits shifted is greater than or equal to the total bit length of the number being shifted. Negative valued bit shifts will result in a runtime error.
The sign bit extension will not be preserved for this operation: vacant bits on the left are filled with 0.
math.bitShiftRight(<int>, <int>) -> <int> math.bitShiftRight(<uint>, <int>) -> <uint>
Examples
math.bitShiftRight(1024, 2) // returns 256 math.bitShiftRight(1024u, 2) // returns 256u math.bitShiftRight(1024u, 64) // returns 0u
Math.Ceil ¶
Introduced at version: 1
Compute the ceiling of a double value.
math.ceil(<double>) -> <double>
Examples:
math.ceil(1.2) // returns 2.0 math.ceil(-1.2) // returns -1.0
Math.Floor ¶
Introduced at version: 1
Compute the floor of a double value.
math.floor(<double>) -> <double>
Examples:
math.floor(1.2) // returns 1.0 math.floor(-1.2) // returns -2.0
Math.Round ¶
Introduced at version: 1
Rounds the double value to the nearest whole number with ties rounding away from zero, e.g. 1.5 -> 2.0, -1.5 -> -2.0.
math.round(<double>) -> <double>
Examples:
math.round(1.2) // returns 1.0 math.round(1.5) // returns 2.0 math.round(-1.5) // returns -2.0
Math.Trunc ¶
Introduced at version: 1
Truncates the fractional portion of the double value.
math.trunc(<double>) -> <double>
Examples:
math.trunc(-1.3) // returns -1.0 math.trunc(1.3) // returns 1.0
Math.Abs ¶
Introduced at version: 1
Returns the absolute value of the numeric type provided as input. If the value is NaN, the output is NaN. If the input is int64 min, the function will result in an overflow error.
math.abs(<double>) -> <double> math.abs(<int>) -> <int> math.abs(<uint>) -> <uint>
Examples:
math.abs(-1) // returns 1 math.abs(1) // returns 1 math.abs(-9223372036854775808) // overflow error
Math.Sign ¶
Introduced at version: 1
Returns the sign of the numeric type, either -1, 0, 1 as an int, double, or uint depending on the overload. For floating point values, if NaN is provided as input, the output is also NaN. The implementation does not differentiate between positive and negative zero.
math.sign(<double>) -> <double> math.sign(<int>) -> <int> math.sign(<uint>) -> <uint>
Examples:
math.sign(-42) // returns -1 math.sign(0) // returns 0 math.sign(42) // returns 1
Math.IsInf ¶
Introduced at version: 1
Returns true if the input double value is -Inf or +Inf.
math.isInf(<double>) -> <bool>
Examples:
math.isInf(1.0/0.0) // returns true math.isInf(1.2) // returns false
Math.IsNaN ¶
Introduced at version: 1
Returns true if the input double value is NaN, false otherwise.
math.isNaN(<double>) -> <bool>
Examples:
math.isNaN(0.0/0.0) // returns true math.isNaN(1.2) // returns false
Math.IsFinite ¶
Introduced at version: 1
Returns true if the value is a finite number. Equivalent in behavior to: !math.isNaN(double) && !math.isInf(double)
math.isFinite(<double>) -> <bool>
Examples:
math.isFinite(0.0/0.0) // returns false math.isFinite(1.2) // returns true
Math.Sqrt ¶
Introduced at version: 2
Returns the square root of the given input as double Throws error for negative or non-numeric inputs
math.sqrt(<double>) -> <double> math.sqrt(<int>) -> <double> math.sqrt(<uint>) -> <double>
Examples:
math.sqrt(81) // returns 9.0
math.sqrt(985.25) // returns 31.388692231439016
math.sqrt(-15) // returns NaN
func NewSetMembershipOptimizer ¶
func NewSetMembershipOptimizer() (cel.ASTOptimizer, error)
NewSetMembershipOptimizer rewrites set membership tests using the `in` operator against a list of constant values of enum, int, uint, string, or boolean type into a set membership test against a map where the map keys are the elements of the list.
func Sets ¶
func Sets(options ...SetsOption) cel.EnvOption
Sets returns a cel.EnvOption to configure namespaced set relationship functions.
There is no set type within CEL, and while one may be introduced in the future, there are cases where a `list` type is known to behave like a set. For such cases, this library provides some basic functionality for determining set containment, equivalence, and intersection.
Sets.Contains ¶
Returns whether the first list argument contains all elements in the second list argument. The list may contain elements of any type and standard CEL equality is used to determine whether a value exists in both lists. If the second list is empty, the result will always return true.
sets.contains(list(T), list(T)) -> bool
Examples:
sets.contains([], []) // true sets.contains([], [1]) // false sets.contains([1, 2, 3, 4], [2, 3]) // true sets.contains([1, 2.0, 3u], [1.0, 2u, 3]) // true
Sets.Equivalent ¶
Returns whether the first and second list are set equivalent. Lists are set equivalent if for every item in the first list, there is an element in the second which is equal. The lists may not be of the same size as they do not guarantee the elements within them are unique, so size does not factor into the computation.
Examples:
sets.equivalent([], []) // true sets.equivalent([1], [1, 1]) // true sets.equivalent([1], [1u, 1.0]) // true sets.equivalent([1, 2, 3], [3u, 2.0, 1]) // true
Sets.Intersects ¶
Returns whether the first list has at least one element whose value is equal to an element in the second list. If either list is empty, the result will be false.
Examples:
sets.intersects([1], []) // false sets.intersects([1], [1, 2]) // true sets.intersects([[1], [2, 3]], [[1, 2], [2, 3.0]]) // true
func Strings ¶
func Strings(options ...StringsOption) cel.EnvOption
Strings returns a cel.EnvOption to configure extended functions for string manipulation. As a general note, all indices are zero-based.
CharAt ¶
Introduced at version: 0 (cost support in version 5)
Returns the character at the given position. If the position is negative, or greater than the length of the string, the function will produce an error:
<string>.charAt(<int>) -> <string>
Examples:
'hello'.charAt(4) // return 'o' 'hello'.charAt(5) // return '' 'hello'.charAt(-1) // error
Format ¶
Introduced at version: 1 (cost at version 5)
Returns a new string with substitutions being performed, printf-style. The valid formatting clauses are:
`%s` - substitutes a string. This can also be used on bools, lists, maps, bytes, Duration and Timestamp, in addition to all numerical types (int, uint, and double). Note that the dot/period decimal separator will always be used when printing a list or map that contains a double, and that null can be passed (which results in the string "null") in addition to types. `%d` - substitutes an integer. `%f` - substitutes a double with fixed-point precision. The default precision is 6, but this can be adjusted. The strings `Infinity`, `-Infinity`, and `NaN` are also valid input for this clause. `%e` - substitutes a double in scientific notation. The default precision is 6, but this can be adjusted. `%b` - substitutes an integer with its equivalent binary string. Can also be used on bools. `%x` - substitutes an integer with its equivalent in hexadecimal, or if given a string or bytes, will output each character's equivalent in hexadecimal. `%X` - same as above, but with A-F capitalized. `%o` - substitutes an integer with its equivalent in octal.
<string>.format(<list>) -> <string>
Examples:
"this is a string: %s\nand an integer: %d".format(["str", 42]) // returns "this is a string: str\nand an integer: 42"
"a double substituted with %%s: %s".format([64.2]) // returns "a double substituted with %s: 64.2"
"string type: %s".format([type(string)]) // returns "string type: string"
"timestamp: %s".format([timestamp("2023-02-03T23:31:20+00:00")]) // returns "timestamp: 2023-02-03T23:31:20Z"
"duration: %s".format([duration("1h45m47s")]) // returns "duration: 6347s"
"%f".format([3.14]) // returns "3.140000"
"scientific notation: %e".format([2.71828]) // returns "scientific notation: 2.718280\u202f\u00d7\u202f10\u2070\u2070"
"5 in binary: %b".format([5]), // returns "5 in binary; 101"
"26 in hex: %x".format([26]), // returns "26 in hex: 1a"
"26 in hex (uppercase): %X".format([26]) // returns "26 in hex (uppercase): 1A"
"30 in octal: %o".format([30]) // returns "30 in octal: 36"
"a map inside a list: %s".format([[1, 2, 3, {"a": "x", "b": "y", "c": "z"}]]) // returns "a map inside a list: [1, 2, 3, {"a":"x", "b":"y", "c":"d"}]"
"true bool: %s - false bool: %s\nbinary bool: %b".format([true, false, true]) // returns "true bool: true - false bool: false\nbinary bool: 1"
Passing an incorrect type (a string to `%b`) is considered an error, as well as attempting to use more formatting clauses than there are arguments (`%d %d %d` while passing two ints, for instance). If compile-time checking is enabled, and the formatting string is a constant, and the argument list is a literal, then letting any arguments go unused/unformatted is also considered an error.
IndexOf ¶
Introduced at version: 0 (cost support in version 5)
Returns the integer index of the first occurrence of the search string. If the search string is not found the function returns -1.
The function also accepts an optional position from which to begin the substring search. If the substring is the empty string, the index where the search starts is returned (zero or custom).
<string>.indexOf(<string>) -> <int> <string>.indexOf(<string>, <int>) -> <int>
Examples:
'hello mellow'.indexOf('') // returns 0
'hello mellow'.indexOf('ello') // returns 1
'hello mellow'.indexOf('jello') // returns -1
'hello mellow'.indexOf('', 2) // returns 2
'hello mellow'.indexOf('ello', 2) // returns 7
'hello mellow'.indexOf('ello', 20) // returns -1
'hello mellow'.indexOf('ello', -1) // error
Join ¶
Introduced at version: 0 (cost support in version 5)
Returns a new string where the elements of string list are concatenated.
The function also accepts an optional separator which is placed between elements in the resulting string.
<list<string>>.join() -> <string> <list<string>>.join(<string>) -> <string>
Examples:
['hello', 'mellow'].join() // returns 'hellomellow'
['hello', 'mellow'].join(' ') // returns 'hello mellow'
[].join() // returns ''
[].join('/') // returns ''
LastIndexOf ¶
Introduced at version: 0 (cost support in version 5)
Returns the integer index at the start of the last occurrence of the search string. If the search string is not found the function returns -1.
The function also accepts an optional position which represents the last index to be considered as the beginning of the substring match. If the substring is the empty string, the index where the search starts is returned (string length or custom).
<string>.lastIndexOf(<string>) -> <int> <string>.lastIndexOf(<string>, <int>) -> <int>
Examples:
'hello mellow'.lastIndexOf('') // returns 12
'hello mellow'.lastIndexOf('ello') // returns 7
'hello mellow'.lastIndexOf('jello') // returns -1
'hello mellow'.lastIndexOf('ello', 6) // returns 1
'hello mellow'.lastIndexOf('ello', 20) // returns -1
'hello mellow'.lastIndexOf('ello', -1) // error
LowerAscii ¶
Introduced at version: 0 (cost support in version 5)
Returns a new string where all ASCII characters are lower-cased.
This function does not perform Unicode case-mapping for characters outside the ASCII range.
<string>.lowerAscii() -> <string>
Examples:
'TacoCat'.lowerAscii() // returns 'tacocat' 'TacoCÆt Xii'.lowerAscii() // returns 'tacocÆt xii'
Strings.Quote ¶
Introduced in version: 1 (cost support in version 5)
Takes the given string and makes it safe to print (without any formatting due to escape sequences). If any invalid UTF-8 characters are encountered, they are replaced with \uFFFD.
strings.quote(<string>)
Examples:
strings.quote('single-quote with "double quote"') // returns '"single-quote with \"double quote\""' strings.quote("two escape sequences \a\n") // returns '"two escape sequences \\a\\n"'
Replace ¶
Introduced at version: 0 (cost support in version 5)
Returns a new string based on the target, which replaces the occurrences of a search string with a replacement string if present. The function accepts an optional limit on the number of substring replacements to be made.
When the replacement limit is 0, the result is the original string. When the limit is a negative number, the function behaves the same as replace all.
<string>.replace(<string>, <string>) -> <string> <string>.replace(<string>, <string>, <int>) -> <string>
Examples:
'hello hello'.replace('he', 'we') // returns 'wello wello'
'hello hello'.replace('he', 'we', -1) // returns 'wello wello'
'hello hello'.replace('he', 'we', 1) // returns 'wello hello'
'hello hello'.replace('he', 'we', 0) // returns 'hello hello'
'hello hello'.replace('', '_') // returns '_h_e_l_l_o_ _h_e_l_l_o_'
'hello hello'.replace('h', '') // returns 'ello ello'
Split ¶
Introduced at version: 0 (cost support in version 5)
Returns a list of strings split from the input by the given separator. The function accepts an optional argument specifying a limit on the number of substrings produced by the split.
When the split limit is 0, the result is an empty list. When the limit is 1, the result is the target string to split. When the limit is a negative number, the function behaves the same as split all.
<string>.split(<string>) -> <list<string>> <string>.split(<string>, <int>) -> <list<string>>
Examples:
'hello hello hello'.split(' ') // returns ['hello', 'hello', 'hello']
'hello hello hello'.split(' ', 0) // returns []
'hello hello hello'.split(' ', 1) // returns ['hello hello hello']
'hello hello hello'.split(' ', 2) // returns ['hello', 'hello hello']
'hello hello hello'.split(' ', -1) // returns ['hello', 'hello', 'hello']
Substring ¶
Introduced at version: 0 (cost support in version 5)
Returns the substring given a numeric range corresponding to character positions. Optionally may omit the trailing range for a substring from a given character position until the end of a string.
Character offsets are 0-based with an inclusive start range and exclusive end range. It is an error to specify an end range that is lower than the start range, or for either the start or end index to be negative or exceed the string length.
<string>.substring(<int>) -> <string> <string>.substring(<int>, <int>) -> <string>
Examples:
'tacocat'.substring(4) // returns 'cat' 'tacocat'.substring(0, 4) // returns 'taco' 'tacocat'.substring(-1) // error 'tacocat'.substring(2, 1) // error
Trim ¶
Introduced at version: 0 (cost support in version 5)
Returns a new string which removes the leading and trailing whitespace in the target string. The trim function uses the Unicode definition of whitespace which does not include the zero-width spaces. See: https://en.wikipedia.org/wiki/Whitespace_character#Unicode
<string>.trim() -> <string>
Examples:
' \ttrim\n '.trim() // returns 'trim'
UpperAscii ¶
Introduced at version: 0 (cost support in version 5)
Returns a new string where all ASCII characters are upper-cased.
This function does not perform Unicode case-mapping for characters outside the ASCII range.
<string>.upperAscii() -> <string>
Examples:
'TacoCat'.upperAscii() // returns 'TACOCAT' 'TacoCÆt Xii'.upperAscii() // returns 'TACOCÆT XII'
Reverse ¶
Introduced at version: 3 (cost support in version 5)
Returns a new string whose characters are the same as the target string, only formatted in reverse order. This function relies on converting strings to rune arrays in order to reverse
<string>.reverse() -> <string>
Examples:
'gums'.reverse() // returns 'smug' 'John Smith'.reverse() // returns 'htimS nhoJ'
Introduced at version: 4 (cost support in version 5)
Formatting updated to adhere to https://github.com/google/cel-spec/blob/master/doc/extensions/strings.md.
<string>.format(<list>) -> <string>
func YAMLEncode ¶
YAMLEncode encodes a CEL ref.Val to its YAML string representation.
func YAMLParse ¶
YAMLParse parses a YAML string into a dynamic CEL ref.Val using the given type adapter. If adapter is nil, types.DefaultTypeAdapter is used.
func YAMLParseWithType ¶
func YAMLParseWithType(adapter types.Adapter, provider types.Provider, str string, targetType ref.Type) (ref.Val, error)
YAMLParseWithType parses a YAML string into a typed CEL ref.Val conforming to targetType. If adapter is nil, types.DefaultTypeAdapter is used. If provider implements types.Adapter, it will be used as the adapter.
Types ¶
type EncodersOption ¶
type EncodersOption func(*encoderLib) *encoderLib
EncodersOption declares a functional operator for configuring encoder extensions.
func EncodersVersion ¶
func EncodersVersion(version uint32) EncodersOption
EncodersVersion sets the library version for encoder extensions.
func EncodersWithoutCostModels ¶
func EncodersWithoutCostModels() EncodersOption
EncodersWithoutCostModels disables overload cost models for Encoders.
type ListsOption ¶
type ListsOption func(*listsLib) *listsLib
ListsOption is a functional interface for configuring the strings library.
func ListsMaxRangeSize ¶
func ListsMaxRangeSize(size int64) ListsOption
ListsMaxRangeSize sets the maximum number of elements lists.range() will allocate. If not set, the default is 1,000,000. Setting this to zero disables the limit (not recommended).
func ListsVersion ¶
func ListsVersion(version uint32) ListsOption
ListsVersion configures the version of the string library.
The version limits which functions are available. Only functions introduced below or equal to the given version included in the library. If this option is not set, all functions are available.
See the library documentation to determine which version a function was introduced. If the documentation does not state which version a function was introduced, it can be assumed to be introduced at version 0, when the library was first created.
func ListsWithoutCostModels ¶
func ListsWithoutCostModels() ListsOption
ListsWithoutCostModels disables overload cost models for Lists.
type MathOption ¶
type MathOption func(*mathLib) *mathLib
MathOption declares a functional operator for configuring math extensions.
func MathVersion ¶
func MathVersion(version uint32) MathOption
MathVersion sets the library version for math extensions.
func MathWithoutCostModels ¶
func MathWithoutCostModels() MathOption
MathWithoutCostModels disables overload cost models for Math.
type SetsOption ¶
type SetsOption func(*setsLib) *setsLib
SetsOption declares a functional operator for configuring set extensions.
func SetsVersion ¶
func SetsVersion(version uint32) SetsOption
SetsVersion sets the library version for set extensions.
func SetsWithoutCostModels ¶
func SetsWithoutCostModels() SetsOption
SetsWithoutCostModels disables overload cost models for Sets.
type StringsOption ¶
type StringsOption func(*stringLib) *stringLib
StringsOption is a functional interface for configuring the strings library.
func StringsLocale ¶
func StringsLocale(locale string) StringsOption
StringsLocale configures the library with the given locale. The locale tag will be checked for validity at the time that EnvOptions are configured. If this option is not passed, string.format will behave as if en_US was passed as the locale.
If StringsVersion is greater than or equal to 4, this option is ignored.
func StringsMaxPrecision ¶
func StringsMaxPrecision(limit int) StringsOption
StringsMaxPrecision configures the maximum precision for floating-point format clauses.
If not set, the default is 100 for version >= 5, and no limit for earlier versions.
func StringsValidateFormatCalls ¶
func StringsValidateFormatCalls(value bool) StringsOption
StringsValidateFormatCalls validates type-checked ASTs to ensure that string.format() calls have valid formatting clauses and valid argument types for each clause.
Deprecated
func StringsVersion ¶
func StringsVersion(version uint32) StringsOption
StringsVersion configures the version of the string library.
The version limits which functions are available. Only functions introduced below or equal to the given version included in the library. If this option is not set, all functions are available.
See the library documentation to determine which version a function was introduced. If the documentation does not state which version a function was introduced, it can be assumed to be introduced at version 0, when the library was first created.
func StringsWithoutCostModels ¶
func StringsWithoutCostModels() StringsOption
StringsWithoutCostModels disables overload cost models for Strings.