Skip to content

Commit

Permalink
fix: root mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
koladilip committed Jun 16, 2024
1 parent e6817e8 commit a1a41b3
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 24 deletions.
63 changes: 39 additions & 24 deletions src/utils/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,17 @@ function processArrayIndexFilter(
}

function processAllFilter(
currentInputAST: PathExpression,
flatMapping: FlatMappingAST,
currentOutputPropAST: ObjectPropExpression,
): Expression {
): ObjectExpression {
const currentInputAST = flatMapping.inputExpr;
const filterIndex = currentInputAST.parts.findIndex(
(part) => part.type === SyntaxType.OBJECT_FILTER_EXPR,
);

if (filterIndex === -1) {
if (currentOutputPropAST.value.type === SyntaxType.OBJECT_EXPR) {
return currentOutputPropAST.value;
return currentOutputPropAST.value as ObjectExpression;
}
} else {
const matchedInputParts = currentInputAST.parts.splice(0, filterIndex + 1);
Expand All @@ -84,7 +85,15 @@ function processAllFilter(
}

const blockExpr = getLastElement(currentOutputPropAST.value.parts) as Expression;
return blockExpr?.statements?.[0] || EMPTY_EXPR;
const objectExpr = blockExpr?.statements?.[0] || EMPTY_EXPR;
if (
objectExpr.type !== SyntaxType.OBJECT_EXPR ||
!objectExpr.props ||
!Array.isArray(objectExpr.props)
) {
throw new Error(`Failed to process output mapping: ${flatMapping.output}`);
}
return objectExpr;
}

function isWildcardSelector(expr: Expression): boolean {
Expand Down Expand Up @@ -146,10 +155,10 @@ function handleNextPart(
flatMapping: FlatMappingAST,
partNum: number,
currentOutputPropAST: ObjectPropExpression,
): Expression | undefined {
): ObjectExpression | undefined {
const nextOutputPart = flatMapping.outputExpr.parts[partNum];
if (nextOutputPart.filter?.type === SyntaxType.ALL_FILTER_EXPR) {
return processAllFilter(flatMapping.inputExpr, currentOutputPropAST);
return processAllFilter(flatMapping, currentOutputPropAST);
}
if (nextOutputPart.filter?.type === SyntaxType.ARRAY_INDEX_FILTER_EXPR) {
return processArrayIndexFilter(
Expand All @@ -170,8 +179,8 @@ function handleNextParts(
flatMapping: FlatMappingAST,
partNum: number,
currentOutputPropAST: ObjectPropExpression,
): Expression {
let objectExpr = currentOutputPropAST.value;
): ObjectExpression {
let objectExpr = currentOutputPropAST.value as ObjectExpression;
let newPartNum = partNum;
while (newPartNum < flatMapping.outputExpr.parts.length) {
const nextObjectExpr = handleNextPart(flatMapping, newPartNum, currentOutputPropAST);
Expand All @@ -184,17 +193,21 @@ function handleNextParts(
return objectExpr;
}

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

function processFlatMappingPart(
flatMapping: FlatMappingAST,
partNum: number,
currentOutputPropsAST: ObjectPropExpression[],
): ObjectPropExpression[] {
const outputPart = flatMapping.outputExpr.parts[partNum];
if (
outputPart.type !== SyntaxType.SELECTOR ||
!outputPart.prop?.value ||
outputPart.prop.value === '*'
) {
if (!isOutputPartRegularSelector(outputPart)) {
return currentOutputPropsAST;
}
const key = outputPart.prop.value;
Expand All @@ -209,15 +222,7 @@ function processFlatMappingPart(
}

const currentOutputPropAST = findOrCreateObjectPropExpression(currentOutputPropsAST, key);

const objectExpr = handleNextParts(flatMapping, partNum + 1, currentOutputPropAST);
if (
objectExpr.type !== SyntaxType.OBJECT_EXPR ||
!objectExpr.props ||
!Array.isArray(objectExpr.props)
) {
throw new Error(`Failed to process output mapping: ${flatMapping.output}`);
}
return objectExpr.props;
}

Expand Down Expand Up @@ -245,17 +250,27 @@ export function convertToObjectMapping(
flatMappingASTs: FlatMappingAST[],
): ObjectExpression | PathExpression {
const outputAST: ObjectExpression = createObjectExpression();
let pathAST: PathExpression | undefined;
for (const flatMapping of flatMappingASTs) {
validateMapping(flatMapping);
let objectExpr = outputAST;
if (flatMapping.outputExpr.parts.length > 0) {
let currentOutputPropsAST = outputAST.props;
if (!isOutputPartRegularSelector(flatMapping.outputExpr.parts[0])) {
const objectPropExpr = {
type: SyntaxType.OBJECT_PROP_EXPR,
key: '',
value: objectExpr as Expression,
};
objectExpr = handleNextParts(flatMapping, 0, objectPropExpr);
pathAST = objectPropExpr.value as PathExpression;
}
let currentOutputPropsAST = objectExpr.props;
for (let i = 0; i < flatMapping.outputExpr.parts.length; i++) {
currentOutputPropsAST = processFlatMappingPart(flatMapping, i, currentOutputPropsAST);
}
} else {
handleRootOnlyOutputMapping(flatMapping, outputAST);
}
}

return outputAST;
return pathAST ?? outputAST;
}
86 changes: 86 additions & 0 deletions test/scenarios/mappings/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,46 @@ export const data: Scenario[] = [
],
},
},
{
mappingsPath: 'root_array_mappings.json',
input: [
{
user_id: 1,
user_name: 'John Doe',
},
{
user_id: 2,
user_name: 'Jane Doe',
},
],
output: [
{
user: {
id: 1,
name: 'John Doe',
},
},
{
user: {
id: 2,
name: 'Jane Doe',
},
},
],
},
{
mappingsPath: 'root_index_mappings.json',
input: {
id: 1,
name: 'John Doe',
},
output: [
{
user_id: 1,
user_name: 'John Doe',
},
],
},
{
mappingsPath: 'root_mappings.json',
input,
Expand All @@ -289,6 +329,52 @@ export const data: Scenario[] = [
timestamp: 1630000000,
},
},
{
mappingsPath: 'root_nested_mappings.json',
input: [
{
user_id: 1,
user_name: 'John Doe',
},
{
user_id: 2,
user_name: 'Jane Doe',
},
],
output: [
{
user_id: {
value: 1,
},
user_name: {
value: 'John Doe',
},
},
{
user_id: {
value: 2,
},
user_name: {
value: 'Jane Doe',
},
},
],
},
{
mappingsPath: 'root_object_mappings.json',
input: {
user_id: 1,
user_name: 'John Doe',
},
output: {
user_id: {
value: 1,
},
user_name: {
value: 'John Doe',
},
},
},
{
input: {
a: [
Expand Down
10 changes: 10 additions & 0 deletions test/scenarios/mappings/root_array_mappings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"input": "$[*].user_id",
"output": "$[*].user.id"
},
{
"input": "$[*].user_name",
"output": "$[*].user.name"
}
]
10 changes: 10 additions & 0 deletions test/scenarios/mappings/root_index_mappings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"input": "$.id",
"output": "$[0].user_id"
},
{
"input": "$.name",
"output": "$[0].user_name"
}
]
6 changes: 6 additions & 0 deletions test/scenarios/mappings/root_nested_mappings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"input": "$[*].*",
"output": "$[*].*.value"
}
]
6 changes: 6 additions & 0 deletions test/scenarios/mappings/root_object_mappings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"input": "$.*",
"output": "$.*.value"
}
]

0 comments on commit a1a41b3

Please sign in to comment.