valueunstructured.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. Copyright 2018 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. )
  17. // NewValueInterface creates a Value backed by an "interface{}" type,
  18. // typically an unstructured object in Kubernetes world.
  19. // interface{} must be one of: map[string]interface{}, map[interface{}]interface{}, []interface{}, int types, float types,
  20. // string or boolean. Nested interface{} must also be one of these types.
  21. func NewValueInterface(v interface{}) Value {
  22. return Value(HeapAllocator.allocValueUnstructured().reuse(v))
  23. }
  24. type valueUnstructured struct {
  25. Value interface{}
  26. }
  27. // reuse replaces the value of the valueUnstructured.
  28. func (vi *valueUnstructured) reuse(value interface{}) Value {
  29. vi.Value = value
  30. return vi
  31. }
  32. func (v valueUnstructured) IsMap() bool {
  33. if _, ok := v.Value.(map[string]interface{}); ok {
  34. return true
  35. }
  36. if _, ok := v.Value.(map[interface{}]interface{}); ok {
  37. return true
  38. }
  39. return false
  40. }
  41. func (v valueUnstructured) AsMap() Map {
  42. return v.AsMapUsing(HeapAllocator)
  43. }
  44. func (v valueUnstructured) AsMapUsing(_ Allocator) Map {
  45. if v.Value == nil {
  46. panic("invalid nil")
  47. }
  48. switch t := v.Value.(type) {
  49. case map[string]interface{}:
  50. return mapUnstructuredString(t)
  51. case map[interface{}]interface{}:
  52. return mapUnstructuredInterface(t)
  53. }
  54. panic(fmt.Errorf("not a map: %#v", v))
  55. }
  56. func (v valueUnstructured) IsList() bool {
  57. if v.Value == nil {
  58. return false
  59. }
  60. _, ok := v.Value.([]interface{})
  61. return ok
  62. }
  63. func (v valueUnstructured) AsList() List {
  64. return v.AsListUsing(HeapAllocator)
  65. }
  66. func (v valueUnstructured) AsListUsing(_ Allocator) List {
  67. return listUnstructured(v.Value.([]interface{}))
  68. }
  69. func (v valueUnstructured) IsFloat() bool {
  70. if v.Value == nil {
  71. return false
  72. } else if _, ok := v.Value.(float64); ok {
  73. return true
  74. } else if _, ok := v.Value.(float32); ok {
  75. return true
  76. }
  77. return false
  78. }
  79. func (v valueUnstructured) AsFloat() float64 {
  80. if f, ok := v.Value.(float32); ok {
  81. return float64(f)
  82. }
  83. return v.Value.(float64)
  84. }
  85. func (v valueUnstructured) IsInt() bool {
  86. if v.Value == nil {
  87. return false
  88. } else if _, ok := v.Value.(int); ok {
  89. return true
  90. } else if _, ok := v.Value.(int8); ok {
  91. return true
  92. } else if _, ok := v.Value.(int16); ok {
  93. return true
  94. } else if _, ok := v.Value.(int32); ok {
  95. return true
  96. } else if _, ok := v.Value.(int64); ok {
  97. return true
  98. } else if _, ok := v.Value.(uint); ok {
  99. return true
  100. } else if _, ok := v.Value.(uint8); ok {
  101. return true
  102. } else if _, ok := v.Value.(uint16); ok {
  103. return true
  104. } else if _, ok := v.Value.(uint32); ok {
  105. return true
  106. }
  107. return false
  108. }
  109. func (v valueUnstructured) AsInt() int64 {
  110. if i, ok := v.Value.(int); ok {
  111. return int64(i)
  112. } else if i, ok := v.Value.(int8); ok {
  113. return int64(i)
  114. } else if i, ok := v.Value.(int16); ok {
  115. return int64(i)
  116. } else if i, ok := v.Value.(int32); ok {
  117. return int64(i)
  118. } else if i, ok := v.Value.(uint); ok {
  119. return int64(i)
  120. } else if i, ok := v.Value.(uint8); ok {
  121. return int64(i)
  122. } else if i, ok := v.Value.(uint16); ok {
  123. return int64(i)
  124. } else if i, ok := v.Value.(uint32); ok {
  125. return int64(i)
  126. }
  127. return v.Value.(int64)
  128. }
  129. func (v valueUnstructured) IsString() bool {
  130. if v.Value == nil {
  131. return false
  132. }
  133. _, ok := v.Value.(string)
  134. return ok
  135. }
  136. func (v valueUnstructured) AsString() string {
  137. return v.Value.(string)
  138. }
  139. func (v valueUnstructured) IsBool() bool {
  140. if v.Value == nil {
  141. return false
  142. }
  143. _, ok := v.Value.(bool)
  144. return ok
  145. }
  146. func (v valueUnstructured) AsBool() bool {
  147. return v.Value.(bool)
  148. }
  149. func (v valueUnstructured) IsNull() bool {
  150. return v.Value == nil
  151. }
  152. func (v valueUnstructured) Unstructured() interface{} {
  153. return v.Value
  154. }