Skip to content

Commit

Permalink
feat: merge filters and indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
koladilip committed Jul 1, 2024
1 parent 4181b86 commit 78fbe36
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
30 changes: 20 additions & 10 deletions src/utils/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,22 @@ function addToArrayToExpression(expr: Expression) {
};
}

function mergeObjectFilterParts(existParts: Expression[], newParts: Expression[]) {
for (let index = 0; index < newParts.length; index++) {
if (
newParts[index].type === SyntaxType.OBJECT_FILTER_EXPR &&
existParts[index].type === SyntaxType.OBJECT_FILTER_EXPR
) {
if (newParts[index].options?.index) {

Check warning on line 119 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
existParts[index].options = { ...existParts[index].options, ...newParts[index].options };

Check warning on line 120 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 121 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 121 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 121 in src/utils/converter.ts

View check run for this annotation

Codecov / codecov/patch

src/utils/converter.ts#L120-L121

Added lines #L120 - L121 were not covered by tests
if (newParts[index].filter.type !== SyntaxType.ALL_FILTER_EXPR) {
existParts[index].filter = newParts[index].filter;
}
}
}
}

function handleAllFilterIndexFound(
currentInputAST: Expression,
currentOutputPropAST: ObjectPropExpression,
Expand All @@ -130,6 +146,8 @@ function handleAllFilterIndexFound(
currentInputAST.root,
matchedInputParts,
);
} else {
mergeObjectFilterParts(currentOutputPropAST.value.parts, matchedInputParts);
}
currentInputAST.root = undefined;
}
Expand Down Expand Up @@ -391,10 +409,7 @@ function validateMappingsForIndexVar(flatMapping: FlatMappingAST, indexVar: stri
);
}
const foundIndexVar = flatMapping.inputExpr.parts.some(
(item) =>
item?.type === SyntaxType.OBJECT_FILTER_EXPR &&
item.filter.type === SyntaxType.ALL_FILTER_EXPR &&
item.options?.index === indexVar,
(item) => item?.type === SyntaxType.OBJECT_FILTER_EXPR && item.options?.index === indexVar,
);
if (!foundIndexVar) {
throw new JsonTemplateMappingError(
Expand Down Expand Up @@ -426,10 +441,6 @@ function processFlatMappingParts(flatMapping: FlatMappingAST, objectExpr: Object
}
}

function orderMappings(flatMappingASTs: FlatMappingAST[]): FlatMappingAST[] {
return flatMappingASTs;
}

/**
* Convert Flat to Object Mappings
*/
Expand All @@ -438,8 +449,7 @@ export function convertToObjectMapping(
): ObjectExpression | PathExpression {
const outputAST: ObjectExpression = createObjectExpression();
let pathAST: PathExpression | undefined;
const orderedMappings = orderMappings(flatMappingASTs);
for (const flatMapping of orderedMappings) {
for (const flatMapping of flatMappingASTs) {
validateMapping(flatMapping);
let objectExpr = outputAST;
if (flatMapping.outputExpr.parts.length > 0) {
Expand Down
16 changes: 8 additions & 8 deletions test/scenarios/mappings/all_features.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,39 @@
"output": "$.events[0].name"
},
{
"input": "$.products[?(@.category)].name",
"input": "$.products[*].name",
"output": "$.events[0].items[*].product_name"
},
{
"from": "$.products[?(@.category)].category",
"from": "$.products[*].category",
"to": "$.events[0].items[*].product_category"
},
{
"input": "$.products[?(@.category)].variations[*].size",
"input": "$.products[*].variations[*].size",
"output": "$.events[0].items[*].options[*].s"
},
{
"input": "$.products[?(@.category)].(@.price * @.quantity * (1 - $.discount / 100))",
"input": "$.products[*].(@.price * @.quantity * (1 - $.discount / 100))",
"output": "$.events[0].items[*].value"
},
{
"input": "$.products[?(@.category)].(@.price * @.quantity * (1 - $.discount / 100)).sum()",
"output": "$.events[0].revenue"
},
{
"input": "$.products[?(@.category)].variations[*].length",
"input": "$.products[*].variations[*].length",
"output": "$.events[0].items[*].options[*].l"
},
{
"input": "$.products[?(@.category)].variations[*].width",
"input": "$.products[*].variations[*].width",
"output": "$.events[0].items[*].options[*].w"
},
{
"input": "$.products[?(@.category)].variations[*].color",
"input": "$.products[*].variations[*].color",
"output": "$.events[0].items[*].options[*].c"
},
{
"input": "$.products[?(@.category)].variations[*].height",
"input": "$.products[*].variations[*].height",
"output": "$.events[0].items[*].options[*].h"
}
]

0 comments on commit 78fbe36

Please sign in to comment.