Skip to content

Commit

Permalink
feat: add support for mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
koladilip committed Jun 14, 2024
1 parent 224b17b commit 9357f60
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 9 deletions.
7 changes: 2 additions & 5 deletions src/common/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { FlatMappingPaths } from '@rudderstack/json-template-engine';

Check warning on line 1 in src/common/types.ts

View workflow job for this annotation

GitHub Actions / Check for formatting & lint errors

'FlatMappingPaths' is defined but never used
import { type StepExecutionError } from '../errors';

export interface Executor {
Expand Down Expand Up @@ -109,17 +110,13 @@ export type SimpleStep = StepCommon & {
// One of the template, templatePath, externalWorkflowPath, Function are required for simple steps
template?: string;
templatePath?: string;
mappings?: boolean;
// external workflow is executed independently and we can access only final output
externalWorkflow?: ExternalWorkflow;
// Function must be passed using bindings
functionName?: string;
};

export type Template = {
content?: string;
path?: string;
};

export type BatchConfig = {
options?: {
size?: number;
Expand Down
29 changes: 25 additions & 4 deletions src/steps/base/simple/executors/template/jsontemplate.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
import { JsonTemplateEngine, PathType } from '@rudderstack/json-template-engine';
import { ExecutionBindings, Step, StepOutput } from '../../../../../common';
import {
Expression,
FlatMappingPaths,
JsonTemplateEngine,
PathType,
} from '@rudderstack/json-template-engine';
import { ExecutionBindings, SimpleStep, StepOutput } from '../../../../../common';
import { BaseStepExecutor } from '../../../executors/base';

export class JsonTemplateStepExecutor extends BaseStepExecutor {
private readonly templateEngine: JsonTemplateEngine;

constructor(step: Step, template: string, bindings?: Record<string, any>) {
static parse(template: string, mappings?: boolean, bindings?: Record<string, any>): Expression {
if (mappings) {
try {
const mappingPaths = JSON.parse(template) as FlatMappingPaths[];
return JsonTemplateEngine.parse(mappingPaths, { defaultPathType: PathType.JSON });
} catch (e) {
// parse as template
}
}
return JsonTemplateEngine.parse(template, {
defaultPathType: PathType.SIMPLE,
compileTimeBindings: bindings,
});
}

constructor(step: SimpleStep, template: string, bindings?: Record<string, any>) {
super(step);
this.templateEngine = JsonTemplateEngine.create(template, {
const expression = JsonTemplateStepExecutor.parse(template, step.mappings, bindings);
this.templateEngine = JsonTemplateEngine.create(expression, {
compileTimeBindings: bindings,
defaultPathType: PathType.SIMPLE,
});
Expand Down
12 changes: 12 additions & 0 deletions test/scenarios/mappings/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Scenario } from '../../types';

export const data: Scenario[] = [
{
input: {
a: 1,
},
output: {
bar: 1,
},
},
];
18 changes: 18 additions & 0 deletions test/scenarios/mappings/workflow.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
templateType: jsontemplate
steps:
- name: mappings
mappings: true
description: |
InputTemplate will be parsed as normal json template/
template will be parsed as mappings.
inputTemplate: |
{
foo: ^.a
}
template: |
[
{
"input": "$.foo",
"output": "$.bar"
}
]

0 comments on commit 9357f60

Please sign in to comment.