Skip to content

Commit

Permalink
feat: add support for or mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
koladilip committed Jun 24, 2024
1 parent 3961899 commit 3d09940
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 = {

Check warning on line 272 in src/utils/converter.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
type: SyntaxType.LOGICAL_OR_EXPR,

Check warning on line 273 in src/utils/converter.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
op: '||',

Check warning on line 274 in src/utils/converter.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
args: [outputPropAST.value, refineLeafOutputPropAST(flatMapping.inputExpr)],

Check warning on line 275 in src/utils/converter.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
} as BinaryExpression;

Check warning on line 276 in src/utils/converter.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 277 in src/utils/converter.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 277 in src/utils/converter.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 277 in src/utils/converter.ts

View check run for this annotation

Codecov / codecov/patch

src/utils/converter.ts#L272-L277

Added lines #L272 - L277 were not covered by tests
}

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 3d09940

Please sign in to comment.