field.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package dynamodbattribute
  2. import (
  3. "reflect"
  4. "sort"
  5. "strings"
  6. )
  7. type field struct {
  8. tag
  9. Name string
  10. NameFromTag bool
  11. Index []int
  12. Type reflect.Type
  13. }
  14. func fieldByName(fields []field, name string) (field, bool) {
  15. foldExists := false
  16. foldField := field{}
  17. for _, f := range fields {
  18. if f.Name == name {
  19. return f, true
  20. }
  21. if !foldExists && strings.EqualFold(f.Name, name) {
  22. foldField = f
  23. foldExists = true
  24. }
  25. }
  26. return foldField, foldExists
  27. }
  28. func buildField(pIdx []int, i int, sf reflect.StructField, fieldTag tag) field {
  29. f := field{
  30. Name: sf.Name,
  31. Type: sf.Type,
  32. tag: fieldTag,
  33. }
  34. if len(fieldTag.Name) != 0 {
  35. f.NameFromTag = true
  36. f.Name = fieldTag.Name
  37. }
  38. f.Index = make([]int, len(pIdx)+1)
  39. copy(f.Index, pIdx)
  40. f.Index[len(pIdx)] = i
  41. return f
  42. }
  43. func unionStructFields(t reflect.Type, opts MarshalOptions) []field {
  44. fields := enumFields(t, opts)
  45. sort.Sort(fieldsByName(fields))
  46. fields = visibleFields(fields)
  47. return fields
  48. }
  49. // enumFields will recursively iterate through a structure and its nested
  50. // anonymous fields.
  51. //
  52. // Based on the enoding/json struct field enumeration of the Go Stdlib
  53. // https://golang.org/src/encoding/json/encode.go typeField func.
  54. func enumFields(t reflect.Type, opts MarshalOptions) []field {
  55. // Fields to explore
  56. current := []field{}
  57. next := []field{{Type: t}}
  58. // count of queued names
  59. count := map[reflect.Type]int{}
  60. nextCount := map[reflect.Type]int{}
  61. visited := map[reflect.Type]struct{}{}
  62. fields := []field{}
  63. for len(next) > 0 {
  64. current, next = next, current[:0]
  65. count, nextCount = nextCount, map[reflect.Type]int{}
  66. for _, f := range current {
  67. if _, ok := visited[f.Type]; ok {
  68. continue
  69. }
  70. visited[f.Type] = struct{}{}
  71. for i := 0; i < f.Type.NumField(); i++ {
  72. sf := f.Type.Field(i)
  73. if sf.PkgPath != "" && !sf.Anonymous {
  74. // Ignore unexported and non-anonymous fields
  75. // unexported but anonymous field may still be used if
  76. // the type has exported nested fields
  77. continue
  78. }
  79. fieldTag := tag{}
  80. fieldTag.parseAVTag(sf.Tag)
  81. if opts.SupportJSONTags && fieldTag == (tag{}) {
  82. fieldTag.parseJSONTag(sf.Tag)
  83. }
  84. if fieldTag.Ignore {
  85. continue
  86. }
  87. ft := sf.Type
  88. if ft.Name() == "" && ft.Kind() == reflect.Ptr {
  89. ft = ft.Elem()
  90. }
  91. structField := buildField(f.Index, i, sf, fieldTag)
  92. structField.Type = ft
  93. if !sf.Anonymous || ft.Kind() != reflect.Struct {
  94. fields = append(fields, structField)
  95. if count[f.Type] > 1 {
  96. // If there were multiple instances, add a second,
  97. // so that the annihilation code will see a duplicate.
  98. // It only cares about the distinction between 1 or 2,
  99. // so don't bother generating any more copies.
  100. fields = append(fields, structField)
  101. }
  102. continue
  103. }
  104. // Record new anon struct to explore next round
  105. nextCount[ft]++
  106. if nextCount[ft] == 1 {
  107. next = append(next, structField)
  108. }
  109. }
  110. }
  111. }
  112. return fields
  113. }
  114. // visibleFields will return a slice of fields which are visible based on
  115. // Go's standard visiblity rules with the exception of ties being broken
  116. // by depth and struct tag naming.
  117. //
  118. // Based on the enoding/json field filtering of the Go Stdlib
  119. // https://golang.org/src/encoding/json/encode.go typeField func.
  120. func visibleFields(fields []field) []field {
  121. // Delete all fields that are hidden by the Go rules for embedded fields,
  122. // except that fields with JSON tags are promoted.
  123. // The fields are sorted in primary order of name, secondary order
  124. // of field index length. Loop over names; for each name, delete
  125. // hidden fields by choosing the one dominant field that survives.
  126. out := fields[:0]
  127. for advance, i := 0, 0; i < len(fields); i += advance {
  128. // One iteration per name.
  129. // Find the sequence of fields with the name of this first field.
  130. fi := fields[i]
  131. name := fi.Name
  132. for advance = 1; i+advance < len(fields); advance++ {
  133. fj := fields[i+advance]
  134. if fj.Name != name {
  135. break
  136. }
  137. }
  138. if advance == 1 { // Only one field with this name
  139. out = append(out, fi)
  140. continue
  141. }
  142. dominant, ok := dominantField(fields[i : i+advance])
  143. if ok {
  144. out = append(out, dominant)
  145. }
  146. }
  147. fields = out
  148. sort.Sort(fieldsByIndex(fields))
  149. return fields
  150. }
  151. // dominantField looks through the fields, all of which are known to
  152. // have the same name, to find the single field that dominates the
  153. // others using Go's embedding rules, modified by the presence of
  154. // JSON tags. If there are multiple top-level fields, the boolean
  155. // will be false: This condition is an error in Go and we skip all
  156. // the fields.
  157. //
  158. // Based on the enoding/json field filtering of the Go Stdlib
  159. // https://golang.org/src/encoding/json/encode.go dominantField func.
  160. func dominantField(fields []field) (field, bool) {
  161. // The fields are sorted in increasing index-length order. The winner
  162. // must therefore be one with the shortest index length. Drop all
  163. // longer entries, which is easy: just truncate the slice.
  164. length := len(fields[0].Index)
  165. tagged := -1 // Index of first tagged field.
  166. for i, f := range fields {
  167. if len(f.Index) > length {
  168. fields = fields[:i]
  169. break
  170. }
  171. if f.NameFromTag {
  172. if tagged >= 0 {
  173. // Multiple tagged fields at the same level: conflict.
  174. // Return no field.
  175. return field{}, false
  176. }
  177. tagged = i
  178. }
  179. }
  180. if tagged >= 0 {
  181. return fields[tagged], true
  182. }
  183. // All remaining fields have the same length. If there's more than one,
  184. // we have a conflict (two fields named "X" at the same level) and we
  185. // return no field.
  186. if len(fields) > 1 {
  187. return field{}, false
  188. }
  189. return fields[0], true
  190. }
  191. // fieldsByName sorts field by name, breaking ties with depth,
  192. // then breaking ties with "name came from json tag", then
  193. // breaking ties with index sequence.
  194. //
  195. // Based on the enoding/json field filtering of the Go Stdlib
  196. // https://golang.org/src/encoding/json/encode.go fieldsByName type.
  197. type fieldsByName []field
  198. func (x fieldsByName) Len() int { return len(x) }
  199. func (x fieldsByName) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
  200. func (x fieldsByName) Less(i, j int) bool {
  201. if x[i].Name != x[j].Name {
  202. return x[i].Name < x[j].Name
  203. }
  204. if len(x[i].Index) != len(x[j].Index) {
  205. return len(x[i].Index) < len(x[j].Index)
  206. }
  207. if x[i].NameFromTag != x[j].NameFromTag {
  208. return x[i].NameFromTag
  209. }
  210. return fieldsByIndex(x).Less(i, j)
  211. }
  212. // fieldsByIndex sorts field by index sequence.
  213. //
  214. // Based on the enoding/json field filtering of the Go Stdlib
  215. // https://golang.org/src/encoding/json/encode.go fieldsByIndex type.
  216. type fieldsByIndex []field
  217. func (x fieldsByIndex) Len() int { return len(x) }
  218. func (x fieldsByIndex) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
  219. func (x fieldsByIndex) Less(i, j int) bool {
  220. for k, xik := range x[i].Index {
  221. if k >= len(x[j].Index) {
  222. return false
  223. }
  224. if xik != x[j].Index[k] {
  225. return xik < x[j].Index[k]
  226. }
  227. }
  228. return len(x[i].Index) < len(x[j].Index)
  229. }