diff --git a/src/utils/converter.ts b/src/utils/converter.ts index 544c641..404aa5e 100644 --- a/src/utils/converter.ts +++ b/src/utils/converter.ts @@ -12,6 +12,7 @@ import { IndexFilterExpression, BlockExpression, TokenType, + BinaryExpression, } from '../types'; import { createBlockExpression, getLastElement } from './common'; @@ -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 = { + type: SyntaxType.LOGICAL_OR_EXPR, + op: '||', + args: [outputPropAST.value, refineLeafOutputPropAST(flatMapping.inputExpr)], + } as BinaryExpression; + } +} + function processFlatMappingPart( flatMapping: FlatMappingAST, partNum: number, @@ -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; } diff --git a/test/scenarios/mappings/data.ts b/test/scenarios/mappings/data.ts index b02a77f..93e7cb0 100644 --- a/test/scenarios/mappings/data.ts +++ b/test/scenarios/mappings/data.ts @@ -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: [ diff --git a/test/scenarios/mappings/or_mappings.json b/test/scenarios/mappings/or_mappings.json new file mode 100644 index 0000000..1d1999c --- /dev/null +++ b/test/scenarios/mappings/or_mappings.json @@ -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" + } +]