-
Notifications
You must be signed in to change notification settings - Fork 2
/
field.go
68 lines (63 loc) · 1.55 KB
/
field.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
package go2typings
import (
"reflect"
"strings"
)
type Field struct {
Name string `json:"name"`
TsType string `json:"type"`
KeyType string `json:"keyType,omitempty"`
ValType string `json:"valType,omitempty"`
CanBeNull bool `json:"canBeNull"`
IsOptional bool `json:"isOptional"`
IsDate bool `json:"isDate"`
T reflect.Type
}
func Type(field *Field) (string, string) {
fieldType := field.TsType
name := field.Name
if field.IsDate {
fieldType = "string"
}
if field.CanBeNull {
fieldType += " | null"
}
if field.IsOptional {
name += "?"
}
return name, fieldType
}
func (f *Field) setProps(sf reflect.StructField, sft reflect.Type) (ignore bool) {
var (
jsonTag = strings.Split(sf.Tag.Get("json"), ",")
tsTag = strings.Split(sf.Tag.Get("ts"), ",")
)
if jsonTag[0] == "" {
return true
}
if ignore = len(tsTag) > 0 && tsTag[0] == "-" || len(jsonTag) > 0 && jsonTag[0] == "-"; ignore {
return true
}
if f.Name = sf.Name; len(jsonTag) > 0 && jsonTag[0] != "" {
f.Name = jsonTag[0]
}
f.IsDate = isDate(sft) || len(tsTag) > 0 && tsTag[0] == "date" || sft.Kind() == reflect.Int64 && strings.HasSuffix(f.Name, "TS")
if len(tsTag) > 1 {
switch tsTag[1] {
case "no-null":
f.CanBeNull = false
case "null":
f.CanBeNull = true
case "optional":
f.IsOptional = true
}
}
f.IsOptional = f.IsOptional || len(jsonTag) > 1 && jsonTag[1] == "omitempty"
if tsTag[0] == "string" {
f.TsType = "string"
}
return
}
func isDate(t reflect.Type) bool {
return t.Name() == "Time" && t.PkgPath() == "time"
}