-
Notifications
You must be signed in to change notification settings - Fork 0
/
type.go
81 lines (75 loc) · 2.08 KB
/
type.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
79
80
81
package superstruct
import (
"fmt"
"github.com/specgen-io/specgen/v2/goven/spec"
"github.com/specgen-io/specgen/v2/goven/typescript/types"
)
func (g *Generator) RuntimeTypeName(typeName string) string {
return fmt.Sprintf("T%s", typeName)
}
func (g *Generator) RuntimeTypeSamePackage(typ *spec.TypeDef) string {
return g.runtimeType(typ, true)
}
func (g *Generator) RuntimeType(typ *spec.TypeDef) string {
return g.runtimeType(typ, false)
}
func (g *Generator) runtimeType(typ *spec.TypeDef, samePackage bool) string {
switch typ.Node {
case spec.PlainType:
return g.plainSuperstructType(typ, samePackage)
case spec.NullableType:
child := g.runtimeType(typ.Child, samePackage)
result := "t.optional(t.nullable(" + child + "))"
return result
case spec.ArrayType:
child := g.runtimeType(typ.Child, samePackage)
result := "t.array(" + child + ")"
return result
case spec.MapType:
child := g.runtimeType(typ.Child, samePackage)
result := "t.record(t.string(), " + child + ")"
return result
default:
panic(fmt.Sprintf("Unknown type: %v", typ))
}
}
func (g *Generator) plainSuperstructType(typ *spec.TypeDef, samePackage bool) string {
switch typ.Plain {
case spec.TypeInt32:
return "t.number()"
case spec.TypeInt64:
return "t.number()"
case spec.TypeFloat:
return "t.number()"
case spec.TypeDouble:
return "t.number()"
case spec.TypeDecimal:
return "t.number()"
case spec.TypeBoolean:
return "t.boolean()"
case spec.TypeString:
return "t.string()"
case spec.TypeUuid:
return "t.string()"
case spec.TypeDate:
return "t.string()"
case spec.TypeDateTime:
return "t.StrDateTime"
case spec.TypeJson:
return "t.unknown()"
default:
if typ.Info.Model != nil {
if !samePackage {
if typ.Info.Model.InVersion != nil {
return fmt.Sprintf(`%s.%s`, types.ModelsPackage, g.RuntimeTypeName(typ.Plain))
}
if typ.Info.Model.InHttpErrors != nil {
return fmt.Sprintf(`%s.%s`, types.ErrorsPackage, g.RuntimeTypeName(typ.Plain))
}
}
return g.RuntimeTypeName(typ.Plain)
} else {
panic(fmt.Sprintf(`unknown type %s`, typ.Plain))
}
}
}