In Serverless Workflow each instance is associated with a data model. These models consist of JSON objects. The data inside these models needs to be accessed and updated throughout the flow execution. jq is a powerful filtering tool that makes working with JSON data seamless. The Serverless Workflow specification supports the use of jq expressions in multiple places.
In order to improve the authoring experience for the user while working with these expressions, a new auto-completion capability is added to the editor. These features are available in the VS Code extension as well as the Serverless Logic Web Tools and work with both JSON and YAML files.
- VS Code Extension for Serverless Workflow Editor (0.28.0)
- Serverless Logic Web Tools (0.28.0)
- VS Code (1.66.0+)
The jq auto-completion feature can be categorized into three:
- The built-in functions
- The workflow variables
- The reusable function expressions
The Built-In Functions:
These are functions provided by jq out of the box. These functions come in handy to do a lot of operations like max, min, length, etc. The completion provides a list of these functions along with a description of what it does so that the user does not have to read the docs every time. More information on the built-in functions can be found here.
The Workflow Variables:
The serverless workflow works with a lot of variables, which are provided to the project in the form of a JSON schema, an open API file, or an async API file. A Variable in Serverless Workflow always starts with a .(DOT). The auto-completion feature parses these files (if present), extracts the variables, and provides the completion result to the user. Please note that the auto-completion feature is able to parse files in a remote URL as well as a local file system.
In the first part of the video above, there is a JSON schema file present in the path: resources/schema/expression.json. The jq expressions can work in certain places and one of them is in the operation of functions which is of type expression. As you can see, the JSON schema has numbers, x, and y in its properties which are listed in the completion items. Please note that remote URLs are also supported for JSON schema.
In the second part of the video, we have a function (Check action on RHODS) and this is an OpenAPI specification. We use a remote URL here and we see in the OpenAPI file some parameters available like noOfRunningPods, avgLoad, etc while trying to use the auto-complete we can see these values appearing in the completion item list. Please note that local files (eg: resources/spec/multiplication.yaml) also works.
In the third part of the video we have a function which is of type async API and the corresponding file is present in the resources/spec/resume-event.yaml. When trying to auto-complete, we parse the async api file and extract the parameters out of it and show the completion items. Please note that this also works with remote URLs.
The Reusable Function Expressions:
A reusable function expression is a function definition inside the functions array, which has a type (field) expression and a jq expression as the value for the operation field. These functions can be reused in the specification using the fn: followed by name of the function. The auto-completion feature also provides a list of these functions present in the functions array.
Note: The expressions can be used in various places. To know more about the usage please visit here.
The auto-completion feature assists the user in writing the jq expressions and thus improves the authoring experience. In order to learn more about the jq expression in Serverless Workflow, please visit our blog post on Serverless Workflow Expressions.
Stay tuned to get more updates on the upcoming features for the Serverless Workflow.