Skip to content

Commit

Permalink
fix: object mappings nested filters
Browse files Browse the repository at this point in the history
  • Loading branch information
koladilip committed Jun 14, 2024
1 parent 8f3df1e commit 3eafe0b
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 78 deletions.
3 changes: 2 additions & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
. "$(dirname -- "$0")/_/husky.sh"

npm test
npx lint-staged
npm run lint:check
npm run lint-staged
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"lint:check": "eslint . || exit 1",
"format": "prettier --write '**/*.ts' '**/*.js' '**/*.json'",
"lint": "npm run format && npm run lint:fix",
"lint-staged": "lint-staged",
"prepare": "husky install",
"jest:scenarios": "jest e2e.test.ts --verbose",
"test:scenario": "jest test/scenario.test.ts --verbose",
Expand Down
12 changes: 7 additions & 5 deletions src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ export class JsonTemplateEngine {
mappings: FlatMappingPaths[],
options?: EngineOptions,
): Expression {
const flatMappingAST = mappings.map((mapping) => ({
...mapping,
inputExpr: JsonTemplateEngine.parse(mapping.input, options).statements[0],
outputExpr: JsonTemplateEngine.parse(mapping.output, options).statements[0],
}));
const flatMappingAST = mappings
.filter((mapping) => mapping.skip !== true)
.map((mapping) => ({
...mapping,
inputExpr: JsonTemplateEngine.parse(mapping.input, options).statements[0],
outputExpr: JsonTemplateEngine.parse(mapping.output, options).statements[0],
}));
return convertToObjectMapping(flatMappingAST);
}

Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ export interface ThrowExpression extends Expression {

export type FlatMappingPaths = {
description?: string;
skip?: boolean;
input: string;
output: string;
};
Expand Down
39 changes: 22 additions & 17 deletions src/utils/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,24 +147,29 @@ function handleNextPart(
partNum: number,
currentOutputPropAST: ObjectPropExpression,
): Expression {
const nextOutputPart = flatMapping.outputExpr.parts[partNum];
if (nextOutputPart.filter?.type === SyntaxType.ALL_FILTER_EXPR) {
return processAllFilter(flatMapping.inputExpr, currentOutputPropAST);
}
if (nextOutputPart.filter?.type === SyntaxType.ARRAY_INDEX_FILTER_EXPR) {
return processArrayIndexFilter(
currentOutputPropAST,
nextOutputPart.filter as IndexFilterExpression,
);
}
if (isWildcardSelector(nextOutputPart)) {
return processWildCardSelector(
flatMapping,
currentOutputPropAST,
partNum === flatMapping.outputExpr.parts.length - 1,
);
let objectExpr = currentOutputPropAST.value;
let newPartNum = partNum;
while (newPartNum < flatMapping.outputExpr.parts.length) {
const nextOutputPart = flatMapping.outputExpr.parts[newPartNum];
if (nextOutputPart.filter?.type === SyntaxType.ALL_FILTER_EXPR) {
objectExpr = processAllFilter(flatMapping.inputExpr, currentOutputPropAST);
} else if (nextOutputPart.filter?.type === SyntaxType.ARRAY_INDEX_FILTER_EXPR) {
objectExpr = processArrayIndexFilter(
currentOutputPropAST,
nextOutputPart.filter as IndexFilterExpression,
);
} else if (isWildcardSelector(nextOutputPart)) {
objectExpr = processWildCardSelector(
flatMapping,
currentOutputPropAST,
newPartNum === flatMapping.outputExpr.parts.length - 1,
);
} else {
break;
}
newPartNum++;
}
return currentOutputPropAST.value;
return objectExpr;
}

function processFlatMappingPart(
Expand Down
9 changes: 3 additions & 6 deletions test/scenario.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,11 @@ describe(`${scenarioName}:`, () => {
const scenarioDir = join(__dirname, 'scenarios', scenarioName);
const scenarios = ScenarioUtils.extractScenarios(scenarioDir);
const scenario: Scenario = scenarios[index] || scenarios[0];
it(`Scenario ${index}: ${Scenario.getTemplatePath(scenario)}`, async () => {
const templatePath = Scenario.getTemplatePath(scenario);
it(`Scenario ${index}: ${templatePath}`, async () => {
let result;
try {
console.log(
`Executing scenario: ${scenarioName}, test: ${index}, template: ${
scenario.templatePath || 'template.jt'
}`,
);
console.log(`Executing scenario: ${scenarioName}, test: ${index}, template: ${templatePath}`);
result = await ScenarioUtils.evaluateScenario(scenarioDir, scenario);
expect(result).toEqual(scenario.output);
} catch (error: any) {
Expand Down
28 changes: 18 additions & 10 deletions test/scenarios/mappings/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,18 @@ export const data: Scenario[] = [
name: 'John Doe',
age: 30,
},
traits2: {
name: {
value: 'John Doe',
traits2: [
{
name: {
value: 'John Doe',
},
},
age: {
value: 30,
{
age: {
value: 30,
},
},
},
],
},
output: {
properties1: {
Expand All @@ -257,10 +261,14 @@ export const data: Scenario[] = [
value: 30,
},
},
properties2: {
name: 'John Doe',
age: 30,
},
properties2: [
{
name: 'John Doe',
},
{
age: 30,
},
],
},
},
{
Expand Down
4 changes: 2 additions & 2 deletions test/scenarios/mappings/object_mappings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"output": "$.properties1.*.value"
},
{
"input": "$.traits2.*.value",
"output": "$.properties2.*"
"input": "$.traits2[*].*.value",
"output": "$.properties2[*].*"
}
]
76 changes: 39 additions & 37 deletions test/test_engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,40 +242,42 @@ const address = {
// ),
// );

console.log(
JsonTemplateEngine.evaluateAsSync(
[
{
description: 'Copies properties of a to root level in the output',
input: '$.a',
output: '$',
},
{
description: 'Combines first and last name in the output',
input: "$.b[*].(@.firstName + ' ' + @.lastName)",
output: '$.items[*].name',
},
{
input: "'buzz'",
output: '$.fizz',
},
],
{ defaultPathType: PathType.JSON },
{
a: {
foo: 1,
bar: 2,
},
b: [
{
firstName: 'foo',
lastName: 'bar',
},
{
firstName: 'fizz',
lastName: 'buzz',
},
],
},
),
);
// console.log(
// JsonTemplateEngine.evaluateAsSync(
// [
// {
// description: 'Copies properties of a to root level in the output',
// input: '$.a',
// output: '$',
// },
// {
// description: 'Combines first and last name in the output',
// input: "$.b[*].(@.firstName + ' ' + @.lastName)",
// output: '$.items[*].name',
// },
// {
// input: "'buzz'",
// output: '$.fizz',
// },
// ],
// { defaultPathType: PathType.JSON },
// {
// a: {
// foo: 1,
// bar: 2,
// },
// b: [
// {
// firstName: 'foo',
// lastName: 'bar',
// },
// {
// firstName: 'fizz',
// lastName: 'buzz',
// },
// ],
// },
// ),
// );

console.log(JsonTemplateEngine.reverseTranslate(JsonTemplateEngine.parse('$.traits.*')));

0 comments on commit 3eafe0b

Please sign in to comment.