Expr package uses a specific syntax. In this document, you can find all supported syntaxes.
The package supports:
- strings - single and double quotes (e.g.
"hello",'hello') - numbers - e.g.
103,2.5 - arrays - e.g.
[1, 2, 3] - maps - e.g.
{foo: "bar"} - booleans -
trueandfalse - nil -
nil
Public properties on structs can be accessed by using the . syntax.
If you pass an array into an expression, use the [] syntax to access array keys.
foo.Array[0].ValueThe . syntax can also be used to call methods on an struct.
price.String()The package comes with a lot of operators:
+(addition)-(subtraction)*(multiplication)/(division)%(modulus)**(pow)
Example:
life + universe + everythingInteger literals may contain digit separators to allow digit grouping into more legible forms.
Example:
10_000_000_000
==(equal)!=(not equal)<(less than)>(greater than)<=(less than or equal to)>=(greater than or equal to)
notor!andor&&oror||
Example:
life < universe || life < everything
+(concatenation)matches(regex match)contains(string contains)startsWith(has prefix)endsWith(has suffix)
To test if a string does not match a regex, use the logical not operator in combination with the matches operator:
not ("foo" matches "^b.+")You must use parenthesis because the unary operator not has precedence over the binary operator matches.
Example:
'Arthur' + ' ' + 'Dent'Result will be set to Arthur Dent.
in(contain)not in(does not contain)
Example:
user.Group in ["human_resources", "marketing"]"foo" in {foo: 1, bar: 2}..(range)
Example:
user.Age in 18..45The range is inclusive:
1..3 == [1, 2, 3]foo ? 'yes' : 'no'
Example:
user.Age > 30 ? "mature" : "immature"len(length of array or string)all(will returntrueif all element satisfies the predicate)none(will returntrueif all element does NOT satisfies the predicate)any(will returntrueif any element satisfies the predicate)one(will returntrueif exactly ONE element satisfies the predicate)filter(filter array by the predicate)map(map all items with the closure)
Example:
// Ensure all tweets are less than 140 chars.
all(Tweets, {.Size < 140}){...}(closure)
Closures allowed only with builtin functions. To access current item use # symbol.
map(0..9, {# + 1})If the item of array is struct, it's possible to access fields of struct with omitted # symbol (#.Value becomes .Value).
filter(Tweets, {.Size > 140})array[:](slice)
Slices can work with arrays or strings.
Example:
// array is [1,2,3,4,5]
array[1:5] == [2,3,4]
array[3:] == [4,5]
array[:4] == [1,2,3]
array[:] == array