Code Pattern Quick Start

In the Interface is described how the code patterns work internally. This page explains how to create your own code pattern component.

Code Pattern is a type of the Component, so its creation is partially described in the Component Quick Start. Here are explained only the extra steps, that are specific for the code patterns.

Creating Component

Follow the Component Quick Start.

Use the Code Pattern type, when creating a component.

Screenshot -- Add component

Modify the settings described in the Interface - Developer Portal.

Implementation

Follow the Component Quick Start.

Learn how the whole process works in the Code Generation Process.

Implement the Generate Action as specified in Interface - Generate Action.

  • First, load the Configuration.
  • Validate the configuration, action = generate is expected.
  • Other actions should not be implemented (not even run).
  • In case of error, use correct Return Value.
  • Generate code based on the configuration.
  • Write the result in Output Format on stdout.
  • Exit with the exit code = 0 if success.

Running Component

The code pattern component is specific.

It cannot be run as described in Component Quick Start - Running Component.

The code pattern component will be invisible in the user interface until it is published.

But there are two other ways to try it as a component.

Run via API

The first way is to call the Generate Action via the API.

  • In this way, you can test that the component returns the desired results based on the specified inputs.
  • Usa the Run component action API call.
  • The API token is needed.

Modify transformation via API

The second way is to modify the transformation to use an unpublished code pattern.

Create a Empty Transformation

First, click Transformations on the project menu.

Then click New Transformation to create a new transformation.

Screenshot -- Transformations page

In the modal click on the selected type of the transformation.

Screenshot -- Add new transformation modal

Fill in the name and optionally the description.

Do not select any code pattern.

Screenshot -- Net transformation

You have created an empty transformation.

Set Code Pattern to Transformation

Make note of the component and configuration id from the URL. You will need them in the API calls.

/admin/projects/{PROJECT_ID}/transformations-v2/{COMPONENT_ID}/{CONFIGURATION_ID}

Set the code pattern to transformation via Storage API.

Load the configuration in the JSON format via the Configuration Detail API call.

curl \ 
  --include \
  --header "X-StorageApi-Token: {API_TOKEN}" \
'{STORAGE API}/v2/storage/components/{COMPONENT_ID}/configs/{CONFIGURATION_ID}'

Example response, some keys omitted.

{
  "id": "1234",
  "name": "API test",
  "configuration": {}
}

It is necessary to set the id of the code pattern component to the configuration.

{
  "configuration": {
    "runtime": {
      "codePattern": {
        "componentId": "keboola.example-pattern"
      }
    }
  }
}

Update the configuration via the Update Configuration API call. JSON must be url-encoded.

curl 
 --include \
 --request PUT \
 --header "X-StorageApi-Token: {API_TOKEN}" \
 --header "Content-Type: application/x-www-form-urlencoded" \
 --data-binary "configuration=%7B%22runtime%22%3A%7B%22codePattern%22%3A%7B%22componentId%22%3A%22keboola.example-pattern%22%7D%7D%7D" \
'{STORAGE API}/v2/storage/components/{COMPONENT_ID}/configs/{CONFIGURATION_ID}'

Example response, some keys omitted.

{
  "id": "1234",
  "name": "API test",
  "configuration": {
    "runtime": {
      "codePattern": {
        "componentId": "keboola.example-pattern"
        }
      }
   }
}

The transformation now uses the code pattern, and you can test it in the user interface.

Publishing Component

Make sure the component is set up according the Interface - Developer Portal.

Follow the Publish Component tutorial.

Next Steps

  • Interface describes how the code patterns work internally.
  • Code Patterns Help shows the code patterns from the user’s point of view.