Skip to content

Commit

Permalink
refactor: add error handling in flat mapping convertor
Browse files Browse the repository at this point in the history
  • Loading branch information
koladilip committed Jun 1, 2024
1 parent 71f3b06 commit 6b5269e
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 12 deletions.
5 changes: 3 additions & 2 deletions src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/reverse_translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('');
}
Expand Down
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ export type FlatMappingPaths = {
output: string;
};

export type FlatMappingAST = {
input: PathExpression;
output: PathExpression;
export type FlatMappingAST = FlatMappingPaths & {
inputExpr: PathExpression;
outputExpr: PathExpression;
};
11 changes: 7 additions & 4 deletions src/utils/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
9 changes: 8 additions & 1 deletion test/scenarios/mappings/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
10 changes: 10 additions & 0 deletions test/scenarios/mappings/invalid_mappings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"input": "$.events[0]",
"output": "$.events[0].name"
},
{
"input": "$.discount",
"output": "$.events[0].name[*].discount"
}
]

0 comments on commit 6b5269e

Please sign in to comment.