forked from starius/api2
/
typescript.printer.go
157 lines (143 loc) · 3.94 KB
/
typescript.printer.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
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
package typegen
import (
"bytes"
"fmt"
"io"
"path"
"reflect"
"text/template"
)
const GlobalTemplate = `/* eslint-disable */
// prettier-disable
// prettier-ignore
// Code generated by api2. DO NOT EDIT.
{{- range $namespace, $types := .}}
export namespace {{$namespace}} {
{{- range $type := $types}}
{{if $type.Doc| ne ""}}// {{$type.Doc -}}{{end}}
export type {{$type.Name}} = {{$type|Serialize}}{{end}}
}{{end}}
`
const RecordTemplate = `{{ range $field := .Embedded}} {{$field | RefName}} & {{end}}{
{{- range $field := .Fields}}
{{$field | Row}} {{if $field.Doc| ne ""}}// {{$field.Doc}}{{end}}
{{- end}}
}
`
type TypeToString = func(t reflect.Type) string
func typeToString(t reflect.Type, getTypeName TypeToString, stringifyType TypeToString) string {
k := t.Kind()
customT := stringifyType(t)
if customT != "" {
return customT
}
switch {
case k == reflect.Ptr:
t = indirect(t)
return fmt.Sprintf("%s | null", typeToString(t, getTypeName, stringifyType))
case k == reflect.Struct:
if isDate(t) {
return "string"
}
return getTypeName(t)
case isNumber(k) && isEnum(t):
return getTypeName(t)
case isNumber(k):
return "number"
case k == reflect.String && isEnum(t):
return getTypeName(t)
case k == reflect.String:
return "string"
case k == reflect.Bool:
return "boolean"
case k == reflect.Slice || k == reflect.Array:
return fmt.Sprintf("Array<%s> | null", typeToString(t.Elem(), getTypeName, stringifyType))
case k == reflect.Interface:
return "any"
case k == reflect.Map:
KeyType, ValType := typeToString(t.Key(), getTypeName, stringifyType), typeToString(t.Elem(), getTypeName, stringifyType)
return fmt.Sprintf("Record<%s, %s> | null", KeyType, ValType)
}
return "any"
}
func stringifyCustom(t reflect.Type) string {
return ""
}
type Stringifier = func(t reflect.Type) string
func PrintTsTypes(parser *Parser, w io.Writer, stringify Stringifier) {
if stringify == nil {
stringify = stringifyCustom
}
output := make(map[string][]IType)
for _, m := range parser.visitOrder {
pkg := parser.seen[m].GetPackage()
output[path.Base(pkg)] = append(output[path.Base(pkg)], parser.seen[m])
}
recordToString := func(r *RecordDef) string {
tmpl, err := template.New("content").Funcs(template.FuncMap{
"RefName": func(t reflect.Type) string {
return parser.GetVisited(t).RefName()
},
"Row": func(t RecordField) string {
keyName := t.Key
// fmt.Println("[printer]", t.Tag, t.Type, t)
fieldType := t.Tag.FieldType
if t.Tag.FieldName != "" {
keyName = t.Tag.FieldName
}
// fmt.Println("[printer]", t.Tag, t.Type, t)
if t.Type != nil {
visited := parser.GetVisited(t.Type)
if visited != nil {
fieldType = visited.RefName()
} else {
// fmt.Println("[printer]", t.Tag, t.Type, visited)
fieldType = typeToString(t.Type, func(t reflect.Type) string {
// fmt.Println("[printer-tostring]", t, parser.GetVisited(t))
return parser.GetVisited(t).RefName()
}, stringify)
}
}
optionalText := ""
if t.Tag.State == Optional {
optionalText = "?"
}
nullText := ""
if t.Tag.State == Null || t.IsRef {
nullText = " | null"
}
return fmt.Sprintf("%s%s: %s%s", keyName, optionalText, fieldType, nullText)
},
}).Parse(RecordTemplate)
panicIf(err)
w := &bytes.Buffer{}
err = tmpl.Execute(w, r)
panicIf(err)
return w.String()
}
tmpl, err := template.New("types template").Funcs(template.FuncMap{
"Serialize": func(t IType) string {
switch v := t.(type) {
case *RecordDef:
return recordToString(v)
case *EnumDef:
res := ""
for _, v := range v.Values {
if res != "" {
res += " | "
}
res += v.Stringify()
}
return res
case *TypeDef:
return typeToString(v.T, func(t reflect.Type) string {
return parser.GetVisited(t).RefName()
}, stringify)
}
return "1"
},
}).Parse(GlobalTemplate)
panicIf(err)
err = tmpl.Execute(w, output)
panicIf(err)
}