|
1 | | -import assert from "node:assert"; |
2 | 1 | import { describe, expect, test } from "vitest"; |
3 | 2 | import { cleanParsedSchemaProperty, type TypeSchema } from "@nlighten/json-schema-utils"; |
4 | 3 | import { EmbeddedTransformerFunction, EmbeddedTransformerFunctions } from "../functions/types"; |
5 | 4 | import { parseTransformer } from "../parse"; |
6 | | -import { functions } from "../functions/functions"; |
7 | | -import { |
8 | | - CsvParseFunctionArgBasedSchemas, |
9 | | - DateFunctionArgBasedSchemas, |
10 | | - DigestFunctionArgBasedSchemas, |
11 | | -} from "../functions/embeddedFunctions"; |
| 5 | +import { functionsParser } from "../functions/functionsParser"; |
12 | 6 |
|
13 | 7 | const transformerResult = ( |
14 | 8 | transformer: Record<string, string | Record<string, any>>, |
@@ -44,7 +38,7 @@ const NULL: TypeSchema = { type: "null" }, |
44 | 38 |
|
45 | 39 | describe("functions schema detection", () => { |
46 | 40 | for (const funcName of EmbeddedTransformerFunctions) { |
47 | | - const func = functions.get(funcName); |
| 41 | + const func = functionsParser.get(funcName); |
48 | 42 | const alias = `$$${funcName}`; |
49 | 43 | const outputSchema = func.outputSchema; |
50 | 44 |
|
@@ -613,64 +607,65 @@ describe("functions schema detection", () => { |
613 | 607 | ); |
614 | 608 | break; |
615 | 609 | } |
616 | | - // static arg based |
617 | | - case EmbeddedTransformerFunction.csvparse: |
618 | | - case EmbeddedTransformerFunction.digest: |
619 | | - case EmbeddedTransformerFunction.date: { |
620 | | - const paramName = func.arguments?.find(p => p.position === 0)?.name ?? ""; |
621 | | - const expected: Record<string, TypeSchema> = {}; |
622 | | - const outputProperties: Record<string, TypeSchema> = {}; |
623 | | - const schemas = |
624 | | - funcName === EmbeddedTransformerFunction.date |
625 | | - ? DateFunctionArgBasedSchemas |
626 | | - : funcName === EmbeddedTransformerFunction.digest |
627 | | - ? DigestFunctionArgBasedSchemas |
628 | | - : funcName === EmbeddedTransformerFunction.csvparse |
629 | | - ? CsvParseFunctionArgBasedSchemas |
630 | | - : {}; |
631 | | - const transformer = Object.entries(schemas).reduce((a, kv) => { |
632 | | - a["inline_" + kv[0]] = `${alias}(${kv[0]}):irrelevant`; |
633 | | - a["object_" + kv[0]] = { |
634 | | - [alias]: "irrelevant", |
635 | | - [paramName]: kv[0], |
636 | | - }; |
637 | | - if (kv[1].outputSchema) { |
638 | | - expected["$.inline_" + kv[0]] = kv[1].outputSchema; |
639 | | - outputProperties["inline_" + kv[0]] = kv[1].outputSchema; |
640 | | - expected["$.object_" + kv[0]] = kv[1].outputSchema; |
641 | | - outputProperties["object_" + kv[0]] = kv[1].outputSchema; |
642 | | - if (kv[1].outputSchema.type === "array" && kv[1].outputSchema.items) { |
643 | | - const firstLevelItems = kv[1].outputSchema.items as TypeSchema; |
644 | | - expected["$.inline_" + kv[0] + "[]"] = firstLevelItems; |
645 | | - expected["$.object_" + kv[0] + "[]"] = firstLevelItems; |
646 | | - if (firstLevelItems.type === "array" && firstLevelItems.items) { |
647 | | - expected["$.inline_" + kv[0] + "[][]"] = firstLevelItems.items as TypeSchema; |
648 | | - expected["$.object_" + kv[0] + "[][]"] = firstLevelItems.items as TypeSchema; |
| 610 | + // static output schema |
| 611 | + default: { |
| 612 | + if (func.overrides) { |
| 613 | + // static conditional |
| 614 | + const paramName = func.arguments?.find(p => p.position === 0)?.name ?? ""; |
| 615 | + const expected: Record<string, TypeSchema> = {}; |
| 616 | + const outputProperties: Record<string, TypeSchema> = {}; |
| 617 | + const transformer = func.overrides.reduce((a, kv) => { |
| 618 | + const argValue = kv.if[0].equals; // TODO: this is not generic |
| 619 | + a["inline_" + argValue] = `${alias}(${argValue}):irrelevant`; |
| 620 | + a["object_" + argValue] = { |
| 621 | + [alias]: "irrelevant", |
| 622 | + [paramName]: argValue, |
| 623 | + }; |
| 624 | + const argOutputSchema = kv.then.outputSchema; |
| 625 | + if (argOutputSchema) { |
| 626 | + expected["$.inline_" + argValue] = argOutputSchema; |
| 627 | + outputProperties["inline_" + argValue] = argOutputSchema; |
| 628 | + expected["$.object_" + argValue] = argOutputSchema; |
| 629 | + outputProperties["object_" + argValue] = argOutputSchema; |
| 630 | + if ( |
| 631 | + argOutputSchema.type === "array" && |
| 632 | + argOutputSchema.items && |
| 633 | + !Array.isArray(argOutputSchema.items) |
| 634 | + ) { |
| 635 | + const firstLevelItems = argOutputSchema.items; |
| 636 | + expected["$.inline_" + argValue + "[]"] = firstLevelItems; |
| 637 | + expected["$.object_" + argValue + "[]"] = firstLevelItems; |
| 638 | + if ( |
| 639 | + firstLevelItems.type === "array" && |
| 640 | + firstLevelItems.items && |
| 641 | + !Array.isArray(firstLevelItems.items) |
| 642 | + ) { |
| 643 | + expected["$.inline_" + argValue + "[][]"] = firstLevelItems.items; |
| 644 | + expected["$.object_" + argValue + "[][]"] = firstLevelItems.items; |
| 645 | + } |
649 | 646 | } |
650 | 647 | } |
651 | | - } |
652 | | - return a; |
653 | | - }, {} as any); |
654 | | - expect(transformerResult(transformer)).toStrictEqual( |
655 | | - createFlowTraversalResult({ |
656 | | - paths: { |
657 | | - $: { |
658 | | - additionalProperties: false, |
659 | | - properties: outputProperties, |
660 | | - type: "object", |
| 648 | + return a; |
| 649 | + }, {} as any); |
| 650 | + expect(transformerResult(transformer)).toStrictEqual( |
| 651 | + createFlowTraversalResult({ |
| 652 | + paths: { |
| 653 | + $: { |
| 654 | + additionalProperties: false, |
| 655 | + properties: outputProperties, |
| 656 | + type: "object", |
| 657 | + }, |
| 658 | + ...expected, |
661 | 659 | }, |
662 | | - ...expected, |
663 | | - }, |
664 | | - }), |
665 | | - ); |
666 | | - break; |
667 | | - } |
668 | | - // static output schema |
669 | | - default: { |
670 | | - assert(outputSchema); |
671 | | - assert(!func.argBased, "missing object arg based check"); |
| 660 | + }), |
| 661 | + ); |
| 662 | + break; |
| 663 | + } |
| 664 | + |
| 665 | + // static output schema |
| 666 | + expect(outputSchema).toBeTruthy(); |
672 | 667 | const expected: Record<string, TypeSchema> = {}; |
673 | | - const funcOutputPaths = functions.get(funcName).parsedOutputSchema?.paths ?? []; |
| 668 | + const funcOutputPaths = functionsParser.get(funcName).parsedOutputSchema?.paths ?? []; |
674 | 669 |
|
675 | 670 | funcOutputPaths.forEach(p => { |
676 | 671 | const suffix = !p.$path ? "" : (p.$path[0] === "[" ? "" : ".") + p.$path; |
|
0 commit comments