-
Notifications
You must be signed in to change notification settings - Fork 5
/
zod.go
342 lines (275 loc) · 7.63 KB
/
zod.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package supervillain
import (
"fmt"
"reflect"
"regexp"
"sort"
"strings"
)
func NewConverter(custom map[string]CustomFn) Converter {
c := Converter{
prefix: "",
outputs: make(map[string]entry),
custom: custom,
}
return c
}
func (c *Converter) Convert(input interface{}) string {
t := reflect.TypeOf(input)
c.addSchema(t.Name(), c.convertStructTopLevel(t))
output := strings.Builder{}
sorted := []entry{}
for _, ent := range c.outputs {
sorted = append(sorted, ent)
}
sort.Sort(ByOrder(sorted))
for _, ent := range sorted {
output.WriteString(ent.data)
output.WriteString("\n\n")
}
return output.String()
}
func StructToZodSchema(input interface{}) string {
c := Converter{
prefix: "",
outputs: make(map[string]entry),
}
t := reflect.TypeOf(input)
c.addSchema(t.Name(), c.convertStructTopLevel(t))
output := strings.Builder{}
sorted := []entry{}
for _, ent := range c.outputs {
sorted = append(sorted, ent)
}
sort.Sort(ByOrder(sorted))
for _, ent := range sorted {
output.WriteString(ent.data)
output.WriteString("\n\n")
}
return output.String()
}
func StructToZodSchemaWithPrefix(prefix string, input interface{}) string {
c := Converter{
prefix: prefix,
outputs: make(map[string]entry),
}
t := reflect.TypeOf(input)
c.addSchema(t.Name(), c.convertStructTopLevel(t))
output := strings.Builder{}
sorted := []entry{}
for _, ent := range c.outputs {
sorted = append(sorted, ent)
}
sort.Sort(ByOrder(sorted))
for _, ent := range sorted {
output.WriteString(ent.data)
output.WriteString("\n\n")
}
return output.String()
}
var typeMapping = map[reflect.Kind]string{
reflect.Bool: "boolean",
reflect.Int: "number",
reflect.Int8: "number",
reflect.Int16: "number",
reflect.Int32: "number",
reflect.Int64: "number",
reflect.Uint: "number",
reflect.Uint8: "number",
reflect.Uint16: "number",
reflect.Uint32: "number",
reflect.Uint64: "number",
reflect.Uintptr: "number",
reflect.Float32: "number",
reflect.Float64: "number",
reflect.Complex64: "number",
reflect.Complex128: "number",
reflect.String: "string",
reflect.Interface: "any",
}
type entry struct {
order int
data string
}
type ByOrder []entry
func (a ByOrder) Len() int { return len(a) }
func (a ByOrder) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByOrder) Less(i, j int) bool { return a[i].order < a[j].order }
type CustomFn func(*Converter, reflect.Type, string, string, int) string
type Converter struct {
prefix string
structs int
outputs map[string]entry
custom map[string]CustomFn
}
func (c *Converter) addSchema(name string, data string) {
order := c.structs
c.outputs[name] = entry{order, data}
c.structs = order + 1
}
func schemaName(prefix, name string) string {
return fmt.Sprintf("%s%sSchema", prefix, name)
}
func fieldName(input reflect.StructField) string {
if json := input.Tag.Get("json"); json != "" {
args := strings.Split(json, ",")
if len(args[0]) > 0 {
return args[0]
}
// This is also valid:
// json:",omitempty"
// so in this case, args[0] will be empty, so fall through to using the
// raw field name.
}
// When Golang marshals a struct to JSON and it doesn't have any JSON tags
// that give the fields names, it defaults to just using the field's name.
return input.Name
}
func typeName(t reflect.Type) string {
if t.Kind() == reflect.Struct {
return t.Name()
}
if t.Kind() == reflect.Ptr {
return typeName(t.Elem())
}
if t.Kind() == reflect.Slice {
return typeName(t.Elem())
}
return "UNKNOWN"
}
func (c *Converter) convertStructTopLevel(t reflect.Type) string {
output := strings.Builder{}
name := t.Name()
output.WriteString(fmt.Sprintf(
`export const %s = %s
`,
schemaName(c.prefix, name), c.convertStruct(t, 0)))
output.WriteString(fmt.Sprintf(`export type %s%s = z.infer<typeof %s%sSchema>`,
c.prefix, name, c.prefix, name))
return output.String()
}
func (c *Converter) convertStruct(input reflect.Type, indent int) string {
output := strings.Builder{}
output.WriteString(`z.object({
`)
fields := input.NumField()
for i := 0; i < fields; i++ {
field := input.Field(i)
optional := isOptional(field)
nullable := isNullable(field)
line := c.convertField(field, indent+1, optional, nullable)
output.WriteString(line)
}
output.WriteString(indentation(indent))
output.WriteString(`})`)
return output.String()
}
var matchGenericTypeName = regexp.MustCompile(`(.+)\[(.+)\]`)
// checking it a reflected type is a generic isn't supported as far as I can see
// so this simple check looks for a `[` character in the type name: `T1[T2]`.
func isGeneric(t reflect.Type) bool {
return strings.Contains(t.Name(), "[")
}
// gets the full name and if it's a generic type, strips out the [T] part.
func getFullName(t reflect.Type) (string, string) {
var typename string
var generic string
if isGeneric(t) {
m := matchGenericTypeName.FindAllStringSubmatch(t.Name(), 1)[0]
typename = m[1]
generic = m[2]
} else {
typename = t.Name()
}
return fmt.Sprintf("%s.%s", t.PkgPath(), typename), generic
}
func (c *Converter) handleCustomType(t reflect.Type, name string, indent int) (string, bool) {
fullName, generic := getFullName(t)
custom, ok := c.custom[fullName]
if ok {
return custom(c, t, name, generic, indent), true
}
return "", false
}
func (c *Converter) ConvertType(t reflect.Type, name string, indent int) string {
if t.Kind() == reflect.Ptr {
inner := t.Elem()
return c.ConvertType(inner, name, indent)
}
if custom, ok := c.handleCustomType(t, name, indent); ok {
return custom
}
if t.Kind() == reflect.Slice {
return fmt.Sprintf(
"%s.array()",
c.ConvertType(t.Elem(), name, indent))
}
if t.Kind() == reflect.Struct {
// Handle nested un-named structs - these are inline.
if t.Name() == "" {
return c.convertStruct(t, indent)
} else if t.Name() == "Time" {
// timestamps are serialised to strings.
return "z.string()"
} else {
c.addSchema(name, c.convertStructTopLevel(t))
return schemaName(c.prefix, name)
}
}
if t.Kind() == reflect.Map {
return c.convertMap(t, name, indent)
}
ztype, ok := typeMapping[t.Kind()]
if !ok {
panic(fmt.Sprint("cannot handle: ", t.Kind()))
}
return fmt.Sprintf("z.%s()", ztype)
}
func (c *Converter) convertField(f reflect.StructField, indent int, optional, nullable bool) string {
name := fieldName(f)
// because nullability is processed before custom types, this makes sure
// the custom type has control over nullability.
fullName, _ := getFullName(f.Type)
_, isCustom := c.custom[fullName]
optionalCall := ""
if optional {
optionalCall = ".optional()"
}
nullableCall := ""
if nullable && !isCustom {
nullableCall = ".nullable()"
}
return fmt.Sprintf(
"%s%s: %s%s%s,\n",
indentation(indent),
name,
c.ConvertType(f.Type, typeName(f.Type), indent),
optionalCall,
nullableCall)
}
func (c *Converter) convertMap(t reflect.Type, name string, indent int) string {
return fmt.Sprintf(`z.record(%s, %s)`,
c.ConvertType(t.Key(), name, indent),
c.ConvertType(t.Elem(), name, indent))
}
func isNullable(field reflect.StructField) bool {
// pointers can be nil, which are mapped to null in JS/TS.
if field.Type.Kind() == reflect.Ptr {
return true
}
// arrays of pointer types may contain null values
if field.Type.Kind() == reflect.Slice {
return true
}
return false
}
func isOptional(field reflect.StructField) bool {
// omitempty zero-values are omitted and are mapped to undefined in JS/TS.
if strings.Contains(field.Tag.Get("json"), "omitempty") {
return true
}
return false
}
func indentation(level int) string {
return strings.Repeat(" ", level*2)
}