ondc-code-generator 0.7.3 → 0.7.4
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/alpha/possible-json-paths.json +3734 -0
- package/dist/bin/cli.js +35 -3
- package/dist/constants/syntax.js +26 -0
- package/dist/generator/config-compiler.d.ts +3 -3
- package/dist/generator/config-compiler.js +10 -6
- package/dist/generator/generators/go/templates/json-normalizer.mustache +9 -9
- package/package.json +1 -1
package/dist/bin/cli.js
CHANGED
|
@@ -41,7 +41,7 @@ program
|
|
|
41
41
|
console.log(Cli.description.info("Initializing compiler..."));
|
|
42
42
|
await compiler.initialize(buildYaml);
|
|
43
43
|
console.log(Cli.description.info("Generating validation code..."));
|
|
44
|
-
await compiler.generateValidationFromBuild(functionName, output);
|
|
44
|
+
await compiler.generateValidationFromBuild(functionName, output, true);
|
|
45
45
|
console.log(Cli.description.success(`Validation code generated successfully in ${output} for language ${lang}`));
|
|
46
46
|
}
|
|
47
47
|
catch (error) {
|
|
@@ -57,8 +57,28 @@ program
|
|
|
57
57
|
.option("-o, --output <directory>", "Output directory for generated schema")
|
|
58
58
|
.option("-f, --format <format>", "Output format (json, yaml,typescript)")
|
|
59
59
|
.description("Generate L0 schema")
|
|
60
|
-
.action(async () => {
|
|
61
|
-
console.log("Schema
|
|
60
|
+
.action(async (options) => {
|
|
61
|
+
console.log(Cli.title("Ondc Schema Generator"));
|
|
62
|
+
try {
|
|
63
|
+
const { config, output, format } = options;
|
|
64
|
+
if (!config || !output || !format) {
|
|
65
|
+
console.log(Cli.description.error("Please provide all required options: --config, --output, --format"));
|
|
66
|
+
process.exit(1);
|
|
67
|
+
}
|
|
68
|
+
console.log(Cli.description.info(`Generating L0 schema...`));
|
|
69
|
+
const buildPath = path.resolve(process.cwd(), config);
|
|
70
|
+
console.log(Cli.description.info(`Reading build file from ${buildPath}...`));
|
|
71
|
+
const buildYaml = await fs.readFile(buildPath, "utf-8");
|
|
72
|
+
const compiler = new ConfigCompiler(SupportedLanguages.Typescript);
|
|
73
|
+
await compiler.initialize(buildYaml);
|
|
74
|
+
const formatType = getSchemaFormat(format);
|
|
75
|
+
await compiler.generateL0Schema(output, formatType, true);
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
79
|
+
console.error(Cli.description.error(`Error: ${message}`));
|
|
80
|
+
process.exit(1);
|
|
81
|
+
}
|
|
62
82
|
});
|
|
63
83
|
program.parse();
|
|
64
84
|
function getSupportedLanguage(lang) {
|
|
@@ -75,6 +95,18 @@ function getSupportedLanguage(lang) {
|
|
|
75
95
|
throw new Error(`Unsupported language: ${lang}. Supported languages are: ${getValidLanguageOptions()}`);
|
|
76
96
|
}
|
|
77
97
|
}
|
|
98
|
+
function getSchemaFormat(format) {
|
|
99
|
+
switch (format.toLowerCase()) {
|
|
100
|
+
case "json":
|
|
101
|
+
return "json";
|
|
102
|
+
case "typescript":
|
|
103
|
+
return "typescript";
|
|
104
|
+
case "yaml":
|
|
105
|
+
throw new Error("YAML format is not yet supported");
|
|
106
|
+
default:
|
|
107
|
+
throw new Error(`Unsupported format: ${format}. Supported formats are: json, typescript`);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
78
110
|
function getValidLanguageOptions() {
|
|
79
111
|
return Object.values(SupportedLanguages).join(", ");
|
|
80
112
|
}
|
package/dist/constants/syntax.js
CHANGED
|
@@ -168,4 +168,30 @@ export const ReservedKeywords = new Set([
|
|
|
168
168
|
"match",
|
|
169
169
|
"case",
|
|
170
170
|
"_",
|
|
171
|
+
// golang keywords
|
|
172
|
+
"break",
|
|
173
|
+
"default",
|
|
174
|
+
"func",
|
|
175
|
+
"interface",
|
|
176
|
+
"select",
|
|
177
|
+
"case",
|
|
178
|
+
"defer",
|
|
179
|
+
"go",
|
|
180
|
+
"map",
|
|
181
|
+
"struct",
|
|
182
|
+
"chan",
|
|
183
|
+
"else",
|
|
184
|
+
"goto",
|
|
185
|
+
"package",
|
|
186
|
+
"switch",
|
|
187
|
+
"const",
|
|
188
|
+
"fallthrough",
|
|
189
|
+
"if",
|
|
190
|
+
"range",
|
|
191
|
+
"type",
|
|
192
|
+
"continue",
|
|
193
|
+
"for",
|
|
194
|
+
"import",
|
|
195
|
+
"return",
|
|
196
|
+
"var",
|
|
171
197
|
]);
|
|
@@ -20,9 +20,9 @@ export declare class ConfigCompiler {
|
|
|
20
20
|
initialize: (buildYaml: string, generatorConfig?: Partial<CodeGeneratorConfig>) => Promise<void>;
|
|
21
21
|
performValidations: (valConfig: ValidationConfig) => Promise<void>;
|
|
22
22
|
withMinimalValidations: (valConfig: ValidationConfig) => Promise<void>;
|
|
23
|
-
generateCode: (valConfig: ValidationConfig, codeName?: string, minimal?: boolean, outputPath?: string) => Promise<void>;
|
|
24
|
-
generateL0Schema: (outputPath?: string, type?: "json" | "typescript") => Promise<void>;
|
|
23
|
+
generateCode: (valConfig: ValidationConfig, codeName?: string, minimal?: boolean, outputPath?: string, absolutePath?: boolean) => Promise<void>;
|
|
24
|
+
generateL0Schema: (outputPath?: string, type?: "json" | "typescript", absolutePath?: boolean) => Promise<void>;
|
|
25
25
|
generateValidPaths: () => Promise<Record<string, string[]>>;
|
|
26
|
-
generateValidationFromBuild: (codeName: string, outputPath: string) => Promise<void>;
|
|
26
|
+
generateValidationFromBuild: (codeName: string, outputPath: string, absolutePath?: boolean) => Promise<void>;
|
|
27
27
|
}
|
|
28
28
|
export {};
|
|
@@ -57,7 +57,7 @@ export class ConfigCompiler {
|
|
|
57
57
|
// throw new Error("validation failed");
|
|
58
58
|
};
|
|
59
59
|
// };
|
|
60
|
-
this.generateCode = async (valConfig, codeName = "L1-Validations", minimal = false, outputPath = "./") => {
|
|
60
|
+
this.generateCode = async (valConfig, codeName = "L1-Validations", minimal = false, outputPath = "./", absolutePath = false) => {
|
|
61
61
|
valConfig = JSON.parse(JSON.stringify(valConfig));
|
|
62
62
|
if (this.generatorConfig?.duplicateVariablesInChildren) {
|
|
63
63
|
valConfig = duplicateVariablesInChildren(valConfig);
|
|
@@ -69,7 +69,9 @@ export class ConfigCompiler {
|
|
|
69
69
|
await this.performValidations(valConfig);
|
|
70
70
|
}
|
|
71
71
|
// Generate code based on the language
|
|
72
|
-
const targetPath =
|
|
72
|
+
const targetPath = absolutePath
|
|
73
|
+
? outputPath
|
|
74
|
+
: `${outputPath}generated/${codeName}`;
|
|
73
75
|
switch (this.language) {
|
|
74
76
|
case SupportedLanguages.Typescript:
|
|
75
77
|
await new TypescriptGenerator(valConfig, this.errorDefinitions ?? [], targetPath).generateCode({
|
|
@@ -95,11 +97,13 @@ export class ConfigCompiler {
|
|
|
95
97
|
throw new Error("Language not supported");
|
|
96
98
|
}
|
|
97
99
|
};
|
|
98
|
-
this.generateL0Schema = async (outputPath = "./", type = "typescript") => {
|
|
100
|
+
this.generateL0Schema = async (outputPath = "./", type = "typescript", absolutePath = false) => {
|
|
99
101
|
if (!this.jsonSchemas) {
|
|
100
102
|
throw new Error("Schemas not initialized");
|
|
101
103
|
}
|
|
102
|
-
const targetPath =
|
|
104
|
+
const targetPath = absolutePath
|
|
105
|
+
? outputPath
|
|
106
|
+
: `${outputPath}generated/L0-schemas/`;
|
|
103
107
|
for (const schema in this.jsonSchemas) {
|
|
104
108
|
const json = this.jsonSchemas[schema];
|
|
105
109
|
if (type === "typescript") {
|
|
@@ -129,13 +133,13 @@ export class ConfigCompiler {
|
|
|
129
133
|
// );
|
|
130
134
|
return this.possibleJsonPaths;
|
|
131
135
|
};
|
|
132
|
-
this.generateValidationFromBuild = async (codeName, outputPath) => {
|
|
136
|
+
this.generateValidationFromBuild = async (codeName, outputPath, absolutePath = false) => {
|
|
133
137
|
if (!this.buildData)
|
|
134
138
|
throw new Error("Build data not initialized");
|
|
135
139
|
const valConfig = this.buildData["x-validations"];
|
|
136
140
|
if (!valConfig)
|
|
137
141
|
throw new Error("No validation config found in build data");
|
|
138
|
-
await this.generateCode(valConfig, codeName, false, outputPath);
|
|
142
|
+
await this.generateCode(valConfig, codeName, false, outputPath, absolutePath);
|
|
139
143
|
};
|
|
140
144
|
this.language = language;
|
|
141
145
|
this.SchemaExtractionService = new SchemaExtractionService();
|
|
@@ -3,13 +3,13 @@
|
|
|
3
3
|
package validationutils
|
|
4
4
|
|
|
5
5
|
import (
|
|
6
|
-
"
|
|
6
|
+
"github.com/bytedance/sonic"
|
|
7
7
|
)
|
|
8
8
|
|
|
9
9
|
// NormalizeKeys normalizes JSON structures so that:
|
|
10
|
-
//
|
|
11
|
-
//
|
|
12
|
-
//
|
|
10
|
+
// - All objects with the same property name share the union of keys seen anywhere
|
|
11
|
+
// - All objects inside the same array share the union of keys at that array level
|
|
12
|
+
// - Missing keys are filled with nil
|
|
13
13
|
func NormalizeKeys(input interface{}) interface{} {
|
|
14
14
|
// Step 1: Collect templates by property name
|
|
15
15
|
templatesByPropName := make(map[string]map[string]struct{})
|
|
@@ -67,12 +67,12 @@ func applyTemplates(node interface{}, templates map[string]map[string]struct{})
|
|
|
67
67
|
if obj, ok := item.(map[string]interface{}); ok {
|
|
68
68
|
// Create new object with array union keys
|
|
69
69
|
next := make(map[string]interface{})
|
|
70
|
-
|
|
70
|
+
|
|
71
71
|
// Copy existing keys
|
|
72
72
|
for k, val := range obj {
|
|
73
73
|
next[k] = val
|
|
74
74
|
}
|
|
75
|
-
|
|
75
|
+
|
|
76
76
|
// Add missing keys from array union
|
|
77
77
|
for key := range arrayUnion {
|
|
78
78
|
if _, exists := next[key]; !exists {
|
|
@@ -141,14 +141,14 @@ func fillFromTemplate(propName string, obj map[string]interface{}, templates map
|
|
|
141
141
|
return filled
|
|
142
142
|
}
|
|
143
143
|
|
|
144
|
-
// DeepCloneJSON creates a deep clone of a JSON-serializable structure
|
|
144
|
+
// DeepCloneJSON creates a deep clone of a JSON-serializable structure using sonic
|
|
145
145
|
func DeepCloneJSON(v interface{}) interface{} {
|
|
146
|
-
b, err :=
|
|
146
|
+
b, err := sonic.Marshal(v)
|
|
147
147
|
if err != nil {
|
|
148
148
|
panic(err) // or handle error
|
|
149
149
|
}
|
|
150
150
|
var out interface{}
|
|
151
|
-
if err :=
|
|
151
|
+
if err := sonic.Unmarshal(b, &out); err != nil {
|
|
152
152
|
panic(err)
|
|
153
153
|
}
|
|
154
154
|
return out
|