Expressions
Expressions are like a mini-scripting language embedded within Pewpew. Expressions only deal with very limited data types--the JSON types--strings, numbers, booleans, null values, arrays and objects.
Expressions are most commonly used to access data from a provider (via templates) or to transform data from an HTTP response to be sent into a provider. Expressions allow the traversal of object and array structures, evaluating boolean logic and basic mathematic operators. Helper functions extend the functionality of expressions further.
Operators
Operator | Description |
---|---|
== | Equal. Check that two values are equal to each other and produces a boolean. |
!= | Not equal. Check that two values are not equal to each other and produces a boolean. |
> | Greater than. Check that the left value is greater than the right and produces a boolean. |
< | Less than. Check that the left value is less than the right and produces a boolean. |
>= | Greater than or equal to. Check that the left value is greater than or equal to the right and produces a boolean. |
<= | Less than or equal to. Check that the left value is less than or equal to the right and produces a boolean. |
&& | And. Checks that two values are true and produces a boolean. |
|| | Or. Checks that one of two values is true and produces a boolean. |
+ | Add. Adds two numbers together producing a number. |
- | Subtract. Subtracts two numbers producing a number. |
* | Multiply. Multiplies two numbers producing a number. |
/ | Divide. Divides two numbers producing a number. |
% | Remainder. Provides the remainder after dividing two numbers. |
Helper functions
Function | Description |
---|---|
or
|
When used in a endpoints.declare subsection When used outside a declare subsection, See the endpoints.declare subsection for an example. |
encode(value, encoding)
|
Encode a string with the given encoding. value - any expression. The result of the expression will be coerced to a string if needed and then encoded with the specified encoding.
Example: with the value |
end_pad(value, min_length, pad_string)
|
Pads a string or number to be minimum length. Any added padding will be added to the end of the string. value - An expression whose value will be coerced to a string if needed. Example: with the value |
entries(value)
|
Returns the "entries" which make up value. For an object this will yield the object's key/value pairs. For an array it yields the array's indices and elements. For a string it yields the indices and the characters. For boolean and null types it yields back those same values. Examples With the value
would return
would return
would return
would return |
epoch(unit)
|
Returns time since the unix epoch. unit - A string literal of |
if(check, true_value, false_value) |
Does a boolean check against the first argument, if true the second argument is returned otherwise the third argument is returned. check - An expression which will be coerced to a boolean if needed. Example: |
or
|
Turns an array of values into a string or turns an object into a string. value - any expression. When the expression resolves to an array, the elements of the array are coerced to a string if needed and are then joined together to a single string using the specified separator. When the value resolves to an object and the three argument variant is used then the object will be turned into a string with the specified separators. In any other case value is coerced to a string and returned. Examples With the value With the value
or for an alternative, json-ified view: |
json_path(query)
|
Provides the ability to execute a json path query against an object and returns an array of values. The query must be a string literal. Example: |
match(string, regex)
|
Allows matching a string against a regex. Returns an object with the matches from the regex. Named matches are supported though any unnamed matches will be a number based on their position. Match If the first parameter is not a string it will be coerced into a string. Regex look arounds are not supported. Example: If a response body were the following:
Then the following expression:
Would return:
|
|
Selects the largest number out of a sequence of numbers. Each argument should be an expression which resolves to a number otherwise it will not be considered in determining the min. If no arguments are provided, or if none of the arguments resolve to a number, then |
|
Selects the smallest number out of a sequence of numbers. Each argument should be an expression which resolves to a number otherwise it will not be considered in determining the max. If no arguments are provided, or if none of the arguments resolve to a number, then |
|
Generates a random number between start (inclusive) and end (exclusive). Both start and end must be number literals. If both numbers are integers only integers will be generated within the specified range. If either number is a floating point number then a floating point number will be generated within the specified range. |
|
Creates an array of numeric values in the specified range. start - any expression resolving to a whole number. Represents the starting number for the range (inclusive). end - any expression resolving to a whole number. Represents the end number for the range (exclusive). Examples:
|
or
|
Creates an array of Example: |
start_pad(value, min_length, pad_string)
|
Pads a string or number to be minimum length. Any added padding will be added to the start of the string. value - an expression whose value will be coerced to a string if needed. Example: with the value |
replace(needle, haystack, replacer)
|
Replaces any instance of a string (needle) within a JSON value (haystack) with another string (replacer). This function will recursively check the JSON for any string value of needle and replace it with replacer. This includes checking within a nested object's key and value pairs, within arrays and within strings. needle - an expression whose value will be coerced to a string if needed. Example: with the value |
parseInt(value)
|
Converts a string or other value into an integer ( value - any expression. The result of the expression will be coerced to a string if needed and then converted. |
parseFloat(value)
|
Converts a string or other value into an floating point number ( value - any expression. The result of the expression will be coerced to a string if needed and then converted. |