Skip to content

Commit

Permalink
feat: add support for or mappings (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
koladilip authored Jun 25, 2024
1 parent 3961899 commit 0e45680
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/utils/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
IndexFilterExpression,
BlockExpression,
TokenType,
BinaryExpression,
} from '../types';
import { createBlockExpression, getLastElement } from './common';

Expand Down Expand Up @@ -255,6 +256,27 @@ function refineLeafOutputPropAST(inputExpr: Expression): Expression {
return inputExpr;
}

function handleLastOutputPart(
flatMapping: FlatMappingAST,
currentOutputPropsAST: ObjectPropExpression[],
key: string,
) {
const outputPropAST = currentOutputPropsAST.find((prop) => prop.key === key);
if (!outputPropAST) {
currentOutputPropsAST.push({
type: SyntaxType.OBJECT_PROP_EXPR,
key,
value: refineLeafOutputPropAST(flatMapping.inputExpr),
} as ObjectPropExpression);
} else {
outputPropAST.value = {
type: SyntaxType.LOGICAL_OR_EXPR,
op: '||',
args: [outputPropAST.value, refineLeafOutputPropAST(flatMapping.inputExpr)],
} as BinaryExpression;
}
}

function processFlatMappingPart(
flatMapping: FlatMappingAST,
partNum: number,
Expand All @@ -267,11 +289,7 @@ function processFlatMappingPart(
const key = outputPart.prop.value;

if (partNum === flatMapping.outputExpr.parts.length - 1) {
currentOutputPropsAST.push({
type: SyntaxType.OBJECT_PROP_EXPR,
key,
value: refineLeafOutputPropAST(flatMapping.inputExpr),
} as ObjectPropExpression);
handleLastOutputPart(flatMapping, currentOutputPropsAST, key);
return currentOutputPropsAST;
}

Expand Down
53 changes: 53 additions & 0 deletions test/scenarios/mappings/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,59 @@ export const data: Scenario[] = [
],
},
},
{
mappingsPath: 'or_mappings.json',
input: {
context: {
properties: {
name: 'John',
age: 30,
},
},
},
output: {
user: {
name: 'John',
age: 30,
},
},
},
{
mappingsPath: 'or_mappings.json',
input: {
properties: {
name: 'John Doe',
age: 30,
},
context: {
properties: {
name: 'John',
age: 30,
},
},
},
output: {
user: {
name: 'John Doe',
age: 30,
},
},
},
{
mappingsPath: 'or_mappings.json',
input: {
properties: {
name: 'John Doe',
age: 30,
},
},
output: {
user: {
name: 'John Doe',
age: 30,
},
},
},
{
mappingsPath: 'root_array_mappings.json',
input: [
Expand Down
18 changes: 18 additions & 0 deletions test/scenarios/mappings/or_mappings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"input": "$.properties.name",
"output": "$.user.name"
},
{
"input": "$.properties.age",
"output": "$.user.age"
},
{
"input": "$.context.properties.name",
"output": "$.user.name"
},
{
"input": "$.context.properties.age",
"output": "$.user.age"
}
]

0 comments on commit 0e45680

Please sign in to comment.