-
-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathmodels.ts
367 lines (351 loc) · 13.1 KB
/
models.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import { CSharpFileGenerator, JavaFileGenerator, JavaScriptFileGenerator, TypeScriptFileGenerator, GoFileGenerator, Logger, DartFileGenerator, PythonFileGenerator, RustFileGenerator, TS_COMMON_PRESET, TS_JSONBINPACK_PRESET, CSHARP_DEFAULT_PRESET, CSHARP_NEWTONSOFT_SERIALIZER_PRESET, CSHARP_COMMON_PRESET, CSHARP_JSON_SERIALIZER_PRESET, KotlinFileGenerator, TS_DESCRIPTION_PRESET, PhpFileGenerator, CplusplusFileGenerator, JAVA_CONSTRAINTS_PRESET, JAVA_JACKSON_PRESET, JAVA_COMMON_PRESET, JAVA_DESCRIPTION_PRESET } from '@asyncapi/modelina';
import { Flags } from '@oclif/core';
import { ConvertDocumentParserAPIVersion } from '@smoya/multi-parser';
import Command from '../../base';
import { load } from '../../models/SpecificationFile';
import { formatOutput, parse, validationFlags } from '../../parser';
import type { AbstractGenerator, AbstractFileGenerator } from '@asyncapi/modelina';
enum Languages {
typescript = 'typescript',
csharp = 'csharp',
golang = 'golang',
java = 'java',
javascript = 'javascript',
dart = 'dart',
python = 'python',
rust = 'rust',
kotlin='kotlin',
php='php',
cplusplus='cplusplus'
}
const possibleLanguageValues = Object.values(Languages).join(', ');
export default class Models extends Command {
static description = 'Generates typed models';
static args = [
{
name: 'language',
description: 'The language you want the typed models generated for.',
options: Object.keys(Languages),
required: true
},
{ name: 'file', description: 'Path or URL to the AsyncAPI document, or context-name', required: true },
];
static flags = {
help: Flags.help({ char: 'h' }),
output: Flags.string({
char: 'o',
description: 'The output directory where the models should be written to. Omitting this flag will write the models to `stdout`.',
required: false
}),
/**
* TypeScript specific options
*/
tsModelType: Flags.string({
type: 'option',
options: ['class', 'interface'],
description: 'TypeScript specific, define which type of model needs to be generated.',
required: false,
default: 'class',
}),
tsEnumType: Flags.string({
type: 'option',
options: ['enum', 'union'],
description: 'TypeScript specific, define which type of enums needs to be generated.',
required: false,
default: 'enum',
}),
tsModuleSystem: Flags.string({
type: 'option',
options: ['ESM', 'CJS'],
description: 'TypeScript specific, define the module system to be used.',
required: false,
default: 'ESM',
}),
tsIncludeComments: Flags.boolean({
description: 'TypeScript specific, if enabled add comments while generating models.',
required: false,
default: false,
}),
tsExportType: Flags.string({
type: 'option',
options: ['default', 'named'],
description: 'TypeScript specific, define which type of export needs to be generated.',
required: false,
default: 'default',
}),
tsJsonBinPack: Flags.boolean({
description: 'TypeScript specific, define basic support for serializing to and from binary with jsonbinpack.',
required: false,
default: false,
}),
tsMarshalling: Flags.boolean({
description: 'TypeScript specific, generate the models with marshalling functions.',
required: false,
default: false,
}),
tsExampleInstance: Flags.boolean({
description: 'Typescript specific, generate example of the model',
required: false,
default: false,
}),
/**
* Go and Java specific package name to use for the generated models
*/
packageName: Flags.string({
description: 'Go, Java and Kotlin specific, define the package to use for the generated models. This is required when language is `go`, `java` or `kotlin`.',
required: false
}),
/**
* Java specific options
*/
javaIncludeComments: Flags.boolean({
description: 'Java specific, if enabled add comments while generating models.',
required: false,
default: false
}),
javaJackson: Flags.boolean({
description: 'Java specific, generate the models with Jackson serialization support',
required: false,
default: false
}),
javaConstraints: Flags.boolean({
description: 'Java specific, generate the models with constraints',
required: false,
default: false
}),
/**
* C++ and C# and PHP specific namespace to use for the generated models
*/
namespace: Flags.string({
description: 'C#, C++ and PHP specific, define the namespace to use for the generated models. This is required when language is `csharp`,`c++` or `php`.',
required: false
}),
/**
* C# specific options
*/
csharpAutoImplement: Flags.boolean({
description: 'C# specific, define whether to generate auto-implemented properties or not.',
required: false,
default: false
}),
csharpNewtonsoft: Flags.boolean({
description: 'C# specific, generate the models with newtonsoft serialization support',
required: false,
default: false
}),
csharpArrayType: Flags.string({
type: 'option',
description: 'C# specific, define which type of array needs to be generated.',
options: ['Array', 'List'],
required: false,
default: 'Array'
}),
csharpHashcode: Flags.boolean({
description: 'C# specific, generate the models with the GetHashCode method overwritten',
required: false,
default: false
}),
csharpEqual: Flags.boolean({
description: 'C# specific, generate the models with the Equal method overwritten',
required: false,
default: false
}),
csharpSystemJson: Flags.boolean({
description: 'C# specific, generate the models with System.Text.Json serialization support',
required: false,
default: false
}),
...validationFlags({ logDiagnostics: false }),
};
/* eslint-disable sonarjs/cognitive-complexity */
async run() {
const { args, flags } = await this.parse(Models);
const { tsModelType, tsEnumType, tsIncludeComments, tsModuleSystem, tsExportType, tsJsonBinPack, tsMarshalling, tsExampleInstance, namespace, csharpAutoImplement, csharpArrayType, csharpNewtonsoft, csharpHashcode, csharpEqual, csharpSystemJson, packageName, javaIncludeComments, javaJackson, javaConstraints, output } = flags;
const { language, file } = args;
const inputFile = (await load(file)) || (await load());
if (inputFile.isAsyncAPI3()) {
this.error('Generate Models command does not support AsyncAPI v3 yet, please checkout https://github.com/asyncapi/modelina/issues/1376');
}
const { document, diagnostics ,status } = await parse(this, inputFile, flags);
if (!document || status === 'invalid') {
const severityErrors = diagnostics.filter((obj) => obj.severity === 0);
this.log(`Input is not a correct AsyncAPI document so it cannot be processed.${formatOutput(severityErrors,'stylish','error')}`);
return;
}
// Modelina, atm, is not using @asyncapi/parser@v3.x but @asyncapi/parser@v2.x, so it still uses Parser-API v1.0.0.
// This call converts the parsed document object using @asyncapi/parser@v3.x (Parser-API v2) to a document compatible with the Parser-API version in use in @asyncapi/parser@v2.x (v1)
// This is needed until https://github.com/asyncapi/modelina/issues/1493 gets fixed.
const convertedDoc = ConvertDocumentParserAPIVersion(document.json(), 1);
Logger.setLogger({
info: (message) => {
this.log(message);
},
debug: (message) => {
this.debug(message);
},
warn: (message) => {
this.warn(message);
},
error: (message) => {
this.error(message);
},
});
let fileGenerator: AbstractGenerator<any, any> & AbstractFileGenerator<any>;
let fileOptions: any = {};
const presets = [];
const options = {
marshalling: tsMarshalling,
example: tsExampleInstance,
};
switch (language) {
case Languages.typescript:
presets.push({
preset: TS_COMMON_PRESET,
options
});
if (tsIncludeComments) {presets.push(TS_DESCRIPTION_PRESET);}
if (tsJsonBinPack) {
presets.push({
preset: TS_COMMON_PRESET,
options
},
TS_JSONBINPACK_PRESET);
}
fileGenerator = new TypeScriptFileGenerator({
modelType: tsModelType as 'class' | 'interface',
enumType: tsEnumType as 'enum' | 'union',
presets
});
fileOptions = {
moduleSystem: tsModuleSystem,
exportType: tsExportType
};
break;
case Languages.python:
fileGenerator = new PythonFileGenerator();
break;
case Languages.rust:
fileGenerator = new RustFileGenerator();
break;
case Languages.csharp:
if (namespace === undefined) {
throw new Error('In order to generate models to C#, we need to know which namespace they are under. Add `--namespace=NAMESPACE` to set the desired namespace.');
}
if (csharpAutoImplement) {
presets.push({
preset: CSHARP_DEFAULT_PRESET,
options: {
autoImplementedProperties: true
}
});
}
if (csharpNewtonsoft) {
presets.push(CSHARP_NEWTONSOFT_SERIALIZER_PRESET);
}
if (csharpSystemJson) {
presets.push(CSHARP_JSON_SERIALIZER_PRESET);
}
if (csharpHashcode || csharpEqual) {
presets.push({
preset: CSHARP_COMMON_PRESET,
options: {
hashCode: csharpHashcode,
equals: csharpEqual
}
});
}
fileGenerator = new CSharpFileGenerator({
presets,
collectionType: csharpArrayType as 'Array' | 'List'
});
fileOptions = {
namespace
};
break;
case Languages.cplusplus:
if (namespace === undefined) {
throw new Error('In order to generate models to C++, we need to know which namespace they are under. Add `--namespace=NAMESPACE` to set the desired namespace.');
}
fileGenerator = new CplusplusFileGenerator({
namespace
});
break;
case Languages.golang:
if (packageName === undefined) {
throw new Error('In order to generate models to Go, we need to know which package they are under. Add `--packageName=PACKAGENAME` to set the desired package name.');
}
fileGenerator = new GoFileGenerator();
fileOptions = {
packageName
};
break;
case Languages.java:
if (packageName === undefined) {
throw new Error('In order to generate models to Java, we need to know which package they are under. Add `--packageName=PACKAGENAME` to set the desired package name.');
}
presets.push({
preset: JAVA_COMMON_PRESET,
options
});
if (javaIncludeComments) {presets.push(JAVA_DESCRIPTION_PRESET);}
if (javaJackson) {presets.push(JAVA_JACKSON_PRESET);}
if (javaConstraints) {presets.push(JAVA_CONSTRAINTS_PRESET);}
fileGenerator = new JavaFileGenerator({ presets });
fileOptions = {
packageName
};
break;
case Languages.javascript:
fileGenerator = new JavaScriptFileGenerator();
break;
case Languages.dart:
if (packageName === undefined) {
throw new Error('In order to generate models to Dart, we need to know which package they are under. Add `--packageName=PACKAGENAME` to set the desired package name.');
}
fileGenerator = new DartFileGenerator();
fileOptions = {
packageName
};
break;
case Languages.kotlin:
if (packageName === undefined) {
throw new Error('In order to generate models to Kotlin, we need to know which package they are under. Add `--packageName=PACKAGENAME` to set the desired package name.');
}
fileGenerator = new KotlinFileGenerator();
fileOptions = {
packageName
};
break;
case Languages.php:
if (namespace === undefined) {
throw new Error('In order to generate models to PHP, we need to know which namespace they are under. Add `--namespace=NAMESPACE` to set the desired namespace.');
}
fileGenerator = new PhpFileGenerator();
fileOptions = {
namespace
};
break;
default:
throw new Error(`Could not determine generator for language ${language}, are you using one of the following values ${possibleLanguageValues}?`);
}
if (output) {
const models = await fileGenerator.generateToFiles(
convertedDoc as any,
output,
{ ...fileOptions, });
const generatedModels = models.map((model) => { return model.modelName; });
this.log(`Successfully generated the following models: ${generatedModels.join(', ')}`);
return;
}
const models = await fileGenerator.generateCompleteModels(
convertedDoc as any,
{ ...fileOptions });
const generatedModels = models.map((model) => {
return `
## Model name: ${model.modelName}
${model.result}
`;
});
this.log(`Successfully generated the following models: ${generatedModels.join('\n')}`);
}
}