Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: object mappings with child props #124

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
} 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 @@
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 @@
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
koladilip marked this conversation as resolved.
Show resolved Hide resolved
if (nextOutputPart.filter?.type === SyntaxType.ALL_FILTER_EXPR) {
const objectExpr = processAllFilter(
flatMapping,
Expand Down Expand Up @@ -336,14 +367,6 @@
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 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"
}
]
Loading