From 6b5269e64e0f037f2c534e2aea4bf4a041025b43 Mon Sep 17 00:00:00 2001 From: Dilip Kola Date: Sat, 1 Jun 2024 17:54:12 +0530 Subject: [PATCH] refactor: add error handling in flat mapping convertor --- src/engine.ts | 5 +++-- src/reverse_translator.ts | 4 ++-- src/types.ts | 6 +++--- src/utils/converter.ts | 11 +++++++---- test/scenarios/mappings/data.ts | 9 ++++++++- test/scenarios/mappings/invalid_mappings.json | 10 ++++++++++ 6 files changed, 33 insertions(+), 12 deletions(-) create mode 100644 test/scenarios/mappings/invalid_mappings.json diff --git a/src/engine.ts b/src/engine.ts index 2036c5f..2a6fe9c 100644 --- a/src/engine.ts +++ b/src/engine.ts @@ -42,8 +42,9 @@ export class JsonTemplateEngine { options?: EngineOptions, ): Expression { const flatMappingAST = mappings.map((mapping) => ({ - input: JsonTemplateEngine.parse(mapping.input, options).statements[0], - output: JsonTemplateEngine.parse(mapping.output, options).statements[0], + ...mapping, + inputExpr: JsonTemplateEngine.parse(mapping.input, options).statements[0], + outputExpr: JsonTemplateEngine.parse(mapping.output, options).statements[0], })); return convertToObjectMapping(flatMappingAST); } diff --git a/src/reverse_translator.ts b/src/reverse_translator.ts index 97a8e6a..f53d7d2 100644 --- a/src/reverse_translator.ts +++ b/src/reverse_translator.ts @@ -429,8 +429,8 @@ export class JsonTemplateReverseTranslator { translateObjectExpression(expr: ObjectExpression): string { const code: string[] = []; - code.push('{\n'); - code.push(this.translateExpressions(expr.props, ',\n')); + code.push('{\n\t'); + code.push(this.translateExpressions(expr.props, ',\n\t')); code.push('\n}'); return code.join(''); } diff --git a/src/types.ts b/src/types.ts index e76f38a..53c283f 100644 --- a/src/types.ts +++ b/src/types.ts @@ -279,7 +279,7 @@ export type FlatMappingPaths = { output: string; }; -export type FlatMappingAST = { - input: PathExpression; - output: PathExpression; +export type FlatMappingAST = FlatMappingPaths & { + inputExpr: PathExpression; + outputExpr: PathExpression; }; diff --git a/src/utils/converter.ts b/src/utils/converter.ts index 65a9874..bb14974 100644 --- a/src/utils/converter.ts +++ b/src/utils/converter.ts @@ -85,11 +85,14 @@ function processAllFilter( function processFlatMapping(flatMapping: FlatMappingAST, outputAST: ObjectExpression) { let currentOutputPropsAST = outputAST.props; - const currentInputAST = flatMapping.input; + const currentInputAST = flatMapping.inputExpr; - const numOutputParts = flatMapping.output.parts.length; + const numOutputParts = flatMapping.outputExpr.parts.length; for (let i = 0; i < numOutputParts; i++) { - const outputPart = flatMapping.output.parts[i]; + if (!currentOutputPropsAST) { + throw new Error(`Failed to process output mapping: ${flatMapping.output}`); + } + const outputPart = flatMapping.outputExpr.parts[i]; if (outputPart.type === SyntaxType.SELECTOR && outputPart.prop?.value) { const key = outputPart.prop.value; @@ -105,7 +108,7 @@ function processFlatMapping(flatMapping: FlatMappingAST, outputAST: ObjectExpres const currentOutputPropAST = findOrCreateObjectPropExpression(currentOutputPropsAST, key); let objectExpr: ObjectExpression = currentOutputPropAST.value as ObjectExpression; - const nextOutputPart = flatMapping.output.parts[i + 1] as ArrayFilterExpression; + const nextOutputPart = flatMapping.outputExpr.parts[i + 1] as ArrayFilterExpression; if (nextOutputPart.filter?.type === SyntaxType.ALL_FILTER_EXPR) { objectExpr = processAllFilter(currentInputAST, currentOutputPropAST); } else if (nextOutputPart.filter?.type === SyntaxType.ARRAY_INDEX_FILTER_EXPR) { diff --git a/test/scenarios/mappings/data.ts b/test/scenarios/mappings/data.ts index d125031..a2f27e1 100644 --- a/test/scenarios/mappings/data.ts +++ b/test/scenarios/mappings/data.ts @@ -143,7 +143,14 @@ export const data: Scenario[] = [ ], }, }, - + { + containsMappings: true, + templatePath: 'invalid_mappings.json', + options: { + defaultPathType: PathType.JSON, + }, + error: 'Failed to process output mapping', + }, { containsMappings: true, templatePath: 'mappings_with_root_fields.json', diff --git a/test/scenarios/mappings/invalid_mappings.json b/test/scenarios/mappings/invalid_mappings.json new file mode 100644 index 0000000..266a1ec --- /dev/null +++ b/test/scenarios/mappings/invalid_mappings.json @@ -0,0 +1,10 @@ +[ + { + "input": "$.events[0]", + "output": "$.events[0].name" + }, + { + "input": "$.discount", + "output": "$.events[0].name[*].discount" + } +]