ondc-code-generator 0.1.4 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/constants/syntax.d.ts +2 -1
- package/dist/constants/syntax.js +1 -0
- package/dist/generator/config-compiler.d.ts +5 -3
- package/dist/generator/config-compiler.js +13 -6
- package/dist/generator/generators/documentation/markdown-message-generator.js +4 -1
- package/dist/services/schema-service.js +1 -5
- package/dist/types/config-types.d.ts +1 -0
- package/dist/utils/config-utils/duplicateVariables.d.ts +2 -0
- package/dist/utils/config-utils/duplicateVariables.js +42 -0
- package/dist/utils/general-utils/string-utils.d.ts +1 -0
- package/dist/utils/general-utils/test-object-utils.d.ts +1 -0
- package/dist/utils/general-utils/test-object-utils.js +7 -0
- package/dist/utils/json-path-utils/paths.js +3 -3
- package/package.json +1 -1
|
@@ -4,7 +4,8 @@ export declare enum TestObjectSyntax {
|
|
|
4
4
|
Scope = "_SCOPE_",
|
|
5
5
|
Continue = "_CONTINUE_",
|
|
6
6
|
ErrorCode = "_ERROR_CODE_",
|
|
7
|
-
SuccessCode = "_SUCCESS_CODE_"
|
|
7
|
+
SuccessCode = "_SUCCESS_CODE_",
|
|
8
|
+
Description = "_DESCRIPTION_"
|
|
8
9
|
}
|
|
9
10
|
export declare const ExternalDataSyntax = "_EXTERNAL";
|
|
10
11
|
export declare enum ConfigSyntax {
|
package/dist/constants/syntax.js
CHANGED
|
@@ -6,6 +6,7 @@ export var TestObjectSyntax;
|
|
|
6
6
|
TestObjectSyntax["Continue"] = "_CONTINUE_";
|
|
7
7
|
TestObjectSyntax["ErrorCode"] = "_ERROR_CODE_";
|
|
8
8
|
TestObjectSyntax["SuccessCode"] = "_SUCCESS_CODE_";
|
|
9
|
+
TestObjectSyntax["Description"] = "_DESCRIPTION_";
|
|
9
10
|
})(TestObjectSyntax || (TestObjectSyntax = {}));
|
|
10
11
|
export const ExternalDataSyntax = "_EXTERNAL";
|
|
11
12
|
export var ConfigSyntax;
|
|
@@ -4,16 +4,18 @@ import { ErrorDefinition } from "../types/error-codes.js";
|
|
|
4
4
|
import { ValidationConfig } from "../types/config-types.js";
|
|
5
5
|
import { SupportedLanguages } from "../types/compiler-types.js";
|
|
6
6
|
type CodeGeneratorConfig = {
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
removeRequiredFromSchema: boolean;
|
|
8
|
+
removeEnumsFromSchema: boolean;
|
|
9
|
+
duplicateVariablesInChildren: boolean;
|
|
9
10
|
};
|
|
10
11
|
export declare class ConfigCompiler {
|
|
11
12
|
buildData: BUILD_TYPE | undefined;
|
|
12
13
|
jsonSchemas: Record<string, JSONSchema7> | undefined;
|
|
13
14
|
possibleJsonPaths: Record<string, string[]> | undefined;
|
|
14
15
|
errorDefinitions: ErrorDefinition[] | undefined;
|
|
16
|
+
generatorConfig: CodeGeneratorConfig | undefined;
|
|
15
17
|
language: SupportedLanguages;
|
|
16
|
-
private
|
|
18
|
+
private SchemaExtractionService;
|
|
17
19
|
constructor(language: SupportedLanguages);
|
|
18
20
|
initialize: (buildYaml: string, generatorConfig?: Partial<CodeGeneratorConfig>) => Promise<void>;
|
|
19
21
|
performValidations: (valConfig: ValidationConfig) => Promise<void>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { loadAndDereferenceYaml } from "../utils/config-utils/yaml.js";
|
|
2
|
-
import { SchemaExtactionService } from "../services/schema-service.js";
|
|
2
|
+
import { SchemaExtactionService as SchemaExtractionService } from "../services/schema-service.js";
|
|
3
3
|
import logger from "../utils/logger.js";
|
|
4
4
|
import { SupportedLanguages } from "../types/compiler-types.js";
|
|
5
5
|
import { TypescriptGenerator } from "./generators/typescript/ts-generator.js";
|
|
@@ -9,20 +9,23 @@ import { readFileSync } from "fs";
|
|
|
9
9
|
import Mustache from "mustache";
|
|
10
10
|
import { fileURLToPath } from "url";
|
|
11
11
|
import path from "path";
|
|
12
|
+
import { duplicateVariablesInChildren } from "../utils/config-utils/duplicateVariables.js";
|
|
12
13
|
const __filename = fileURLToPath(import.meta.url);
|
|
13
14
|
const __dirname = path.dirname(__filename);
|
|
14
15
|
const defaultConfig = {
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
removeRequiredFromSchema: true,
|
|
17
|
+
removeEnumsFromSchema: true,
|
|
18
|
+
duplicateVariablesInChildren: true,
|
|
17
19
|
};
|
|
18
20
|
export class ConfigCompiler {
|
|
19
21
|
constructor(language) {
|
|
20
22
|
// 1. extract build, create schemas , extract possible paths , extract errorcodes
|
|
21
23
|
this.initialize = async (buildYaml, generatorConfig = {}) => {
|
|
22
24
|
const finalConfig = { ...defaultConfig, ...generatorConfig };
|
|
25
|
+
this.generatorConfig = finalConfig;
|
|
23
26
|
this.buildData = await loadAndDereferenceYaml(buildYaml);
|
|
24
|
-
this.jsonSchemas = await this.
|
|
25
|
-
this.possibleJsonPaths = this.
|
|
27
|
+
this.jsonSchemas = await this.SchemaExtractionService.extractSchemas(this.buildData, finalConfig.removeRequiredFromSchema, finalConfig.removeEnumsFromSchema);
|
|
28
|
+
this.possibleJsonPaths = this.SchemaExtractionService.extractPossiblePaths(this.jsonSchemas);
|
|
26
29
|
const errors = this.buildData["x-errorcodes"];
|
|
27
30
|
this.errorDefinitions = errors.code;
|
|
28
31
|
};
|
|
@@ -44,6 +47,10 @@ export class ConfigCompiler {
|
|
|
44
47
|
}
|
|
45
48
|
};
|
|
46
49
|
this.generateCode = async (valConfig, codeName = "L1-Validations") => {
|
|
50
|
+
if (this.generatorConfig?.duplicateVariablesInChildren) {
|
|
51
|
+
console.log("Duplicating variables");
|
|
52
|
+
valConfig = duplicateVariablesInChildren(valConfig);
|
|
53
|
+
}
|
|
47
54
|
await this.performValidations(valConfig);
|
|
48
55
|
// Generate code based on the language
|
|
49
56
|
switch (this.language) {
|
|
@@ -84,6 +91,6 @@ export class ConfigCompiler {
|
|
|
84
91
|
return this.possibleJsonPaths;
|
|
85
92
|
};
|
|
86
93
|
this.language = language;
|
|
87
|
-
this.
|
|
94
|
+
this.SchemaExtractionService = new SchemaExtractionService();
|
|
88
95
|
}
|
|
89
96
|
}
|
|
@@ -2,9 +2,12 @@ import { CompileToMarkdown } from "../../../services/return-complier/ast-functio
|
|
|
2
2
|
import { buildAstFromInput } from "../../../services/return-complier/combined.js";
|
|
3
3
|
import Mustache from "mustache";
|
|
4
4
|
import { addBlockquoteToMarkdown, addTabToMarkdown, ConvertArrayToStringsInTestObject, } from "../../../utils/general-utils/string-utils.js";
|
|
5
|
+
import { TestObjectSyntax } from "../../../constants/syntax.js";
|
|
5
6
|
export function markdownMessageGenerator(returnInput, variableValues, startingPointer, skipInput) {
|
|
6
7
|
const ast = buildAstFromInput(returnInput);
|
|
7
|
-
const returnTemplate =
|
|
8
|
+
const returnTemplate = variableValues[TestObjectSyntax.Description]
|
|
9
|
+
? variableValues[TestObjectSyntax.Description]
|
|
10
|
+
: CompileToMarkdown(ast, startingPointer, 0, false);
|
|
8
11
|
let finalReturn = Mustache.render(returnTemplate, ConvertArrayToStringsInTestObject(variableValues));
|
|
9
12
|
if (skipInput) {
|
|
10
13
|
let skipMarkdown = `Note: **Condition ${startingPointer}** can be skipped if the following conditions are met:`;
|
|
@@ -11,10 +11,6 @@ export class SchemaExtactionService {
|
|
|
11
11
|
for (const targetApi of apis) {
|
|
12
12
|
const existingSchema = paths[`/${targetApi}`].post.requestBody.content["application/json"]
|
|
13
13
|
.schema;
|
|
14
|
-
// output["response"] =
|
|
15
|
-
// paths[`/${targetApi}`].post.responses.default.content[
|
|
16
|
-
// "application/json"
|
|
17
|
-
// ].schema;
|
|
18
14
|
const filtteredSchema = removeRequiredAndEnum(existingSchema, removeEnums, removeRequired);
|
|
19
15
|
output[targetApi] = filtteredSchema;
|
|
20
16
|
}
|
|
@@ -23,7 +19,7 @@ export class SchemaExtactionService {
|
|
|
23
19
|
this.extractPossiblePaths = (schemas) => {
|
|
24
20
|
const paths = {};
|
|
25
21
|
for (const [key, schema] of Object.entries(schemas)) {
|
|
26
|
-
paths[key] = getAllJsonPaths(schema);
|
|
22
|
+
paths[key] = getAllJsonPaths(schema).map((p) => p.replace(/\.([\w-]+\/[\w-]+)(?![\w\]])/g, (_, match) => `['${match}']`));
|
|
27
23
|
}
|
|
28
24
|
return paths;
|
|
29
25
|
};
|
|
@@ -7,6 +7,7 @@ export type TestObject = {
|
|
|
7
7
|
[TestObjectSyntax.ErrorCode]?: number;
|
|
8
8
|
[TestObjectSyntax.SuccessCode]?: number;
|
|
9
9
|
[TestObjectSyntax.Continue]?: string;
|
|
10
|
+
[TestObjectSyntax.Description]?: string;
|
|
10
11
|
[key: string]: ConfigVariable | number | TestObject[] | undefined;
|
|
11
12
|
};
|
|
12
13
|
export type ConfigVariable = string | Primitive[];
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { ConfigSyntax, TestObjectSyntax } from "../../constants/syntax.js";
|
|
2
|
+
import { getVariablesFromTest, mergePathWithScope, } from "../general-utils/test-object-utils.js";
|
|
3
|
+
export function duplicateVariablesInChildren(valConfig) {
|
|
4
|
+
const tests = valConfig[ConfigSyntax.Tests];
|
|
5
|
+
for (const key in tests) {
|
|
6
|
+
const testArray = tests[key];
|
|
7
|
+
for (const test of testArray) {
|
|
8
|
+
duplicateVariables(test, {});
|
|
9
|
+
// console.log(test);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
// console.log(JSON.stringify(valConfig, null, 2));
|
|
13
|
+
return valConfig;
|
|
14
|
+
}
|
|
15
|
+
function duplicateVariables(test, parentVariables) {
|
|
16
|
+
const variables = getVariablesFromTest(test);
|
|
17
|
+
const extractedVariables = {};
|
|
18
|
+
for (const v of variables) {
|
|
19
|
+
const value = test[v];
|
|
20
|
+
if (typeof value === "string") {
|
|
21
|
+
const scope = test[TestObjectSyntax.Scope];
|
|
22
|
+
extractedVariables[v] = mergePathWithScope(value, scope);
|
|
23
|
+
}
|
|
24
|
+
else if (Array.isArray(value)) {
|
|
25
|
+
extractedVariables[v] = value;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
if (Array.isArray(test[TestObjectSyntax.Return])) {
|
|
29
|
+
const returnArray = test[TestObjectSyntax.Return];
|
|
30
|
+
for (const returnTest of returnArray) {
|
|
31
|
+
duplicateVariables(returnTest, {
|
|
32
|
+
...parentVariables,
|
|
33
|
+
...extractedVariables,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
// console.log("adding variables", parentVariables, test);
|
|
39
|
+
Object.assign(test, parentVariables);
|
|
40
|
+
console.log(test);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -22,6 +22,7 @@ export declare function ConvertArrayToStringsInTestObject(testObject: TestObject
|
|
|
22
22
|
_ERROR_CODE_?: number;
|
|
23
23
|
_SUCCESS_CODE_?: number;
|
|
24
24
|
_CONTINUE_?: string;
|
|
25
|
+
_DESCRIPTION_?: string;
|
|
25
26
|
};
|
|
26
27
|
export declare function ConvertArrayToString(arr: any[]): string;
|
|
27
28
|
export declare function addTabToMarkdown(markdown: string): string;
|
|
@@ -10,3 +10,10 @@ export function getVariablesFromTest(testObject) {
|
|
|
10
10
|
}
|
|
11
11
|
return variables;
|
|
12
12
|
}
|
|
13
|
+
export function mergePathWithScope(path, scope) {
|
|
14
|
+
if (scope) {
|
|
15
|
+
const pathWithoutDollar = path.slice(2);
|
|
16
|
+
path = `${scope}.${pathWithoutDollar}`;
|
|
17
|
+
}
|
|
18
|
+
return path;
|
|
19
|
+
}
|
|
@@ -19,8 +19,8 @@ export function replaceBracketsWithAsteriskNested(jsonPath) {
|
|
|
19
19
|
let result = "";
|
|
20
20
|
let i = 0;
|
|
21
21
|
while (i < jsonPath.length) {
|
|
22
|
-
if (jsonPath[i] === "[") {
|
|
23
|
-
// Start of bracketed expression
|
|
22
|
+
if (jsonPath[i] === "[" && jsonPath.substring(i).startsWith("[?(@.")) {
|
|
23
|
+
// Start of query bracketed expression
|
|
24
24
|
let bracketDepth = 1;
|
|
25
25
|
let j = i + 1;
|
|
26
26
|
while (j < jsonPath.length && bracketDepth > 0) {
|
|
@@ -32,7 +32,7 @@ export function replaceBracketsWithAsteriskNested(jsonPath) {
|
|
|
32
32
|
}
|
|
33
33
|
j++;
|
|
34
34
|
}
|
|
35
|
-
// Replace the
|
|
35
|
+
// Replace the query condition with '*'
|
|
36
36
|
result += "[*]";
|
|
37
37
|
i = j; // Move i to after the closing ']'
|
|
38
38
|
}
|