blob: d8e208628defc4ed93d35005e5bf12151c60c530 [file] [log] [blame]
Matteo Scandoloa4285862020-12-01 18:10:10 -08001/*
2Copyright 2019 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package value
18
19type mapUnstructuredInterface map[interface{}]interface{}
20
21func (m mapUnstructuredInterface) Set(key string, val Value) {
22 m[key] = val.Unstructured()
23}
24
25func (m mapUnstructuredInterface) Get(key string) (Value, bool) {
26 return m.GetUsing(HeapAllocator, key)
27}
28
29func (m mapUnstructuredInterface) GetUsing(a Allocator, key string) (Value, bool) {
30 if v, ok := m[key]; !ok {
31 return nil, false
32 } else {
33 return a.allocValueUnstructured().reuse(v), true
34 }
35}
36
37func (m mapUnstructuredInterface) Has(key string) bool {
38 _, ok := m[key]
39 return ok
40}
41
42func (m mapUnstructuredInterface) Delete(key string) {
43 delete(m, key)
44}
45
46func (m mapUnstructuredInterface) Iterate(fn func(key string, value Value) bool) bool {
47 return m.IterateUsing(HeapAllocator, fn)
48}
49
50func (m mapUnstructuredInterface) IterateUsing(a Allocator, fn func(key string, value Value) bool) bool {
51 if len(m) == 0 {
52 return true
53 }
54 vv := a.allocValueUnstructured()
55 defer a.Free(vv)
56 for k, v := range m {
57 if ks, ok := k.(string); !ok {
58 continue
59 } else {
60 if !fn(ks, vv.reuse(v)) {
61 return false
62 }
63 }
64 }
65 return true
66}
67
68func (m mapUnstructuredInterface) Length() int {
69 return len(m)
70}
71
72func (m mapUnstructuredInterface) Empty() bool {
73 return len(m) == 0
74}
75
76func (m mapUnstructuredInterface) Equals(other Map) bool {
77 return m.EqualsUsing(HeapAllocator, other)
78}
79
80func (m mapUnstructuredInterface) EqualsUsing(a Allocator, other Map) bool {
81 lhsLength := m.Length()
82 rhsLength := other.Length()
83 if lhsLength != rhsLength {
84 return false
85 }
86 if lhsLength == 0 {
87 return true
88 }
89 vv := a.allocValueUnstructured()
90 defer a.Free(vv)
91 return other.Iterate(func(key string, value Value) bool {
92 lhsVal, ok := m[key]
93 if !ok {
94 return false
95 }
96 return Equals(vv.reuse(lhsVal), value)
97 })
98}
99
100func (m mapUnstructuredInterface) Zip(other Map, order MapTraverseOrder, fn func(key string, lhs, rhs Value) bool) bool {
101 return m.ZipUsing(HeapAllocator, other, order, fn)
102}
103
104func (m mapUnstructuredInterface) ZipUsing(a Allocator, other Map, order MapTraverseOrder, fn func(key string, lhs, rhs Value) bool) bool {
105 return defaultMapZip(a, m, other, order, fn)
106}
107
108type mapUnstructuredString map[string]interface{}
109
110func (m mapUnstructuredString) Set(key string, val Value) {
111 m[key] = val.Unstructured()
112}
113
114func (m mapUnstructuredString) Get(key string) (Value, bool) {
115 return m.GetUsing(HeapAllocator, key)
116}
117func (m mapUnstructuredString) GetUsing(a Allocator, key string) (Value, bool) {
118 if v, ok := m[key]; !ok {
119 return nil, false
120 } else {
121 return a.allocValueUnstructured().reuse(v), true
122 }
123}
124
125func (m mapUnstructuredString) Has(key string) bool {
126 _, ok := m[key]
127 return ok
128}
129
130func (m mapUnstructuredString) Delete(key string) {
131 delete(m, key)
132}
133
134func (m mapUnstructuredString) Iterate(fn func(key string, value Value) bool) bool {
135 return m.IterateUsing(HeapAllocator, fn)
136}
137
138func (m mapUnstructuredString) IterateUsing(a Allocator, fn func(key string, value Value) bool) bool {
139 if len(m) == 0 {
140 return true
141 }
142 vv := a.allocValueUnstructured()
143 defer a.Free(vv)
144 for k, v := range m {
145 if !fn(k, vv.reuse(v)) {
146 return false
147 }
148 }
149 return true
150}
151
152func (m mapUnstructuredString) Length() int {
153 return len(m)
154}
155
156func (m mapUnstructuredString) Equals(other Map) bool {
157 return m.EqualsUsing(HeapAllocator, other)
158}
159
160func (m mapUnstructuredString) EqualsUsing(a Allocator, other Map) bool {
161 lhsLength := m.Length()
162 rhsLength := other.Length()
163 if lhsLength != rhsLength {
164 return false
165 }
166 if lhsLength == 0 {
167 return true
168 }
169 vv := a.allocValueUnstructured()
170 defer a.Free(vv)
171 return other.Iterate(func(key string, value Value) bool {
172 lhsVal, ok := m[key]
173 if !ok {
174 return false
175 }
176 return Equals(vv.reuse(lhsVal), value)
177 })
178}
179
180func (m mapUnstructuredString) Zip(other Map, order MapTraverseOrder, fn func(key string, lhs, rhs Value) bool) bool {
181 return m.ZipUsing(HeapAllocator, other, order, fn)
182}
183
184func (m mapUnstructuredString) ZipUsing(a Allocator, other Map, order MapTraverseOrder, fn func(key string, lhs, rhs Value) bool) bool {
185 return defaultMapZip(a, m, other, order, fn)
186}
187
188func (m mapUnstructuredString) Empty() bool {
189 return len(m) == 0
190}