-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.go
78 lines (71 loc) · 2.75 KB
/
models.go
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
package superstruct
import (
"github.com/specgen-io/specgen/v2/goven/generator"
"github.com/specgen-io/specgen/v2/goven/spec"
"github.com/specgen-io/specgen/v2/goven/typescript/common"
"github.com/specgen-io/specgen/v2/goven/typescript/module"
"github.com/specgen-io/specgen/v2/goven/typescript/writer"
)
func (g *Generator) Models(version *spec.Version) *generator.CodeFile {
modelsModule := g.Modules.Models(version)
return g.models(version.ResolvedModels, modelsModule)
}
func (g *Generator) ErrorModels(httpErrors *spec.HttpErrors) *generator.CodeFile {
return g.models(httpErrors.ResolvedModels, g.Modules.ErrorsModels)
}
func (g *Generator) models(models []*spec.NamedModel, modelsModule module.Module) *generator.CodeFile {
w := writer.New(modelsModule)
w.Imports.Star(g.Modules.Validation, `t`)
for _, model := range models {
w.EmptyLine()
if model.IsObject() {
g.objectModel(w, model)
} else if model.IsEnum() {
g.enumModel(w, model)
} else if model.IsOneOf() {
g.unionModel(w, model)
}
}
return w.ToCodeFile()
}
func (g *Generator) objectModel(w *writer.Writer, model *spec.NamedModel) {
w.Line("export const T%s = t.type({", model.Name.PascalCase())
for _, field := range model.Object.Fields {
w.Line(" %s: %s,", field.Name.Source, g.RuntimeTypeSamePackage(&field.Type.Definition))
}
w.Line("})")
w.Line("")
w.Line("export type %s = t.Infer<typeof T%s>", model.Name.PascalCase(), model.Name.PascalCase())
}
func (g *Generator) enumModel(w *writer.Writer, model *spec.NamedModel) {
w.Line("export const T%s = t.enums ([", model.Name.PascalCase())
for _, item := range model.Enum.Items {
w.Line(` "%s",`, item.Value)
}
w.Line("])")
w.EmptyLine()
w.Line("export type %s = t.Infer<typeof T%s>", model.Name.PascalCase(), model.Name.PascalCase())
w.EmptyLine()
w.Line("export const %s = {", model.Name.PascalCase())
for _, item := range model.Enum.Items {
w.Line(` %s: <%s>"%s",`, item.Name.UpperCase(), model.Name.PascalCase(), item.Value)
}
w.Line("}")
}
func (g *Generator) unionModel(w *writer.Writer, model *spec.NamedModel) {
if model.OneOf.Discriminator != nil {
w.Line("export const T%s = t.union([", model.Name.PascalCase())
for _, item := range model.OneOf.Items {
w.Line(" t.intersection([t.type({%s: t.literal('%s')}), %s]),", common.TSIdentifier(*model.OneOf.Discriminator), item.Name.Source, g.RuntimeTypeSamePackage(&item.Type.Definition))
}
w.Line("])")
} else {
w.Line("export const T%s = t.union([", model.Name.PascalCase())
for _, item := range model.OneOf.Items {
w.Line(" t.object({%s: %s}),", item.Name.Source, g.RuntimeTypeSamePackage(&item.Type.Definition))
}
w.Line("])")
}
w.EmptyLine()
w.Line("export type %s = t.Infer<typeof T%s>", model.Name.PascalCase(), model.Name.PascalCase())
}