valuereflect.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. "encoding/base64"
  16. "fmt"
  17. "reflect"
  18. )
  19. // NewValueReflect creates a Value backed by an "interface{}" type,
  20. // typically an structured object in Kubernetes world that is uses reflection to expose.
  21. // The provided "interface{}" value must be a pointer so that the value can be modified via reflection.
  22. // The provided "interface{}" may contain structs and types that are converted to Values
  23. // by the jsonMarshaler interface.
  24. func NewValueReflect(value interface{}) (Value, error) {
  25. if value == nil {
  26. return NewValueInterface(nil), nil
  27. }
  28. v := reflect.ValueOf(value)
  29. if v.Kind() != reflect.Ptr {
  30. // The root value to reflect on must be a pointer so that map.Set() and map.Delete() operations are possible.
  31. return nil, fmt.Errorf("value provided to NewValueReflect must be a pointer")
  32. }
  33. return wrapValueReflect(v, nil, nil)
  34. }
  35. // wrapValueReflect wraps the provide reflect.Value as a value. If parent in the data tree is a map, parentMap
  36. // and parentMapKey must be provided so that the returned value may be set and deleted.
  37. func wrapValueReflect(value reflect.Value, parentMap, parentMapKey *reflect.Value) (Value, error) {
  38. val := HeapAllocator.allocValueReflect()
  39. return val.reuse(value, nil, parentMap, parentMapKey)
  40. }
  41. // wrapValueReflect wraps the provide reflect.Value as a value, and panics if there is an error. If parent in the data
  42. // tree is a map, parentMap and parentMapKey must be provided so that the returned value may be set and deleted.
  43. func mustWrapValueReflect(value reflect.Value, parentMap, parentMapKey *reflect.Value) Value {
  44. v, err := wrapValueReflect(value, parentMap, parentMapKey)
  45. if err != nil {
  46. panic(err)
  47. }
  48. return v
  49. }
  50. // the value interface doesn't care about the type for value.IsNull, so we can use a constant
  51. var nilType = reflect.TypeOf(&struct{}{})
  52. // reuse replaces the value of the valueReflect. If parent in the data tree is a map, parentMap and parentMapKey
  53. // must be provided so that the returned value may be set and deleted.
  54. func (r *valueReflect) reuse(value reflect.Value, cacheEntry *TypeReflectCacheEntry, parentMap, parentMapKey *reflect.Value) (Value, error) {
  55. if cacheEntry == nil {
  56. cacheEntry = TypeReflectEntryOf(value.Type())
  57. }
  58. if cacheEntry.CanConvertToUnstructured() {
  59. u, err := cacheEntry.ToUnstructured(value)
  60. if err != nil {
  61. return nil, err
  62. }
  63. if u == nil {
  64. value = reflect.Zero(nilType)
  65. } else {
  66. value = reflect.ValueOf(u)
  67. }
  68. }
  69. r.Value = dereference(value)
  70. r.ParentMap = parentMap
  71. r.ParentMapKey = parentMapKey
  72. r.kind = kind(r.Value)
  73. return r, nil
  74. }
  75. // mustReuse replaces the value of the valueReflect and panics if there is an error. If parent in the data tree is a
  76. // map, parentMap and parentMapKey must be provided so that the returned value may be set and deleted.
  77. func (r *valueReflect) mustReuse(value reflect.Value, cacheEntry *TypeReflectCacheEntry, parentMap, parentMapKey *reflect.Value) Value {
  78. v, err := r.reuse(value, cacheEntry, parentMap, parentMapKey)
  79. if err != nil {
  80. panic(err)
  81. }
  82. return v
  83. }
  84. func dereference(val reflect.Value) reflect.Value {
  85. kind := val.Kind()
  86. if (kind == reflect.Interface || kind == reflect.Ptr) && !safeIsNil(val) {
  87. return val.Elem()
  88. }
  89. return val
  90. }
  91. type valueReflect struct {
  92. ParentMap *reflect.Value
  93. ParentMapKey *reflect.Value
  94. Value reflect.Value
  95. kind reflectType
  96. }
  97. func (r valueReflect) IsMap() bool {
  98. return r.kind == mapType || r.kind == structMapType
  99. }
  100. func (r valueReflect) IsList() bool {
  101. return r.kind == listType
  102. }
  103. func (r valueReflect) IsBool() bool {
  104. return r.kind == boolType
  105. }
  106. func (r valueReflect) IsInt() bool {
  107. return r.kind == intType || r.kind == uintType
  108. }
  109. func (r valueReflect) IsFloat() bool {
  110. return r.kind == floatType
  111. }
  112. func (r valueReflect) IsString() bool {
  113. return r.kind == stringType || r.kind == byteStringType
  114. }
  115. func (r valueReflect) IsNull() bool {
  116. return r.kind == nullType
  117. }
  118. type reflectType = int
  119. const (
  120. mapType = iota
  121. structMapType
  122. listType
  123. intType
  124. uintType
  125. floatType
  126. stringType
  127. byteStringType
  128. boolType
  129. nullType
  130. )
  131. func kind(v reflect.Value) reflectType {
  132. typ := v.Type()
  133. rk := typ.Kind()
  134. switch rk {
  135. case reflect.Map:
  136. if v.IsNil() {
  137. return nullType
  138. }
  139. return mapType
  140. case reflect.Struct:
  141. return structMapType
  142. case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
  143. return intType
  144. case reflect.Uint, reflect.Uint32, reflect.Uint16, reflect.Uint8:
  145. // Uint64 deliberately excluded, see valueUnstructured.Int.
  146. return uintType
  147. case reflect.Float64, reflect.Float32:
  148. return floatType
  149. case reflect.String:
  150. return stringType
  151. case reflect.Bool:
  152. return boolType
  153. case reflect.Slice:
  154. if v.IsNil() {
  155. return nullType
  156. }
  157. elemKind := typ.Elem().Kind()
  158. if elemKind == reflect.Uint8 {
  159. return byteStringType
  160. }
  161. return listType
  162. case reflect.Chan, reflect.Func, reflect.Ptr, reflect.UnsafePointer, reflect.Interface:
  163. if v.IsNil() {
  164. return nullType
  165. }
  166. panic(fmt.Sprintf("unsupported type: %v", v.Type()))
  167. default:
  168. panic(fmt.Sprintf("unsupported type: %v", v.Type()))
  169. }
  170. }
  171. // TODO find a cleaner way to avoid panics from reflect.IsNil()
  172. func safeIsNil(v reflect.Value) bool {
  173. k := v.Kind()
  174. switch k {
  175. case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.UnsafePointer, reflect.Interface, reflect.Slice:
  176. return v.IsNil()
  177. }
  178. return false
  179. }
  180. func (r valueReflect) AsMap() Map {
  181. return r.AsMapUsing(HeapAllocator)
  182. }
  183. func (r valueReflect) AsMapUsing(a Allocator) Map {
  184. switch r.kind {
  185. case structMapType:
  186. v := a.allocStructReflect()
  187. v.valueReflect = r
  188. return v
  189. case mapType:
  190. v := a.allocMapReflect()
  191. v.valueReflect = r
  192. return v
  193. default:
  194. panic("value is not a map or struct")
  195. }
  196. }
  197. func (r valueReflect) AsList() List {
  198. return r.AsListUsing(HeapAllocator)
  199. }
  200. func (r valueReflect) AsListUsing(a Allocator) List {
  201. if r.IsList() {
  202. v := a.allocListReflect()
  203. v.Value = r.Value
  204. return v
  205. }
  206. panic("value is not a list")
  207. }
  208. func (r valueReflect) AsBool() bool {
  209. if r.IsBool() {
  210. return r.Value.Bool()
  211. }
  212. panic("value is not a bool")
  213. }
  214. func (r valueReflect) AsInt() int64 {
  215. if r.kind == intType {
  216. return r.Value.Int()
  217. }
  218. if r.kind == uintType {
  219. return int64(r.Value.Uint())
  220. }
  221. panic("value is not an int")
  222. }
  223. func (r valueReflect) AsFloat() float64 {
  224. if r.IsFloat() {
  225. return r.Value.Float()
  226. }
  227. panic("value is not a float")
  228. }
  229. func (r valueReflect) AsString() string {
  230. switch r.kind {
  231. case stringType:
  232. return r.Value.String()
  233. case byteStringType:
  234. return base64.StdEncoding.EncodeToString(r.Value.Bytes())
  235. }
  236. panic("value is not a string")
  237. }
  238. func (r valueReflect) Unstructured() interface{} {
  239. val := r.Value
  240. switch {
  241. case r.IsNull():
  242. return nil
  243. case val.Kind() == reflect.Struct:
  244. return structReflect{r}.Unstructured()
  245. case val.Kind() == reflect.Map:
  246. return mapReflect{valueReflect: r}.Unstructured()
  247. case r.IsList():
  248. return listReflect{r.Value}.Unstructured()
  249. case r.IsString():
  250. return r.AsString()
  251. case r.IsInt():
  252. return r.AsInt()
  253. case r.IsBool():
  254. return r.AsBool()
  255. case r.IsFloat():
  256. return r.AsFloat()
  257. default:
  258. panic(fmt.Sprintf("value of type %s is not a supported by value reflector", val.Type()))
  259. }
  260. }