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: add prefix for wildcard prop values #68

Merged
merged 9 commits into from
Jun 10, 2024
17 changes: 13 additions & 4 deletions src/translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,12 +542,19 @@ export class JsonTemplateTranslator {
return code.join('');
}

private static translateObjectWildcardValue(expr: ObjectWildcardValueExpression) {
return `${VARS_PREFIX}${expr.value}`;
}

private translateObjectWildcardValueExpr(
expr: ObjectWildcardValueExpression,
dest: string,
_ctx: string,
): string {
return JsonTemplateTranslator.generateAssignmentCode(dest, expr.value);
return JsonTemplateTranslator.generateAssignmentCode(
dest,
JsonTemplateTranslator.translateObjectWildcardValue(expr),
);
}

private translateObjectWildcardProp(
Expand All @@ -557,13 +564,15 @@ export class JsonTemplateTranslator {
vars: string[] = [],
): string {
const code: string[] = [];
const keyExpr = expr.key as ObjectWildcardValueExpression;
const keyVar = JsonTemplateTranslator.translateObjectWildcardValue(
expr.key as ObjectWildcardValueExpression,
);
code.push(JsonTemplateTranslator.generateAssignmentCode(dest, '{}'));
const valueVar = this.acquireVar();
vars.push(valueVar);
code.push(`for(let [key, value] of Object.entries(${ctx})){`);
code.push(`for(let [${VARS_PREFIX}key, ${VARS_PREFIX}value] of Object.entries(${ctx})){`);
code.push(this.translateExpr(expr.value, valueVar, ctx));
code.push(`${dest}[${keyExpr.value}] = ${valueVar};`);
code.push(`${dest}[${keyVar}] = ${valueVar};`);
koladilip marked this conversation as resolved.
Show resolved Hide resolved
code.push('}');
return code.join('');
}
Expand Down
40 changes: 22 additions & 18 deletions src/utils/converter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable no-param-reassign */
import { EMPTY_EXPR } from '../constants';
import {
SyntaxType,
PathExpression,
Expand Down Expand Up @@ -58,29 +59,32 @@ function processArrayIndexFilter(
function processAllFilter(
currentInputAST: PathExpression,
currentOutputPropAST: ObjectPropExpression,
): ObjectExpression {
): Expression {
const filterIndex = currentInputAST.parts.findIndex(
(part) => part.type === SyntaxType.OBJECT_FILTER_EXPR,
);

if (filterIndex === -1) {
return currentOutputPropAST.value as ObjectExpression;
}
const matchedInputParts = currentInputAST.parts.splice(0, filterIndex + 1);
if (currentOutputPropAST.value.type !== SyntaxType.PATH) {
matchedInputParts.push(createBlockExpression(currentOutputPropAST.value));
currentOutputPropAST.value = {
type: SyntaxType.PATH,
root: currentInputAST.root,
pathType: currentInputAST.pathType,
parts: matchedInputParts,
returnAsArray: true,
} as PathExpression;
if (currentOutputPropAST.value.type === SyntaxType.OBJECT_EXPR) {
return currentOutputPropAST.value;
}
} else {
const matchedInputParts = currentInputAST.parts.splice(0, filterIndex + 1);
if (currentOutputPropAST.value.type !== SyntaxType.PATH) {
matchedInputParts.push(createBlockExpression(currentOutputPropAST.value));
currentOutputPropAST.value = {
type: SyntaxType.PATH,
root: currentInputAST.root,
pathType: currentInputAST.pathType,
parts: matchedInputParts,
returnAsArray: true,
} as PathExpression;
}
currentInputAST.root = undefined;
}
currentInputAST.root = undefined;

const blockExpr = getLastElement(currentOutputPropAST.value.parts) as BlockExpression;
return blockExpr.statements[0] as ObjectExpression;
const blockExpr = getLastElement(currentOutputPropAST.value.parts) as Expression;
return blockExpr?.statements?.[0] || EMPTY_EXPR;
}

function isWildcardSelector(expr: Expression): boolean {
Expand Down Expand Up @@ -136,7 +140,7 @@ function handleNextPart(
flatMapping: FlatMappingAST,
partNum: number,
currentOutputPropAST: ObjectPropExpression,
): ObjectExpression {
): Expression {
const nextOutputPart = flatMapping.outputExpr.parts[partNum];
if (nextOutputPart.filter?.type === SyntaxType.ALL_FILTER_EXPR) {
return processAllFilter(flatMapping.inputExpr, currentOutputPropAST);
Expand All @@ -154,7 +158,7 @@ function handleNextPart(
partNum === flatMapping.outputExpr.parts.length - 1,
);
}
return currentOutputPropAST.value as ObjectExpression;
return currentOutputPropAST.value;
}

function processFlatMappingPart(
Expand Down
4 changes: 4 additions & 0 deletions test/scenarios/mappings/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Scenario } from '../../types';
const input = {
userId: 'u1',
discount: 10,
coupon: 'DISCOUNT',
events: ['purchase', 'custom'],
context: {
traits: {
Expand Down Expand Up @@ -185,17 +186,20 @@ export const data: Scenario[] = [
product_name: 'p1',
product_category: 'baby',
discount: 10,
coupon_code: 'DISCOUNT',
},
{
product_id: 2,
product_name: 'p2',
discount: 10,
coupon_code: 'DISCOUNT',
},
{
product_id: 3,
product_name: 'p3',
product_category: 'home',
discount: 10,
coupon_code: 'DISCOUNT',
},
],
},
Expand Down
4 changes: 4 additions & 0 deletions test/scenarios/mappings/mappings_with_root_fields.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
"input": "$.products[*].name",
"output": "$.items[*].product_name"
},
{
"input": "$.coupon",
"output": "$.items[*].coupon_code"
},
{
"input": "$.products[*].category",
"output": "$.items[*].product_category"
Expand Down
Loading