From 9fade93d047879e4f63cad1ee5f62e2509a09dc1 Mon Sep 17 00:00:00 2001 From: Dilip Kola Date: Sun, 16 Jun 2024 16:55:05 +0530 Subject: [PATCH] refactor: mappings converter --- src/utils/converter.ts | 13 +++++++---- test/scenarios/mappings/data.ts | 24 ++++++++++++++++++++ test/scenarios/mappings/object_mappings.json | 4 ++++ 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/utils/converter.ts b/src/utils/converter.ts index 55137d0..533c826 100644 --- a/src/utils/converter.ts +++ b/src/utils/converter.ts @@ -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 */ @@ -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); } diff --git a/test/scenarios/mappings/data.ts b/test/scenarios/mappings/data.ts index 5092fae..4887c6d 100644 --- a/test/scenarios/mappings/data.ts +++ b/test/scenarios/mappings/data.ts @@ -238,6 +238,7 @@ export const data: Scenario[] = [ { mappingsPath: 'object_mappings.json', input: { + user_id: 1, traits1: { name: 'John Doe', age: 30, @@ -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', diff --git a/test/scenarios/mappings/object_mappings.json b/test/scenarios/mappings/object_mappings.json index 0fa7178..ba13424 100644 --- a/test/scenarios/mappings/object_mappings.json +++ b/test/scenarios/mappings/object_mappings.json @@ -1,4 +1,8 @@ [ + { + "input": "$.*", + "output": "$.*.value" + }, { "input": "$.traits1.*", "output": "$.properties1.*.value"