structreflect.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package value
  14. import (
  15. "fmt"
  16. "reflect"
  17. )
  18. type structReflect struct {
  19. valueReflect
  20. }
  21. func (r structReflect) Length() int {
  22. i := 0
  23. eachStructField(r.Value, func(_ *TypeReflectCacheEntry, s string, value reflect.Value) bool {
  24. i++
  25. return true
  26. })
  27. return i
  28. }
  29. func (r structReflect) Empty() bool {
  30. return eachStructField(r.Value, func(_ *TypeReflectCacheEntry, s string, value reflect.Value) bool {
  31. return false // exit early if the struct is non-empty
  32. })
  33. }
  34. func (r structReflect) Get(key string) (Value, bool) {
  35. return r.GetUsing(HeapAllocator, key)
  36. }
  37. func (r structReflect) GetUsing(a Allocator, key string) (Value, bool) {
  38. if val, ok := r.findJsonNameField(key); ok {
  39. return a.allocValueReflect().mustReuse(val, nil, nil, nil), true
  40. }
  41. return nil, false
  42. }
  43. func (r structReflect) Has(key string) bool {
  44. _, ok := r.findJsonNameField(key)
  45. return ok
  46. }
  47. func (r structReflect) Set(key string, val Value) {
  48. fieldEntry, ok := TypeReflectEntryOf(r.Value.Type()).Fields()[key]
  49. if !ok {
  50. panic(fmt.Sprintf("key %s may not be set on struct %T: field does not exist", key, r.Value.Interface()))
  51. }
  52. oldVal := fieldEntry.GetFrom(r.Value)
  53. newVal := reflect.ValueOf(val.Unstructured())
  54. r.update(fieldEntry, key, oldVal, newVal)
  55. }
  56. func (r structReflect) Delete(key string) {
  57. fieldEntry, ok := TypeReflectEntryOf(r.Value.Type()).Fields()[key]
  58. if !ok {
  59. panic(fmt.Sprintf("key %s may not be deleted on struct %T: field does not exist", key, r.Value.Interface()))
  60. }
  61. oldVal := fieldEntry.GetFrom(r.Value)
  62. if oldVal.Kind() != reflect.Ptr && !fieldEntry.isOmitEmpty {
  63. panic(fmt.Sprintf("key %s may not be deleted on struct: %T: value is neither a pointer nor an omitempty field", key, r.Value.Interface()))
  64. }
  65. r.update(fieldEntry, key, oldVal, reflect.Zero(oldVal.Type()))
  66. }
  67. func (r structReflect) update(fieldEntry *FieldCacheEntry, key string, oldVal, newVal reflect.Value) {
  68. if oldVal.CanSet() {
  69. oldVal.Set(newVal)
  70. return
  71. }
  72. // map items are not addressable, so if a struct is contained in a map, the only way to modify it is
  73. // to write a replacement fieldEntry into the map.
  74. if r.ParentMap != nil {
  75. if r.ParentMapKey == nil {
  76. panic("ParentMapKey must not be nil if ParentMap is not nil")
  77. }
  78. replacement := reflect.New(r.Value.Type()).Elem()
  79. fieldEntry.GetFrom(replacement).Set(newVal)
  80. r.ParentMap.SetMapIndex(*r.ParentMapKey, replacement)
  81. return
  82. }
  83. // This should never happen since NewValueReflect ensures that the root object reflected on is a pointer and map
  84. // item replacement is handled above.
  85. panic(fmt.Sprintf("key %s may not be modified on struct: %T: struct is not settable", key, r.Value.Interface()))
  86. }
  87. func (r structReflect) Iterate(fn func(string, Value) bool) bool {
  88. return r.IterateUsing(HeapAllocator, fn)
  89. }
  90. func (r structReflect) IterateUsing(a Allocator, fn func(string, Value) bool) bool {
  91. vr := a.allocValueReflect()
  92. defer a.Free(vr)
  93. return eachStructField(r.Value, func(e *TypeReflectCacheEntry, s string, value reflect.Value) bool {
  94. return fn(s, vr.mustReuse(value, e, nil, nil))
  95. })
  96. }
  97. func eachStructField(structVal reflect.Value, fn func(*TypeReflectCacheEntry, string, reflect.Value) bool) bool {
  98. for _, fieldCacheEntry := range TypeReflectEntryOf(structVal.Type()).OrderedFields() {
  99. fieldVal := fieldCacheEntry.GetFrom(structVal)
  100. if fieldCacheEntry.CanOmit(fieldVal) {
  101. // omit it
  102. continue
  103. }
  104. ok := fn(fieldCacheEntry.TypeEntry, fieldCacheEntry.JsonName, fieldVal)
  105. if !ok {
  106. return false
  107. }
  108. }
  109. return true
  110. }
  111. func (r structReflect) Unstructured() interface{} {
  112. // Use number of struct fields as a cheap way to rough estimate map size
  113. result := make(map[string]interface{}, r.Value.NumField())
  114. r.Iterate(func(s string, value Value) bool {
  115. result[s] = value.Unstructured()
  116. return true
  117. })
  118. return result
  119. }
  120. func (r structReflect) Equals(m Map) bool {
  121. return r.EqualsUsing(HeapAllocator, m)
  122. }
  123. func (r structReflect) EqualsUsing(a Allocator, m Map) bool {
  124. // MapEquals uses zip and is fairly efficient for structReflect
  125. return MapEqualsUsing(a, &r, m)
  126. }
  127. func (r structReflect) findJsonNameFieldAndNotEmpty(jsonName string) (reflect.Value, bool) {
  128. structCacheEntry, ok := TypeReflectEntryOf(r.Value.Type()).Fields()[jsonName]
  129. if !ok {
  130. return reflect.Value{}, false
  131. }
  132. fieldVal := structCacheEntry.GetFrom(r.Value)
  133. return fieldVal, !structCacheEntry.CanOmit(fieldVal)
  134. }
  135. func (r structReflect) findJsonNameField(jsonName string) (val reflect.Value, ok bool) {
  136. structCacheEntry, ok := TypeReflectEntryOf(r.Value.Type()).Fields()[jsonName]
  137. if !ok {
  138. return reflect.Value{}, false
  139. }
  140. fieldVal := structCacheEntry.GetFrom(r.Value)
  141. return fieldVal, !structCacheEntry.CanOmit(fieldVal)
  142. }
  143. func (r structReflect) Zip(other Map, order MapTraverseOrder, fn func(key string, lhs, rhs Value) bool) bool {
  144. return r.ZipUsing(HeapAllocator, other, order, fn)
  145. }
  146. func (r structReflect) ZipUsing(a Allocator, other Map, order MapTraverseOrder, fn func(key string, lhs, rhs Value) bool) bool {
  147. if otherStruct, ok := other.(*structReflect); ok && r.Value.Type() == otherStruct.Value.Type() {
  148. lhsvr, rhsvr := a.allocValueReflect(), a.allocValueReflect()
  149. defer a.Free(lhsvr)
  150. defer a.Free(rhsvr)
  151. return r.structZip(otherStruct, lhsvr, rhsvr, fn)
  152. }
  153. return defaultMapZip(a, &r, other, order, fn)
  154. }
  155. // structZip provides an optimized zip for structReflect types. The zip is always lexical key ordered since there is
  156. // no additional cost to ordering the zip for structured types.
  157. func (r structReflect) structZip(other *structReflect, lhsvr, rhsvr *valueReflect, fn func(key string, lhs, rhs Value) bool) bool {
  158. lhsVal := r.Value
  159. rhsVal := other.Value
  160. for _, fieldCacheEntry := range TypeReflectEntryOf(lhsVal.Type()).OrderedFields() {
  161. lhsFieldVal := fieldCacheEntry.GetFrom(lhsVal)
  162. rhsFieldVal := fieldCacheEntry.GetFrom(rhsVal)
  163. lhsOmit := fieldCacheEntry.CanOmit(lhsFieldVal)
  164. rhsOmit := fieldCacheEntry.CanOmit(rhsFieldVal)
  165. if lhsOmit && rhsOmit {
  166. continue
  167. }
  168. var lhsVal, rhsVal Value
  169. if !lhsOmit {
  170. lhsVal = lhsvr.mustReuse(lhsFieldVal, fieldCacheEntry.TypeEntry, nil, nil)
  171. }
  172. if !rhsOmit {
  173. rhsVal = rhsvr.mustReuse(rhsFieldVal, fieldCacheEntry.TypeEntry, nil, nil)
  174. }
  175. if !fn(fieldCacheEntry.JsonName, lhsVal, rhsVal) {
  176. return false
  177. }
  178. }
  179. return true
  180. }