field.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. // Because MarshalOptions.TagKey must be explicitly set, use it
  82. // over JSON, which is enabled by default.
  83. if opts.TagKey != "" && fieldTag == (tag{}) {
  84. fieldTag.parseStructTag(opts.TagKey, sf.Tag)
  85. } else if opts.SupportJSONTags && fieldTag == (tag{}) {
  86. fieldTag.parseStructTag("json", sf.Tag)
  87. }
  88. if fieldTag.Ignore {
  89. continue
  90. }
  91. ft := sf.Type
  92. if ft.Name() == "" && ft.Kind() == reflect.Ptr {
  93. ft = ft.Elem()
  94. }
  95. structField := buildField(f.Index, i, sf, fieldTag)
  96. structField.Type = ft
  97. if !sf.Anonymous || ft.Kind() != reflect.Struct {
  98. fields = append(fields, structField)
  99. if count[f.Type] > 1 {
  100. // If there were multiple instances, add a second,
  101. // so that the annihilation code will see a duplicate.
  102. // It only cares about the distinction between 1 or 2,
  103. // so don't bother generating any more copies.
  104. fields = append(fields, structField)
  105. }
  106. continue
  107. }
  108. // Record new anon struct to explore next round
  109. nextCount[ft]++
  110. if nextCount[ft] == 1 {
  111. next = append(next, structField)
  112. }
  113. }
  114. }
  115. }
  116. return fields
  117. }
  118. // visibleFields will return a slice of fields which are visible based on
  119. // Go's standard visiblity rules with the exception of ties being broken
  120. // by depth and struct tag naming.
  121. //
  122. // Based on the enoding/json field filtering of the Go Stdlib
  123. // https://golang.org/src/encoding/json/encode.go typeField func.
  124. func visibleFields(fields []field) []field {
  125. // Delete all fields that are hidden by the Go rules for embedded fields,
  126. // except that fields with JSON tags are promoted.
  127. // The fields are sorted in primary order of name, secondary order
  128. // of field index length. Loop over names; for each name, delete
  129. // hidden fields by choosing the one dominant field that survives.
  130. out := fields[:0]
  131. for advance, i := 0, 0; i < len(fields); i += advance {
  132. // One iteration per name.
  133. // Find the sequence of fields with the name of this first field.
  134. fi := fields[i]
  135. name := fi.Name
  136. for advance = 1; i+advance < len(fields); advance++ {
  137. fj := fields[i+advance]
  138. if fj.Name != name {
  139. break
  140. }
  141. }
  142. if advance == 1 { // Only one field with this name
  143. out = append(out, fi)
  144. continue
  145. }
  146. dominant, ok := dominantField(fields[i : i+advance])
  147. if ok {
  148. out = append(out, dominant)
  149. }
  150. }
  151. fields = out
  152. sort.Sort(fieldsByIndex(fields))
  153. return fields
  154. }
  155. // dominantField looks through the fields, all of which are known to
  156. // have the same name, to find the single field that dominates the
  157. // others using Go's embedding rules, modified by the presence of
  158. // JSON tags. If there are multiple top-level fields, the boolean
  159. // will be false: This condition is an error in Go and we skip all
  160. // the fields.
  161. //
  162. // Based on the enoding/json field filtering of the Go Stdlib
  163. // https://golang.org/src/encoding/json/encode.go dominantField func.
  164. func dominantField(fields []field) (field, bool) {
  165. // The fields are sorted in increasing index-length order. The winner
  166. // must therefore be one with the shortest index length. Drop all
  167. // longer entries, which is easy: just truncate the slice.
  168. length := len(fields[0].Index)
  169. tagged := -1 // Index of first tagged field.
  170. for i, f := range fields {
  171. if len(f.Index) > length {
  172. fields = fields[:i]
  173. break
  174. }
  175. if f.NameFromTag {
  176. if tagged >= 0 {
  177. // Multiple tagged fields at the same level: conflict.
  178. // Return no field.
  179. return field{}, false
  180. }
  181. tagged = i
  182. }
  183. }
  184. if tagged >= 0 {
  185. return fields[tagged], true
  186. }
  187. // All remaining fields have the same length. If there's more than one,
  188. // we have a conflict (two fields named "X" at the same level) and we
  189. // return no field.
  190. if len(fields) > 1 {
  191. return field{}, false
  192. }
  193. return fields[0], true
  194. }
  195. // fieldsByName sorts field by name, breaking ties with depth,
  196. // then breaking ties with "name came from json tag", then
  197. // breaking ties with index sequence.
  198. //
  199. // Based on the enoding/json field filtering of the Go Stdlib
  200. // https://golang.org/src/encoding/json/encode.go fieldsByName type.
  201. type fieldsByName []field
  202. func (x fieldsByName) Len() int { return len(x) }
  203. func (x fieldsByName) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
  204. func (x fieldsByName) Less(i, j int) bool {
  205. if x[i].Name != x[j].Name {
  206. return x[i].Name < x[j].Name
  207. }
  208. if len(x[i].Index) != len(x[j].Index) {
  209. return len(x[i].Index) < len(x[j].Index)
  210. }
  211. if x[i].NameFromTag != x[j].NameFromTag {
  212. return x[i].NameFromTag
  213. }
  214. return fieldsByIndex(x).Less(i, j)
  215. }
  216. // fieldsByIndex sorts field by index sequence.
  217. //
  218. // Based on the enoding/json field filtering of the Go Stdlib
  219. // https://golang.org/src/encoding/json/encode.go fieldsByIndex type.
  220. type fieldsByIndex []field
  221. func (x fieldsByIndex) Len() int { return len(x) }
  222. func (x fieldsByIndex) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
  223. func (x fieldsByIndex) Less(i, j int) bool {
  224. for k, xik := range x[i].Index {
  225. if k >= len(x[j].Index) {
  226. return false
  227. }
  228. if xik != x[j].Index[k] {
  229. return xik < x[j].Index[k]
  230. }
  231. }
  232. return len(x[i].Index) < len(x[j].Index)
  233. }