1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
19 // indirect walks down v allocating pointers as needed,
20 // until it gets to a non-pointer.
21 // if it encounters an Unmarshaler, indirect stops and returns that.
22 // if decodingNull is true, indirect stops at the last pointer so it can be set to nil.
23 func indirect(v reflect.Value, decodingNull bool) (json.Unmarshaler, encoding.TextUnmarshaler, reflect.Value) {
24 // If v is a named type and is addressable,
25 // start with its address, so that if the type has pointer methods,
27 if v.Kind() != reflect.Ptr && v.Type().Name() != "" && v.CanAddr() {
31 // Load value from interface, but only if the result will be
32 // usefully addressable.
33 if v.Kind() == reflect.Interface && !v.IsNil() {
35 if e.Kind() == reflect.Ptr && !e.IsNil() && (!decodingNull || e.Elem().Kind() == reflect.Ptr) {
41 if v.Kind() != reflect.Ptr {
45 if v.Elem().Kind() != reflect.Ptr && decodingNull && v.CanSet() {
50 v.Set(reflect.New(v.Type().Elem()))
52 v = reflect.New(v.Type().Elem())
55 if v.Type().NumMethod() > 0 {
56 if u, ok := v.Interface().(json.Unmarshaler); ok {
57 return u, nil, reflect.Value{}
59 if u, ok := v.Interface().(encoding.TextUnmarshaler); ok {
60 return nil, u, reflect.Value{}
68 // A field represents a single field found in a struct.
71 nameBytes []byte // []byte(name)
72 equalFold func(s, t []byte) bool // bytes.EqualFold or equivalent
81 func fillField(f field) field {
82 f.nameBytes = []byte(f.name)
83 f.equalFold = foldFunc(f.nameBytes)
87 // byName sorts field by name, breaking ties with depth,
88 // then breaking ties with "name came from json tag", then
89 // breaking ties with index sequence.
92 func (x byName) Len() int { return len(x) }
94 func (x byName) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
96 func (x byName) Less(i, j int) bool {
97 if x[i].name != x[j].name {
98 return x[i].name < x[j].name
100 if len(x[i].index) != len(x[j].index) {
101 return len(x[i].index) < len(x[j].index)
103 if x[i].tag != x[j].tag {
106 return byIndex(x).Less(i, j)
109 // byIndex sorts field by index sequence.
112 func (x byIndex) Len() int { return len(x) }
114 func (x byIndex) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
116 func (x byIndex) Less(i, j int) bool {
117 for k, xik := range x[i].index {
118 if k >= len(x[j].index) {
121 if xik != x[j].index[k] {
122 return xik < x[j].index[k]
125 return len(x[i].index) < len(x[j].index)
128 // typeFields returns a list of fields that JSON should recognize for the given type.
129 // The algorithm is breadth-first search over the set of structs to include - the top struct
130 // and then any reachable anonymous structs.
131 func typeFields(t reflect.Type) []field {
132 // Anonymous fields to explore at the current level and the next.
134 next := []field{{typ: t}}
136 // Count of queued names for current level and the next.
137 count := map[reflect.Type]int{}
138 nextCount := map[reflect.Type]int{}
140 // Types already visited at an earlier level.
141 visited := map[reflect.Type]bool{}
147 current, next = next, current[:0]
148 count, nextCount = nextCount, map[reflect.Type]int{}
150 for _, f := range current {
154 visited[f.typ] = true
156 // Scan f.typ for fields to include.
157 for i := 0; i < f.typ.NumField(); i++ {
159 if sf.PkgPath != "" { // unexported
162 tag := sf.Tag.Get("json")
166 name, opts := parseTag(tag)
167 if !isValidTag(name) {
170 index := make([]int, len(f.index)+1)
172 index[len(f.index)] = i
175 if ft.Name() == "" && ft.Kind() == reflect.Ptr {
180 // Record found field and index sequence.
181 if name != "" || !sf.Anonymous || ft.Kind() != reflect.Struct {
186 fields = append(fields, fillField(field{
191 omitEmpty: opts.Contains("omitempty"),
192 quoted: opts.Contains("string"),
194 if count[f.typ] > 1 {
195 // If there were multiple instances, add a second,
196 // so that the annihilation code will see a duplicate.
197 // It only cares about the distinction between 1 or 2,
198 // so don't bother generating any more copies.
199 fields = append(fields, fields[len(fields)-1])
204 // Record new anonymous struct to explore in next round.
206 if nextCount[ft] == 1 {
207 next = append(next, fillField(field{name: ft.Name(), index: index, typ: ft}))
213 sort.Sort(byName(fields))
215 // Delete all fields that are hidden by the Go rules for embedded fields,
216 // except that fields with JSON tags are promoted.
218 // The fields are sorted in primary order of name, secondary order
219 // of field index length. Loop over names; for each name, delete
220 // hidden fields by choosing the one dominant field that survives.
222 for advance, i := 0, 0; i < len(fields); i += advance {
223 // One iteration per name.
224 // Find the sequence of fields with the name of this first field.
227 for advance = 1; i+advance < len(fields); advance++ {
228 fj := fields[i+advance]
233 if advance == 1 { // Only one field with this name
234 out = append(out, fi)
237 dominant, ok := dominantField(fields[i : i+advance])
239 out = append(out, dominant)
244 sort.Sort(byIndex(fields))
249 // dominantField looks through the fields, all of which are known to
250 // have the same name, to find the single field that dominates the
251 // others using Go's embedding rules, modified by the presence of
252 // JSON tags. If there are multiple top-level fields, the boolean
253 // will be false: This condition is an error in Go and we skip all
255 func dominantField(fields []field) (field, bool) {
256 // The fields are sorted in increasing index-length order. The winner
257 // must therefore be one with the shortest index length. Drop all
258 // longer entries, which is easy: just truncate the slice.
259 length := len(fields[0].index)
260 tagged := -1 // Index of first tagged field.
261 for i, f := range fields {
262 if len(f.index) > length {
268 // Multiple tagged fields at the same level: conflict.
270 return field{}, false
276 return fields[tagged], true
278 // All remaining fields have the same length. If there's more than one,
279 // we have a conflict (two fields named "X" at the same level) and we
282 return field{}, false
284 return fields[0], true
287 var fieldCache struct {
289 m map[reflect.Type][]field
292 // cachedTypeFields is like typeFields but uses a cache to avoid repeated work.
293 func cachedTypeFields(t reflect.Type) []field {
301 // Compute fields without lock.
302 // Might duplicate effort but won't hold other computations back.
309 if fieldCache.m == nil {
310 fieldCache.m = map[reflect.Type][]field{}
317 func isValidTag(s string) bool {
321 for _, c := range s {
323 case strings.ContainsRune("!#$%&()*+-./:<=>?@[]^_{|}~ ", c):
324 // Backslash and quote chars are reserved, but
325 // otherwise any punctuation chars are allowed
328 if !unicode.IsLetter(c) && !unicode.IsDigit(c) {
337 caseMask = ^byte(0x20) // Mask to ignore case in ASCII.
339 smallLongEss = '\u017f'
342 // foldFunc returns one of four different case folding equivalence
343 // functions, from most general (and slow) to fastest:
345 // 1) bytes.EqualFold, if the key s contains any non-ASCII UTF-8
346 // 2) equalFoldRight, if s contains special folding ASCII ('k', 'K', 's', 'S')
347 // 3) asciiEqualFold, no special, but includes non-letters (including _)
348 // 4) simpleLetterEqualFold, no specials, no non-letters.
350 // The letters S and K are special because they map to 3 runes, not just 2:
351 // * S maps to s and to U+017F 'ſ' Latin small letter long s
352 // * k maps to K and to U+212A 'K' Kelvin sign
353 // See http://play.golang.org/p/tTxjOc0OGo
355 // The returned function is specialized for matching against s and
356 // should only be given s. It's not curried for performance reasons.
357 func foldFunc(s []byte) func(s, t []byte) bool {
359 special := false // special letter
360 for _, b := range s {
361 if b >= utf8.RuneSelf {
362 return bytes.EqualFold
364 upper := b & caseMask
365 if upper < 'A' || upper > 'Z' {
367 } else if upper == 'K' || upper == 'S' {
368 // See above for why these letters are special.
373 return equalFoldRight
376 return asciiEqualFold
378 return simpleLetterEqualFold
381 // equalFoldRight is a specialization of bytes.EqualFold when s is
382 // known to be all ASCII (including punctuation), but contains an 's',
383 // 'S', 'k', or 'K', requiring a Unicode fold on the bytes in t.
384 // See comments on foldFunc.
385 func equalFoldRight(s, t []byte) bool {
386 for _, sb := range s {
391 if tb < utf8.RuneSelf {
393 sbUpper := sb & caseMask
394 if 'A' <= sbUpper && sbUpper <= 'Z' {
395 if sbUpper != tb&caseMask {
405 // sb is ASCII and t is not. t must be either kelvin
406 // sign or long s; sb must be s, S, k, or K.
407 tr, size := utf8.DecodeRune(t)
410 if tr != smallLongEss {
429 // asciiEqualFold is a specialization of bytes.EqualFold for use when
430 // s is all ASCII (but may contain non-letters) and contains no
431 // special-folding letters.
432 // See comments on foldFunc.
433 func asciiEqualFold(s, t []byte) bool {
434 if len(s) != len(t) {
437 for i, sb := range s {
442 if ('a' <= sb && sb <= 'z') || ('A' <= sb && sb <= 'Z') {
443 if sb&caseMask != tb&caseMask {
453 // simpleLetterEqualFold is a specialization of bytes.EqualFold for
454 // use when s is all ASCII letters (no underscores, etc) and also
455 // doesn't contain 'k', 'K', 's', or 'S'.
456 // See comments on foldFunc.
457 func simpleLetterEqualFold(s, t []byte) bool {
458 if len(s) != len(t) {
461 for i, b := range s {
462 if b&caseMask != t[i]&caseMask {
469 // tagOptions is the string following a comma in a struct field's "json"
470 // tag, or the empty string. It does not include the leading comma.
471 type tagOptions string
473 // parseTag splits a struct field's json tag into its name and
474 // comma-separated options.
475 func parseTag(tag string) (string, tagOptions) {
476 if idx := strings.Index(tag, ","); idx != -1 {
477 return tag[:idx], tagOptions(tag[idx+1:])
479 return tag, tagOptions("")
482 // Contains reports whether a comma-separated list of options
483 // contains a particular substr flag. substr must be surrounded by a
484 // string boundary or commas.
485 func (o tagOptions) Contains(optionName string) bool {
492 i := strings.Index(s, ",")
494 s, next = s[:i], s[i+1:]