-
Notifications
You must be signed in to change notification settings - Fork 8
/
fields.go
351 lines (320 loc) · 8.47 KB
/
fields.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
343
344
345
346
347
348
349
350
351
package sonnet
import (
"reflect"
"sort"
"strings"
"unicode"
)
type (
flag byte
fields struct {
flds []field
fldsMap *perf
caseMap *perf
flg flag
}
field struct {
dec decoder
enc encoder
flg flag
name string
nameJSON []byte
nameHTML []byte
typ reflect.Type
idxs []int
}
byIdx []field
)
const (
flagDup = 1 << iota
flagAZ95
flagTag
flagString
flagOmitempty
)
func (by byIdx) Len() int {
return len(by)
}
func (by byIdx) Swap(fst, sec int) {
by[fst], by[sec] = by[sec], by[fst]
}
func (by byIdx) Less(fst, sec int) bool {
for idx := range by[fst].idxs {
if idx >= len(by[sec].idxs) {
return false
}
if by[fst].idxs[idx] != by[sec].idxs[idx] {
return by[fst].idxs[idx] < by[sec].idxs[idx]
}
}
return len(by[fst].idxs) < len(by[sec].idxs)
}
// typeFields returns a list of fields that JSON should recognize for the given type.
// The algorithm is breadth-first search over the set of structs to include - the top struct
// and then any reachable anonymous structs.
func makeFields(typ reflect.Type) fields {
var flds, currFlds, nextFlds []field
var currCnt, nextCnt map[reflect.Type]int
nextFlds = append(nextFlds, field{typ: typ})
vis := make(map[reflect.Type]struct{})
for len(nextFlds) > 0 {
currFlds, nextFlds = nextFlds, currFlds[:0]
currCnt, nextCnt = nextCnt, map[reflect.Type]int{}
for _, fld := range currFlds {
if _, ok := vis[fld.typ]; ok {
continue
}
vis[fld.typ] = struct{}{}
// Scan fld.typ for fields to include.
for idx := 0; idx < fld.typ.NumField(); idx++ {
str := fld.typ.Field(idx)
if str.Anonymous {
typ := str.Type
if typ.Kind() == reflect.Pointer {
typ = typ.Elem()
}
if !str.IsExported() && typ.Kind() != reflect.Struct {
// Ignore embedded fields of unexported non-struct types.
continue
}
// Do not ignore embedded fields of unexported struct types
// since they may have exported fields.
} else if !str.IsExported() {
// Ignore unexported non-embedded fields.
continue
}
tag := str.Tag.Get("json")
if tag == "-" {
continue
}
var name string
spl := strings.Split(tag, ",")
if len(spl) > 0 {
name = spl[0]
spl = spl[1:]
}
if !isValidTag(name) {
name = ""
}
idxs := make([]int, len(fld.idxs)+1)
copy(idxs, fld.idxs)
idxs[len(fld.idxs)] = idx
typ := str.Type
if typ.Name() == "" && typ.Kind() == reflect.Pointer {
typ = typ.Elem()
}
var flg flag
for idx := range spl {
if spl[idx] == "string" {
flg |= flagString
}
if spl[idx] == "omitempty" {
flg |= flagOmitempty
}
}
const accept = reflect.Float64 - reflect.Bool
if typ.Kind()-reflect.Bool > accept && typ.Kind() != reflect.String {
flg &^= flagString
}
// Record found field and index sequence.
if name != "" || !str.Anonymous || typ.Kind() != reflect.Struct {
if name != "" {
flg |= flagTag
} else {
name = str.Name
}
app := field{
name: name,
idxs: idxs,
typ: typ,
flg: flg,
}
flds = append(flds, app)
if currCnt[fld.typ] > 1 {
// If there were multiple instances, add a second,
// so that the annihilation code will see a duplicate.
// It only cares about the distinction between 1 or 2,
// so don't bother generating any more copies.
flds = append(flds, flds[len(flds)-1])
}
continue
}
// Record new anonymous struct to explore in nextFlds round.
nextCnt[typ]++
if nextCnt[typ] == 1 {
app := field{
name: typ.Name(),
idxs: idxs,
typ: typ,
}
nextFlds = append(nextFlds, app)
}
}
}
}
sort.Slice(flds, func(fst, sec int) bool {
// sort field by name, breaking ties with depth, then
// breaking ties with "name came from json tag", then
// breaking ties with index sequence.
if flds[fst].name != flds[sec].name {
return flds[fst].name < flds[sec].name
}
if len(flds[fst].idxs) != len(flds[sec].idxs) {
return len(flds[fst].idxs) < len(flds[sec].idxs)
}
if (flds[fst].flg^flds[sec].flg)&flagTag != 0 {
return flds[fst].flg&flagTag != 0
}
return byIdx(flds).Less(fst, sec)
})
// Delete all fields that are hidden by the Go rules for embedded fields,
// except that fields with JSON tags are promoted.
// The fields are sorted in primary order of name, secondary order
// of field index length. Loop over names; for each name, delete
// hidden fields by choosing the one dominant field that survives.
out := flds[:0]
for adv, idx := 0, 0; idx < len(flds); idx += adv {
// One iteration per name.
// Find the sequence of fields with the name of this first field.
fld := flds[idx]
name := fld.name
for adv = 1; idx+adv < len(flds); adv++ {
fld := flds[idx+adv]
if fld.name != name {
break
}
}
if adv == 1 { // Only one field with this name
out = append(out, fld)
continue
}
dom, ok := dominant(flds[idx : idx+adv])
if ok {
out = append(out, dom)
}
}
flds = out
sort.Sort(byIdx(flds))
az95 := true
for idx := range flds {
fld := &flds[idx]
az95 = az95 && isAZ95(fld.name)
flw := followType(typ, fld.idxs)
fld.dec, _ = decs.get(flw)
fld.enc, _ = encs.get(flw)
fld.nameJSON = make([]byte, 0, len(fld.name)+2)
fld.nameJSON = append(fld.nameJSON, ',')
fld.nameJSON = appendString(fld.nameJSON, fld.name, false)
fld.nameJSON = append(fld.nameJSON, ':')
fld.nameHTML = make([]byte, 0, len(fld.name)+2)
fld.nameHTML = append(fld.nameHTML, ',')
fld.nameHTML = appendString(fld.nameHTML, fld.name, true)
fld.nameHTML = append(fld.nameHTML, ':')
}
ret := fields{
flds: flds,
fldsMap: makePerf(len(flds)),
caseMap: makePerf(len(flds)),
}
tups := make([]tuple, 0, len(flds))
for idx, fld := range flds {
tups = append(tups, tuple{
key: []byte(fld.name),
elm: &flds[idx],
})
}
ret.fldsMap.set(tups)
fst := make(map[string]struct{}, len(tups))
dst := tups[:0]
for idx := range tups {
var buf [12]byte
up := appendUpper(buf[:0], tups[idx].key)
if _, ok := fst[string(up)]; !ok {
fst[string(up)] = struct{}{}
dst = append(dst, tuple{
key: up,
elm: tups[idx].elm,
})
continue
}
ret.flg |= flagDup
}
ret.caseMap.set(dst)
if az95 {
ret.flg |= flagAZ95
}
return ret
}
func isValidTag(str string) bool {
if str == "" {
return false
}
for _, char := range str {
switch {
case strings.ContainsRune("!#$%&()*+-./:;<=>?@[]^_{|}~ ", char):
// Backslash and quote chars are reserved, but
// otherwise any punctuation chars are allowed
// in a tag name.
default:
if !unicode.IsLetter(char) && !unicode.IsDigit(char) {
return false
}
}
}
return true
}
func isAZ95(str string) bool {
const lenAZ = 'Z' - 'A'
for idx := range str {
if str[idx] != '_' && str[idx]&^0x20-'A' > lenAZ {
return false
}
}
return true
}
// dominant looks through the fields, all of which are known to
// have the same name, to find the single field that dominates the
// others using Go's embedding rules, modified by the presence of
// JSON tags. If there are multiple top-level fields, the boolean
// will be false: This condition is an error in Go and we skip all
// the fields.
func dominant(flds []field) (field, bool) {
// The fields are sorted in increasing index-length order, then by presence of tag.
// That means that the first field is the dominant one. We need only check
// for error cases: two fields at top level, either both tagged or neither tagged.
if len(flds) > 1 && len(flds[0].idxs) == len(flds[1].idxs) && (flds[0].flg^flds[1].flg)&flagTag == 0 {
return field{}, false
}
return flds[0], true
}
func followType(typ reflect.Type, idxs []int) reflect.Type {
for _, idx := range idxs {
if typ.Kind() == reflect.Pointer {
typ = typ.Elem()
}
typ = typ.Field(idx).Type
}
return typ
}
func followValue(val reflect.Value, idxs []int) (reflect.Value, error) {
const tmpl = "cannot set embedded pointer to unexported struct: "
for _, idx := range idxs {
if val.Kind() == reflect.Pointer {
if val.IsNil() {
// If a struct embeds a pointer to an unexported type,
// it is not possible to set a newly allocated value
// since the field is unexported.
//
// See https://golang.org/issue/21357
elm := val.Type().Elem()
if !val.CanSet() {
return reflect.Value{}, fieldError(tmpl + elm.String())
}
val.Set(reflect.New(elm))
}
val = val.Elem()
}
val = val.Field(idx)
}
return val, nil
}