-
Notifications
You must be signed in to change notification settings - Fork 46
/
VariantsRenderer.ts
53 lines (48 loc) · 1.37 KB
/
VariantsRenderer.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
import { WarthogModel } from '../model'
import { GeneratorContext } from './SourcesGenerator'
import { AbstractRenderer } from './AbstractRenderer'
import { ModelRenderer } from './ModelRenderer'
import { withUnionType } from './union-context'
import { generateEntityImport } from './utils'
export class VariantsRenderer extends AbstractRenderer {
constructor(model: WarthogModel, context: GeneratorContext = {}) {
super(model, context)
}
withImports(): { imports: string[] } {
const moduleImports = new Set<string>()
for (const variant of this.model.variants) {
variant.fields.map((f) => {
if (f.isEntity()) {
moduleImports.add(generateEntityImport(f.type))
}
})
}
return { imports: Array.from(moduleImports) }
}
withVariants(): GeneratorContext {
const variants: GeneratorContext[] = []
for (const v of this.model.variants) {
const renderer = new ModelRenderer(this.model, v, {})
variants.push(renderer.transform())
}
return {
variants,
}
}
withUnions(): GeneratorContext {
const unions: GeneratorContext[] = []
for (const u of this.model.unions) {
unions.push(withUnionType(u))
}
return {
unions,
}
}
transform(): GeneratorContext {
return {
...this.withUnions(),
...this.withVariants(),
...this.withImports(),
}
}
}