This topic describes how to create an Aviator function and use the Aviator function as the prefilter or post-filter for an API.
Background information
Limits
- You must use an exclusive resource group for DataService Studio to run an Aviator function.
- For the same API, the function used as the prefilter and the function used as the post-filter must be of the same type. You are not allowed to use an Aviator function and a Python function as the filters for the same API.
Precautions
To ensure data security and function performance, you must use an exclusive resource
group for DataService Studio to run an Aviator function.
- When you edit or test a function, you must associate the function with an exclusive resource group for DataService Studio. For more information, see Create and manage functions.
- If you enable the filter feature for an API and associate the API with a function when you develop the API, you must associate the API with an exclusive resource group for DataStudio Service. For more information, see Create an API in the codeless UI.
Syntax of an Aviator function
The system provides the following function template for an Aviator function. For more information about the syntax of an Aviator function, see Aviator function syntax.
## --- AviatorScript ---
## $0: the default name of the input param.
## Expressions must be separated by the semicolon ";".
## Example 1:
## $0.a: Get the attribute "a" from the input param "$0".
## $0.b[index]: Get the index of the array "b" from the input param "$0". The index cannot be an expression or a variable.
## Example 2:
## Input: {"keyA":[1,2],"keyB":"value"}
## Function: $0.keyA[0]=3; $0.keyB="new_value"; return $0;
## Output: {"keyA":[3,2],"keyB":"new_value"}
return $0;
You can modify the function based on your business requirements.
- The default name of the input parameter of an Aviator function is
$0
and cannot be changed. You can use the$0
parameter to obtain the input of the Aviator function. - The input of a function must be in the JSON format. In DataService Studio, a function
is used as a filter to process the request parameters or responses of an API. The
request parameters of an API are defined in the single-layer JSON format. The responses
of the API are defined in the multi-layer JSON format. Therefore, the input of a function
must also be in the JSON format. The
$0
parameter specifies input data in the JSON format. - If a function is used as the prefilter for an API, the system converts request parameters
of the API or outputs of the previous function from
key-value pairs
into data in the JSON format. Then, the system uses the data in the JSON format to replace the$0
parameter. The data type of values in the JSON data can only be STRING. - If a function is used as the prefilter for an API, the outputs of the function are
key-value pairs
. The key-value pairs are used as the input parameter of an SQL statement. Therefore, thekey-value pairs
cannot contain nested key-value pairs.