diff --git a/src/utils/converter.ts b/src/utils/converter.ts index 4019452..544c641 100644 --- a/src/utils/converter.ts +++ b/src/utils/converter.ts @@ -57,11 +57,48 @@ 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, ); @@ -69,61 +106,32 @@ function processAllFilter( 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; } } 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; }