Skip to content

Commit

Permalink
fix: object mappings with child props
Browse files Browse the repository at this point in the history
nested object mappings when added with new child props
was not working so this fix that and allows to define
then at any level.
  • Loading branch information
koladilip committed Oct 28, 2024
1 parent 9338ff3 commit 7e1e15d
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 17 deletions.
51 changes: 34 additions & 17 deletions src/utils/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,27 @@ import {
} from '../types';
import { createBlockExpression, getLastElement } from './common';

function createObjectExpression(): ObjectExpression {
function createObjectExpression(props: ObjectPropExpression[] = []): ObjectExpression {
return {
type: SyntaxType.OBJECT_EXPR,
props: [] as ObjectPropExpression[],
props,
};
}

function createObjectPropExpressionWithSpread(value: Expression): ObjectPropExpression {
return {
type: SyntaxType.OBJECT_PROP_EXPR,
value: {
type: SyntaxType.SPREAD_EXPR,
value,
},
};
}

function createObjectExpressionWithSpread(value: Expression): ObjectExpression {
return createObjectExpression([createObjectPropExpressionWithSpread(value)]);
}

function findOrCreateObjectPropExpression(
props: ObjectPropExpression[],
key: string,
Expand Down Expand Up @@ -222,6 +236,14 @@ function isWildcardSelector(expr: Expression): boolean {
return expr.type === SyntaxType.SELECTOR && expr.prop?.value === '*';
}

function isOutputPartRegularSelector(outputPart: Expression) {
return (
outputPart?.type === SyntaxType.SELECTOR &&
outputPart.prop?.value &&
outputPart.prop.value !== '*'
);
}

function processWildCardSelector(
flatMapping: FlatMappingAST,
currentOutputPropAST: ObjectPropExpression,
Expand Down Expand Up @@ -282,6 +304,15 @@ function handleNextPart(
currentOutputPropAST: ObjectPropExpression,
): ObjectExpression | undefined {
const nextOutputPart = flatMapping.outputExpr.parts[partNum];
const prevOutputPart = flatMapping.outputExpr.parts[partNum - 1];
if (
isOutputPartRegularSelector(prevOutputPart) &&
isOutputPartRegularSelector(nextOutputPart) &&
currentOutputPropAST.value.type !== SyntaxType.OBJECT_EXPR
) {
currentOutputPropAST.value = createObjectExpressionWithSpread(currentOutputPropAST.value);

Check warning on line 313 in src/utils/converter.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
return currentOutputPropAST.value as ObjectExpression;

Check warning on line 314 in src/utils/converter.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 315 in src/utils/converter.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 315 in src/utils/converter.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 315 in src/utils/converter.ts

View check run for this annotation

Codecov / codecov/patch

src/utils/converter.ts#L313-L315

Added lines #L313 - L315 were not covered by tests
if (nextOutputPart.filter?.type === SyntaxType.ALL_FILTER_EXPR) {
const objectExpr = processAllFilter(
flatMapping,
Expand Down Expand Up @@ -336,14 +367,6 @@ function handleNextParts(
return objectExpr;
}

function isOutputPartRegularSelector(outputPart: Expression) {
return (
outputPart.type === SyntaxType.SELECTOR &&
outputPart.prop?.value &&
outputPart.prop.value !== '*'
);
}

function refineLeafOutputPropAST(inputExpr: Expression): Expression {
if (
inputExpr.type === SyntaxType.PATH &&
Expand Down Expand Up @@ -399,13 +422,7 @@ function processFlatMappingPart(
}

function handleRootOnlyOutputMapping(flatMapping: FlatMappingAST, outputAST: ObjectExpression) {
outputAST.props.push({
type: SyntaxType.OBJECT_PROP_EXPR,
value: {
type: SyntaxType.SPREAD_EXPR,
value: flatMapping.inputExpr,
},
} as ObjectPropExpression);
outputAST.props.push(createObjectPropExpressionWithSpread(flatMapping.inputExpr));
}

function validateMappingsForIndexVar(flatMapping: FlatMappingAST, indexVar: string) {
Expand Down
28 changes: 28 additions & 0 deletions test/scenarios/mappings/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,13 @@ export const data: Scenario[] = [
},
},
],
traits3: {
display_name: 'Rudderstack Inc.',
category: 'Analytics',
custom_properties: {
bar: 1,
},
},
},
output: {
user_id: {
Expand All @@ -344,6 +351,15 @@ export const data: Scenario[] = [
},
],
},
traits3: {
value: {
display_name: 'Rudderstack Inc.',
category: 'Analytics',
custom_properties: {
bar: 1,
},
},
},
properties1: {
name: {
value: 'John Doe',
Expand All @@ -360,6 +376,18 @@ export const data: Scenario[] = [
age: 30,
},
],
properties3: {
display_name: 'Rudderstack Inc.',
category: 'Analytics',
custom_properties: {
bar: 1,
},
name: 'Rudderstack Inc.',
custom: {
bar: 1,
foo: 1,
},
},
},
},
{
Expand Down
16 changes: 16 additions & 0 deletions test/scenarios/mappings/object_mappings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,21 @@
{
"input": "$.traits2[*].*.value",
"output": "$.properties2[*].*"
},
{
"input": "$.traits3",
"output": "$.properties3"
},
{
"input": "$.traits3.display_name",
"output": "$.properties3.name"
},
{
"input": "$.traits3.custom_properties",
"output": "$.properties3.custom"
},
{
"input": "$.traits3.custom_properties.bar",
"output": "$.properties3.custom.foo"
}
]

0 comments on commit 7e1e15d

Please sign in to comment.