Skip to content

Commit

Permalink
refactor: mappings converter
Browse files Browse the repository at this point in the history
  • Loading branch information
koladilip committed Jun 16, 2024
1 parent a1a41b3 commit 9fade93
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/utils/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,14 @@ function validateMapping(flatMapping: FlatMappingAST) {
);
}
}

function processFlatMappingParts(flatMapping: FlatMappingAST, objectExpr: ObjectExpression) {
let currentOutputPropsAST = objectExpr.props;
for (let i = 0; i < flatMapping.outputExpr.parts.length; i++) {
currentOutputPropsAST = processFlatMappingPart(flatMapping, i, currentOutputPropsAST);
}
}

/**
* Convert Flat to Object Mappings
*/
Expand All @@ -264,10 +272,7 @@ export function convertToObjectMapping(
objectExpr = handleNextParts(flatMapping, 0, objectPropExpr);
pathAST = objectPropExpr.value as PathExpression;
}
let currentOutputPropsAST = objectExpr.props;
for (let i = 0; i < flatMapping.outputExpr.parts.length; i++) {
currentOutputPropsAST = processFlatMappingPart(flatMapping, i, currentOutputPropsAST);
}
processFlatMappingParts(flatMapping, objectExpr);
} else {
handleRootOnlyOutputMapping(flatMapping, outputAST);
}
Expand Down
24 changes: 24 additions & 0 deletions test/scenarios/mappings/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ export const data: Scenario[] = [
{
mappingsPath: 'object_mappings.json',
input: {
user_id: 1,
traits1: {
name: 'John Doe',
age: 30,
Expand All @@ -256,6 +257,29 @@ export const data: Scenario[] = [
],
},
output: {
user_id: {
value: 1,
},
traits1: {
value: {
name: 'John Doe',
age: 30,
},
},
traits2: {
value: [
{
name: {
value: 'John Doe',
},
},
{
age: {
value: 30,
},
},
],
},
properties1: {
name: {
value: 'John Doe',
Expand Down
4 changes: 4 additions & 0 deletions test/scenarios/mappings/object_mappings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
[
{
"input": "$.*",
"output": "$.*.value"
},
{
"input": "$.traits1.*",
"output": "$.properties1.*.value"
Expand Down

0 comments on commit 9fade93

Please sign in to comment.