mapunstructured.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. type mapUnstructuredInterface map[interface{}]interface{}
  15. func (m mapUnstructuredInterface) Set(key string, val Value) {
  16. m[key] = val.Unstructured()
  17. }
  18. func (m mapUnstructuredInterface) Get(key string) (Value, bool) {
  19. return m.GetUsing(HeapAllocator, key)
  20. }
  21. func (m mapUnstructuredInterface) GetUsing(a Allocator, key string) (Value, bool) {
  22. if v, ok := m[key]; !ok {
  23. return nil, false
  24. } else {
  25. return a.allocValueUnstructured().reuse(v), true
  26. }
  27. }
  28. func (m mapUnstructuredInterface) Has(key string) bool {
  29. _, ok := m[key]
  30. return ok
  31. }
  32. func (m mapUnstructuredInterface) Delete(key string) {
  33. delete(m, key)
  34. }
  35. func (m mapUnstructuredInterface) Iterate(fn func(key string, value Value) bool) bool {
  36. return m.IterateUsing(HeapAllocator, fn)
  37. }
  38. func (m mapUnstructuredInterface) IterateUsing(a Allocator, fn func(key string, value Value) bool) bool {
  39. if len(m) == 0 {
  40. return true
  41. }
  42. vv := a.allocValueUnstructured()
  43. defer a.Free(vv)
  44. for k, v := range m {
  45. if ks, ok := k.(string); !ok {
  46. continue
  47. } else {
  48. if !fn(ks, vv.reuse(v)) {
  49. return false
  50. }
  51. }
  52. }
  53. return true
  54. }
  55. func (m mapUnstructuredInterface) Length() int {
  56. return len(m)
  57. }
  58. func (m mapUnstructuredInterface) Empty() bool {
  59. return len(m) == 0
  60. }
  61. func (m mapUnstructuredInterface) Equals(other Map) bool {
  62. return m.EqualsUsing(HeapAllocator, other)
  63. }
  64. func (m mapUnstructuredInterface) EqualsUsing(a Allocator, other Map) bool {
  65. lhsLength := m.Length()
  66. rhsLength := other.Length()
  67. if lhsLength != rhsLength {
  68. return false
  69. }
  70. if lhsLength == 0 {
  71. return true
  72. }
  73. vv := a.allocValueUnstructured()
  74. defer a.Free(vv)
  75. return other.Iterate(func(key string, value Value) bool {
  76. lhsVal, ok := m[key]
  77. if !ok {
  78. return false
  79. }
  80. return Equals(vv.reuse(lhsVal), value)
  81. })
  82. }
  83. func (m mapUnstructuredInterface) Zip(other Map, order MapTraverseOrder, fn func(key string, lhs, rhs Value) bool) bool {
  84. return m.ZipUsing(HeapAllocator, other, order, fn)
  85. }
  86. func (m mapUnstructuredInterface) ZipUsing(a Allocator, other Map, order MapTraverseOrder, fn func(key string, lhs, rhs Value) bool) bool {
  87. return defaultMapZip(a, m, other, order, fn)
  88. }
  89. type mapUnstructuredString map[string]interface{}
  90. func (m mapUnstructuredString) Set(key string, val Value) {
  91. m[key] = val.Unstructured()
  92. }
  93. func (m mapUnstructuredString) Get(key string) (Value, bool) {
  94. return m.GetUsing(HeapAllocator, key)
  95. }
  96. func (m mapUnstructuredString) GetUsing(a Allocator, key string) (Value, bool) {
  97. if v, ok := m[key]; !ok {
  98. return nil, false
  99. } else {
  100. return a.allocValueUnstructured().reuse(v), true
  101. }
  102. }
  103. func (m mapUnstructuredString) Has(key string) bool {
  104. _, ok := m[key]
  105. return ok
  106. }
  107. func (m mapUnstructuredString) Delete(key string) {
  108. delete(m, key)
  109. }
  110. func (m mapUnstructuredString) Iterate(fn func(key string, value Value) bool) bool {
  111. return m.IterateUsing(HeapAllocator, fn)
  112. }
  113. func (m mapUnstructuredString) IterateUsing(a Allocator, fn func(key string, value Value) bool) bool {
  114. if len(m) == 0 {
  115. return true
  116. }
  117. vv := a.allocValueUnstructured()
  118. defer a.Free(vv)
  119. for k, v := range m {
  120. if !fn(k, vv.reuse(v)) {
  121. return false
  122. }
  123. }
  124. return true
  125. }
  126. func (m mapUnstructuredString) Length() int {
  127. return len(m)
  128. }
  129. func (m mapUnstructuredString) Equals(other Map) bool {
  130. return m.EqualsUsing(HeapAllocator, other)
  131. }
  132. func (m mapUnstructuredString) EqualsUsing(a Allocator, other Map) bool {
  133. lhsLength := m.Length()
  134. rhsLength := other.Length()
  135. if lhsLength != rhsLength {
  136. return false
  137. }
  138. if lhsLength == 0 {
  139. return true
  140. }
  141. vv := a.allocValueUnstructured()
  142. defer a.Free(vv)
  143. return other.Iterate(func(key string, value Value) bool {
  144. lhsVal, ok := m[key]
  145. if !ok {
  146. return false
  147. }
  148. return Equals(vv.reuse(lhsVal), value)
  149. })
  150. }
  151. func (m mapUnstructuredString) Zip(other Map, order MapTraverseOrder, fn func(key string, lhs, rhs Value) bool) bool {
  152. return m.ZipUsing(HeapAllocator, other, order, fn)
  153. }
  154. func (m mapUnstructuredString) ZipUsing(a Allocator, other Map, order MapTraverseOrder, fn func(key string, lhs, rhs Value) bool) bool {
  155. return defaultMapZip(a, m, other, order, fn)
  156. }
  157. func (m mapUnstructuredString) Empty() bool {
  158. return len(m) == 0
  159. }