Skip to content

Commit

Permalink
refactor: process all filter
Browse files Browse the repository at this point in the history
  • Loading branch information
koladilip committed Jun 22, 2024
1 parent 535ecd6 commit 6103188
Showing 1 changed file with 47 additions and 39 deletions.
86 changes: 47 additions & 39 deletions src/utils/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,73 +57,81 @@ function processArrayIndexFilter(
return currrentOutputPropAST.value.elements[filterIndex];
}

function isPathWithEmptyPartsAndObjectRoot(expr: Expression) {
return (
expr.type === SyntaxType.PATH &&
expr.parts.length === 0 &&
expr.root?.type === SyntaxType.OBJECT_EXPR
);
}

function getPathExpressionForAllFilter(
currentInputAST: PathExpression,
root: any,
parts: Expression[] = [],
): PathExpression {
return {
type: SyntaxType.PATH,
root,
pathType: currentInputAST.pathType,
inferredPathType: currentInputAST.inferredPathType,
parts,
returnAsArray: true,
} as PathExpression;
}

function validateResultOfAllFilter(objectExpr: Expression, flatMapping: FlatMappingAST) {
if (
objectExpr.type !== SyntaxType.OBJECT_EXPR ||
!objectExpr.props ||
!Array.isArray(objectExpr.props)
) {
throw new JsonTemplateMappingError(
'Invalid mapping: invalid array mapping',
flatMapping.input as string,
flatMapping.output as string,
);
}
}

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

if (filterIndex === -1) {
if (currentOutputPropAST.value.type === SyntaxType.OBJECT_EXPR) {
const currObjectExpr = currentOutputPropAST.value as ObjectExpression;
currentOutputPropAST.value = {
type: SyntaxType.PATH,
root: currObjectExpr,
pathType: currentInputAST.pathType,
inferredPathType: currentInputAST.inferredPathType,
parts: [],
returnAsArray: true,
} as PathExpression;
currentOutputPropAST.value = getPathExpressionForAllFilter(currentInputAST, currObjectExpr);
return currObjectExpr;
}

if (
currentOutputPropAST.value.type === SyntaxType.PATH &&
currentOutputPropAST.value.parts.length === 0 &&
currentOutputPropAST.value.root?.type === SyntaxType.OBJECT_EXPR
) {
if (isPathWithEmptyPartsAndObjectRoot(currentOutputPropAST.value)) {
return currentOutputPropAST.value.root as ObjectExpression;

Check warning on line 113 in src/utils/converter.ts

View check run for this annotation

Codecov / codecov/patch

src/utils/converter.ts#L113

Added line #L113 was not covered by tests
}

Check warning on line 114 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
} else {
const matchedInputParts = currentInputAST.parts.splice(0, filterIndex + 1);
if (
currentOutputPropAST.value.type === SyntaxType.PATH &&
currentOutputPropAST.value.parts.length === 0 &&
currentOutputPropAST.value.root?.type === SyntaxType.OBJECT_EXPR
) {
if (isPathWithEmptyPartsAndObjectRoot(currentOutputPropAST.value)) {
currentOutputPropAST.value = currentOutputPropAST.value.root;
}

if (currentOutputPropAST.value.type !== SyntaxType.PATH) {
matchedInputParts.push(createBlockExpression(currentOutputPropAST.value));
currentOutputPropAST.value = {
type: SyntaxType.PATH,
root: currentInputAST.root,
pathType: currentInputAST.pathType,
inferredPathType: currentInputAST.inferredPathType,
parts: matchedInputParts,
returnAsArray: true,
} as PathExpression;
currentOutputPropAST.value = getPathExpressionForAllFilter(
currentInputAST,
currentInputAST.root,
matchedInputParts,
);
}
currentInputAST.root = undefined;
}

const blockExpr = getLastElement(currentOutputPropAST.value.parts) as Expression;
const objectExpr = blockExpr?.statements?.[0] || EMPTY_EXPR;
if (
objectExpr.type !== SyntaxType.OBJECT_EXPR ||
!objectExpr.props ||
!Array.isArray(objectExpr.props)
) {
throw new JsonTemplateMappingError(
'Invalid mapping: invalid array mapping',
flatMapping.input as string,
flatMapping.output as string,
);
}
validateResultOfAllFilter(objectExpr, flatMapping);
return objectExpr;
}

Expand Down

0 comments on commit 6103188

Please sign in to comment.