-
Notifications
You must be signed in to change notification settings - Fork 3
/
struct_info.go
171 lines (143 loc) · 3.69 KB
/
struct_info.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
package urlstruct
import (
"context"
"net/url"
"reflect"
"github.com/codemodus/kace"
"github.com/vmihailenco/tagparser"
)
type Unmarshaler interface {
UnmarshalValues(ctx context.Context, values url.Values) error
}
type ParamUnmarshaler interface {
UnmarshalParam(ctx context.Context, name string, values []string) error
}
//------------------------------------------------------------------------------
type StructInfo struct {
fields []*Field
fieldMap map[string]*Field
structs map[string][]int
isUnmarshaler bool
isParamUnmarshaler bool
unmarshalerIndexes [][]int
}
func newStructInfo(typ reflect.Type) *StructInfo {
sinfo := &StructInfo{
fields: make([]*Field, 0, typ.NumField()),
fieldMap: make(map[string]*Field),
isUnmarshaler: isUnmarshaler(reflect.PtrTo(typ)),
isParamUnmarshaler: isParamUnmarshaler(reflect.PtrTo(typ)),
}
addFields(sinfo, typ, nil)
return sinfo
}
func (s *StructInfo) Field(name string) *Field {
return s.fieldMap[name]
}
func addFields(sinfo *StructInfo, typ reflect.Type, baseIndex []int) {
for i := 0; i < typ.NumField(); i++ {
sf := typ.Field(i)
if sf.PkgPath != "" && !sf.Anonymous {
continue
}
if sf.Anonymous {
tag := sf.Tag.Get("urlstruct")
if tag == "-" {
continue
}
sfType := sf.Type
if sfType.Kind() == reflect.Ptr {
sfType = sfType.Elem()
}
if sfType.Kind() != reflect.Struct {
continue
}
if isUnmarshaler(reflect.PtrTo(sfType)) {
index := joinIndex(baseIndex, sf.Index)
sinfo.unmarshalerIndexes = append(sinfo.unmarshalerIndexes, index)
}
addFields(sinfo, sfType, joinIndex(baseIndex, sf.Index))
} else {
addField(sinfo, sf, baseIndex)
}
}
}
func addField(sinfo *StructInfo, sf reflect.StructField, baseIndex []int) {
tag := tagparser.Parse(sf.Tag.Get("urlstruct"))
if tag.Name == "-" {
return
}
name := tag.Name
if name == "" {
name = sf.Name
}
index := joinIndex(baseIndex, sf.Index)
if sf.Type.Kind() == reflect.Struct {
if sinfo.structs == nil {
sinfo.structs = make(map[string][]int)
}
sinfo.structs[kace.Snake(name)] = append(baseIndex, sf.Index...)
}
if isUnmarshaler(reflect.PtrTo(sf.Type)) {
sinfo.unmarshalerIndexes = append(sinfo.unmarshalerIndexes, index)
}
f := &Field{
Type: sf.Type,
Name: kace.Snake(name),
Index: index,
Tag: tag,
}
f.init()
if f.scanValue != nil {
sinfo.fields = append(sinfo.fields, f)
sinfo.fieldMap[f.Name] = f
}
}
func joinIndex(base, idx []int) []int {
if len(base) == 0 {
return idx
}
r := make([]int, 0, len(base)+len(idx))
r = append(r, base...)
r = append(r, idx...)
return r
}
//------------------------------------------------------------------------------
var (
contextType = reflect.TypeOf((*context.Context)(nil)).Elem()
urlValuesType = reflect.TypeOf((*url.Values)(nil)).Elem()
errorType = reflect.TypeOf((*error)(nil)).Elem()
)
func isUnmarshaler(typ reflect.Type) bool {
for i := 0; i < typ.NumMethod(); i++ {
meth := typ.Method(i)
if meth.Name == "UnmarshalValues" &&
meth.Type.NumIn() == 3 &&
meth.Type.NumOut() == 1 &&
meth.Type.In(1) == contextType &&
meth.Type.In(2) == urlValuesType &&
meth.Type.Out(0) == errorType {
return true
}
}
return false
}
var (
stringType = reflect.TypeOf("")
stringSliceType = reflect.TypeOf((*[]string)(nil)).Elem()
)
func isParamUnmarshaler(typ reflect.Type) bool {
for i := 0; i < typ.NumMethod(); i++ {
meth := typ.Method(i)
if meth.Name == "UnmarshalParam" &&
meth.Type.NumIn() == 4 &&
meth.Type.NumOut() == 1 &&
meth.Type.In(1) == contextType &&
meth.Type.In(2) == stringType &&
meth.Type.In(3) == stringSliceType &&
meth.Type.Out(0) == errorType {
return true
}
}
return false
}