forked from asyncapi/modelina
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractGenerator.ts
305 lines (279 loc) · 9.19 KB
/
AbstractGenerator.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
import {
InputMetaModel,
OutputModel,
Preset,
Presets,
RenderOutput,
ProcessorOptions,
MetaModel,
ConstrainedMetaModel,
UnionModel,
ConstrainedUnionModel
} from '../models';
import { InputProcessor } from '../processors';
import { IndentationTypes } from '../helpers';
import { DeepPartial, isPresetWithOptions } from '../utils';
import { AbstractDependencyManager } from './AbstractDependencyManager';
export interface CommonGeneratorOptions<
P extends Preset = Preset,
DependencyManager extends
AbstractDependencyManager = AbstractDependencyManager
> {
indentation?: {
type: IndentationTypes;
size: number;
};
defaultPreset?: P;
presets?: Presets<P>;
processorOptions?: ProcessorOptions;
/**
* This dependency manager type serves two functions.
* 1. It can be used to provide a factory for generate functions
* 2. It can be used to provide a single instance of a dependency manager, to add all dependencies together
*
* This depends on context and where it's used.
*/
dependencyManager?: (() => DependencyManager) | DependencyManager;
}
export const defaultGeneratorOptions: CommonGeneratorOptions = {
indentation: {
type: IndentationTypes.SPACES,
size: 2
}
};
export interface AbstractGeneratorRenderArgs<
Options extends CommonGeneratorOptions,
ConstrainedModel = ConstrainedMetaModel
> {
constrainedModel: ConstrainedModel;
inputModel: InputMetaModel;
options?: DeepPartial<Options>;
}
export interface AbstractGeneratorRenderCompleteModelArgs<
Options extends CommonGeneratorOptions,
RenderCompleteModelOptions
> {
constrainedModel: ConstrainedMetaModel;
inputModel: InputMetaModel;
completeOptions: Partial<RenderCompleteModelOptions>;
options?: DeepPartial<Options>;
}
/**
* Abstract generator which must be implemented by each language
*/
export abstract class AbstractGenerator<
Options extends CommonGeneratorOptions,
RenderCompleteModelOptions
> {
constructor(
public readonly languageName: string,
public readonly options: Options
) {}
public abstract render(
args: AbstractGeneratorRenderArgs<Options>
): Promise<RenderOutput>;
public abstract renderCompleteModel(
args: AbstractGeneratorRenderCompleteModelArgs<
Options,
RenderCompleteModelOptions
>
): Promise<RenderOutput>;
public abstract constrainToMetaModel(
model: MetaModel,
options: DeepPartial<Options>
): ConstrainedMetaModel;
public abstract getDependencyManager(
options: Options
): AbstractDependencyManager;
public abstract splitMetaModel(model: MetaModel): MetaModel[];
public process(input: Record<string, unknown>): Promise<InputMetaModel> {
return InputProcessor.processor.process(
input,
this.options.processorOptions
);
}
/**
* This function returns an instance of the dependency manager which is either a factory or an instance.
*/
protected getDependencyManagerInstance(
options: Options
): AbstractDependencyManager {
if (options.dependencyManager === undefined) {
throw new Error(
'Internal error, could not find dependency manager instance'
);
}
if (typeof options.dependencyManager === 'function') {
return options.dependencyManager();
}
return options.dependencyManager;
}
/**
* Generates an array of ConstrainedMetaModel with its dependency manager from an InputMetaModel.
* It also adds parents to the ConstrainedMetaModel's which can be used in renderers which needs to know what parents they belong to.
*/
private getConstrainedModels(inputModel: InputMetaModel): Array<{
constrainedModel: ConstrainedMetaModel;
dependencyManager: AbstractDependencyManager;
}> {
interface ConstrainedMetaModelWithDepManager {
constrainedModel: ConstrainedMetaModel;
dependencyManager: AbstractDependencyManager;
}
const getConstrainedMetaModelWithDepManager = (
model: MetaModel
): ConstrainedMetaModelWithDepManager => {
const dependencyManager = this.getDependencyManager(this.options);
const constrainedModel = this.constrainToMetaModel(model, {
dependencyManager
} as DeepPartial<Options>);
return {
constrainedModel,
dependencyManager
};
};
const unionConstrainedModelsWithDepManager: ConstrainedMetaModelWithDepManager[] =
[];
const constrainedModelsWithDepManager: ConstrainedMetaModelWithDepManager[] =
[];
for (const model of Object.values(inputModel.models)) {
if (model instanceof UnionModel) {
unionConstrainedModelsWithDepManager.push(
getConstrainedMetaModelWithDepManager(model)
);
continue;
}
constrainedModelsWithDepManager.push(
getConstrainedMetaModelWithDepManager(model)
);
}
for (const { constrainedModel } of constrainedModelsWithDepManager) {
for (const unionConstrainedModel of unionConstrainedModelsWithDepManager) {
if (
unionConstrainedModel.constrainedModel instanceof
ConstrainedUnionModel &&
unionConstrainedModel.constrainedModel.union.some(
(m) =>
m.name === constrainedModel.name &&
m.type === constrainedModel.type
)
) {
if (!constrainedModel.options.parents) {
constrainedModel.options.parents = [];
}
constrainedModel.options.parents.push(
unionConstrainedModel.constrainedModel
);
}
}
}
return [
...unionConstrainedModelsWithDepManager,
...constrainedModelsWithDepManager
];
}
/**
* Generates the full output of a model, instead of a scattered model.
*
* OutputModels result is no longer the model itself, but including package, package dependencies and model dependencies.
*
*/
public async generateCompleteModels(
input: any | InputMetaModel,
completeOptions: Partial<RenderCompleteModelOptions>
): Promise<OutputModel[]> {
const inputModel = await this.processInput(input);
return Promise.all(
this.getConstrainedModels(inputModel).map(
async ({ constrainedModel, dependencyManager }) => {
const renderedOutput = await this.renderCompleteModel({
constrainedModel,
inputModel,
completeOptions,
options: { dependencyManager } as DeepPartial<Options>
});
return OutputModel.toOutputModel({
result: renderedOutput.result,
modelName: renderedOutput.renderedName,
dependencies: renderedOutput.dependencies,
model: constrainedModel,
inputModel
});
}
)
);
}
/**
* Generates a scattered model where dependencies and rendered results are separated.
*/
public async generate(input: any | InputMetaModel): Promise<OutputModel[]> {
const inputModel = await this.processInput(input);
return Promise.all(
this.getConstrainedModels(inputModel).map(
async ({ constrainedModel, dependencyManager }) => {
const renderedOutput = await this.render({
constrainedModel,
inputModel,
options: {
dependencyManager
} as DeepPartial<Options>
});
return OutputModel.toOutputModel({
result: renderedOutput.result,
modelName: renderedOutput.renderedName,
dependencies: renderedOutput.dependencies,
model: constrainedModel,
inputModel
});
}
)
);
}
/**
* Process any of the input formats to the appropriate InputMetaModel type and split out the meta models
* based on the requirements of the generators
*
* @param input
*/
protected async processInput(
input: any | InputMetaModel
): Promise<InputMetaModel> {
const rawInputModel =
input instanceof InputMetaModel ? input : await this.process(input);
//Split out the models based on the language specific requirements of which models is rendered separately
const splitOutModels: { [key: string]: MetaModel } = {};
for (const model of Object.values(rawInputModel.models)) {
const splitModels = this.splitMetaModel(model);
for (const splitModel of splitModels) {
splitOutModels[splitModel.name] = splitModel;
}
}
rawInputModel.models = splitOutModels;
return rawInputModel;
}
/**
* Get all presets (default and custom ones from options) for a given preset type (class, enum, etc).
*/
protected getPresets(presetType: string): Array<[Preset, unknown]> {
const filteredPresets: Array<[Preset, unknown]> = [];
const defaultPreset = this.options.defaultPreset;
if (defaultPreset !== undefined) {
filteredPresets.push([defaultPreset[String(presetType)], this.options]);
}
const presets = this.options.presets || [];
for (const p of presets) {
if (isPresetWithOptions(p)) {
const preset = p.preset[String(presetType)];
if (preset) {
filteredPresets.push([preset, p.options]);
}
} else {
const preset = p[String(presetType)];
if (preset) {
filteredPresets.push([preset, undefined]);
}
}
}
return filteredPresets;
}
}