-
Notifications
You must be signed in to change notification settings - Fork 0
/
collect.go
308 lines (286 loc) · 8.17 KB
/
collect.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
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
306
307
308
package gotots
import (
"errors"
"fmt"
"io"
"reflect"
"strings"
)
// Collect initiates the type collection process starting from a given type 't'.
func Collect(ctx *Context, roots ...any) error {
for _, root := range roots {
tyof := reflect.TypeOf(root)
if reflect.ValueOf(root).Kind() == reflect.Pointer {
tyof = tyof.Elem()
}
if tyof.PkgPath() == "" {
return errors.New("anonymous types are not supported for root type")
}
namePath := []string{ctx.config.FieldPackageNameToPrefix(tyof.PkgPath()) + tyof.Name()}
if err := CollectType(ctx, tyof, namePath); err != nil {
return err
}
}
return nil
}
// WriteFromContext writes the collected TypeScript interfaces to the provided writer.
func WriteFromContext(ctx *Context, w io.Writer) error {
for _, header := range ctx.customHeaders {
if _, err := fmt.Fprintln(w, header); err != nil {
return err
}
}
if len(ctx.customHeaders) > 0 {
if _, err := fmt.Fprintln(w); err != nil {
return err
}
}
for _, sinfo := range ctx.Structs {
if err := DescribeStruct(ctx, w, sinfo); err != nil {
return err
}
if _, err := fmt.Fprintln(w); err != nil {
return err
}
}
return nil
}
// CollectType recursively collects type information for the given type 't'.
func CollectType(ctx *Context, t reflect.Type, namePath []string) error {
if ctx.Cache[t] {
return nil
}
ctx.Cache[t] = true
switch t.Kind() {
case reflect.Ptr, reflect.Slice, reflect.Array:
elemType := t.Elem()
newNamePath := namePath
if elemType.Kind() == reflect.Struct && elemType.Name() == "" {
newNamePath = append(newNamePath, "Elem")
}
return CollectType(ctx, elemType, newNamePath)
case reflect.Map:
keyType := t.Key()
elemType := t.Elem()
if err := CollectType(ctx, keyType, namePath); err != nil {
return err
}
newElemNamePath := namePath
if elemType.Kind() == reflect.Struct && elemType.Name() == "" {
newElemNamePath = append(newElemNamePath, "Elem")
}
return CollectType(ctx, elemType, newElemNamePath)
case reflect.Struct:
// Skip anonymous structs
if t.Name() == "" {
return nil
}
var name string
pkg := t.PkgPath()
if pkg == "" {
pkg = ctx.config.PackageNameForAnonymous
}
pkgPrefix := ctx.config.FieldPackageNameToPrefix(pkg)
name = pkgPrefix + t.Name()
ctx.TypeNames[t] = name
sinfo := StructInfo{Type: t, GeneratedName: name}
if err := CollectFields(ctx, &sinfo, t, namePath); err != nil {
return err
}
ctx.Structs = append(ctx.Structs, sinfo)
default:
// Do nothing for other types
}
return nil
}
// StructInfo holds information about a collected struct.
type StructInfo struct {
Type reflect.Type
Fields []StructFieldInfo
GeneratedName string
}
// StructFieldInfo holds information about a struct field.
type StructFieldInfo struct {
Name string
Type reflect.Type
CustomType string
JsonTag string
}
// GoTypeToTsTypeMapKey converts a Go type to a TypeScript map key type.
func GoTypeToTsTypeMapKey(ctx *Context, t reflect.Type) (string, error) {
switch t.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32,
reflect.Float32, reflect.Float64:
return "number", nil
case reflect.String, reflect.Int64, reflect.Uint64:
return "string", nil
default:
return "", errors.New("unsupported map key type " + t.String())
}
}
// GoTypeToTSType converts a Go type to a TypeScript type.
func GoTypeToTSType(ctx *Context, t reflect.Type, fromPtr bool, depth int) (ty string, isOptional bool, err error) {
indent := " "
if ctx.config.IndentWithTabs {
indent = "\t"
}
switch t.Kind() {
case reflect.Struct:
if t.Name() == "" {
var structDef strings.Builder
structDef.WriteString("{\n")
sinfo := StructInfo{Type: t}
if err := CollectFields(ctx, &sinfo, t, []string{}); err != nil {
return "", false, err
}
for _, field := range sinfo.Fields {
fieldType, isOptional, _ := GoTypeToTSType(ctx, field.Type, false, depth+1)
optionalMark := ""
if isOptional {
optionalMark = "?"
}
if field.JsonTag != "" {
field.Name = field.JsonTag
}
// Adjust indentation based on depth
structDef.WriteString(fmt.Sprintf("%s%s%s: %s;\n", strings.Repeat(indent, depth+1), field.Name, optionalMark, fieldType))
}
structDef.WriteString(strings.Repeat(indent, depth) + "}")
return structDef.String(), fromPtr, nil
}
if name, ok := ctx.TypeNames[t]; ok {
return name, fromPtr, nil
}
return "any", fromPtr, nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32,
reflect.Float32, reflect.Float64:
return "number", fromPtr, nil
case reflect.Int64, reflect.Uint64:
return "bigint", fromPtr, nil
case reflect.String:
return "string", fromPtr, nil
case reflect.Bool:
return "boolean", fromPtr, nil
case reflect.Ptr:
return GoTypeToTSType(ctx, t.Elem(), true, depth)
case reflect.Array, reflect.Slice:
elemType, _, err := GoTypeToTSType(ctx, t.Elem(), false, depth)
if err != nil {
return "", true, err
}
// Set isOptional to true for arrays and slices
return elemType + "[]", true, nil
case reflect.Map:
keyType, err := GoTypeToTsTypeMapKey(ctx, t.Key())
if err != nil {
return "", false, err
}
elemType, isOptional, err := GoTypeToTSType(ctx, t.Elem(), false, depth)
if err != nil {
return "", false, err
}
if isOptional {
return fmt.Sprintf("{ [key: %s]: (%s | undefined) }", keyType, elemType), fromPtr, nil
} else {
return fmt.Sprintf("{ [key: %s]: %s }", keyType, elemType), fromPtr, nil
}
default:
return "any", fromPtr, nil
}
}
// CollectFields collects field information from a struct type.
func CollectFields(ctx *Context, sinfo *StructInfo, t reflect.Type, namePath []string) error {
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
jsonTag := field.Tag.Get("json")
if field.Anonymous && (field.Type.Kind() == reflect.Struct || field.Type.Kind() == reflect.Ptr) {
if jsonTag != "" && jsonTag != "-" {
// Treat embedded field with json tag as a normal field
} else {
// Embedded field without json tag, flatten fields
if err := CollectFields(ctx, sinfo, field.Type, namePath); err != nil {
return err
}
continue
}
}
sfield := StructFieldInfo{
Name: field.Name,
Type: field.Type,
CustomType: field.Tag.Get("tstype"),
}
if jsonTag != "" {
sfield.JsonTag = strings.Split(jsonTag, ",")[0]
}
if sfield.JsonTag == "-" {
continue
}
if sfield.CustomType == "" {
newNamePath := namePath
switch field.Type.Kind() {
case reflect.Struct:
if field.Type.Name() == "" {
newNamePath = append(newNamePath, field.Name)
}
case reflect.Slice, reflect.Array, reflect.Ptr:
elemType := field.Type.Elem()
if elemType.Kind() == reflect.Struct && elemType.Name() == "" {
newNamePath = append(newNamePath, field.Name)
}
case reflect.Map:
elemType := field.Type.Elem()
if elemType.Kind() == reflect.Struct && elemType.Name() == "" {
newNamePath = append(newNamePath, field.Name)
}
default:
}
if err := CollectType(ctx, field.Type, newNamePath); err != nil {
return err
}
}
sinfo.Fields = append(sinfo.Fields, sfield)
}
return nil
}
// DescribeStruct writes the TypeScript interface definition for a struct.
func DescribeStruct(ctx *Context, w io.Writer, s StructInfo) error {
name := s.GeneratedName
if _, err := fmt.Fprintf(w, "export interface %s {\n", name); err != nil {
return err
}
for _, field := range s.Fields {
tstype := field.CustomType
isOptional := false
if tstype == "" {
var err error
tstype, isOptional, err = GoTypeToTSType(ctx, field.Type, false, 1)
if err != nil {
return err
}
}
fieldName := field.Name
if field.JsonTag != "" {
fieldName = field.JsonTag
}
optionalMark := ""
if isOptional {
optionalMark = "?"
}
indent := " "
if ctx.config.IndentWithTabs {
indent = "\t"
}
if _, err := fmt.Fprintf(w, "%s%s%s: %s;\n", indent, fieldName, optionalMark, tstype); err != nil {
return err
}
}
if _, err := fmt.Fprintln(w, "}"); err != nil {
return err
}
return nil
}