-
Notifications
You must be signed in to change notification settings - Fork 23
/
stringmap.go
326 lines (274 loc) · 6.41 KB
/
stringmap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* === This file is part of ALICE O² ===
*
* Copyright 2020 CERN and copyright holders of ALICE O².
* Author: Teo Mrnjavac <teo.mrnjavac@cern.ch>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* In applying this license CERN does not waive the privileges and
* immunities granted to it by virtue of its status as an
* Intergovernmental Organization or submit itself to any jurisdiction.
*/
package gera
import (
"sync"
"dario.cat/mergo"
"gopkg.in/yaml.v3"
)
type StringMap interface {
Wrap(m StringMap) StringMap
IsHierarchyRoot() bool
HierarchyContains(m StringMap) bool
Unwrap() StringMap
Has(key string) bool
Len() int
Get(key string) (string, bool)
Set(key string, value string) bool
Del(key string) bool
Flattened() (map[string]string, error)
FlattenedParent() (map[string]string, error)
WrappedAndFlattened(m StringMap) (map[string]string, error)
Raw() map[string]string
Copy() StringMap
RawCopy() map[string]string
}
func MakeStringMap() *StringWrapMap {
return &StringWrapMap{
theMap: make(map[string]string),
parent: nil,
}
}
func MakeStringMapWithMap(fromMap map[string]string) *StringWrapMap {
myMap := &StringWrapMap{
theMap: fromMap,
parent: nil,
}
return myMap
}
func FlattenStack(stringMaps ...StringMap) (flattened map[string]string, err error) {
flattenedSM := MakeStringMap()
for _, stringMap := range stringMaps {
var localFlattened map[string]string
localFlattened, err = stringMap.Flattened()
if err != nil {
return
}
flattenedSM = MakeStringMapWithMap(localFlattened).Wrap(flattenedSM).(*StringWrapMap)
}
flattened, err = flattenedSM.Flattened()
return
}
func MakeStringMapWithMapCopy(fromMap map[string]string) *StringWrapMap {
newBackingMap := make(map[string]string)
for k, v := range fromMap {
newBackingMap[k] = v
}
return MakeStringMapWithMap(newBackingMap)
}
type StringWrapMap struct {
theMap map[string]string
parent StringMap
mu sync.RWMutex
}
func (w *StringWrapMap) UnmarshalYAML(unmarshal func(interface{}) error) error {
nodes := make(map[string]yaml.Node)
err := unmarshal(&nodes)
if err == nil {
m := make(map[string]string)
for k, v := range nodes {
if v.Kind == yaml.ScalarNode {
m[k] = v.Value
} else if v.Kind == yaml.MappingNode && v.Tag == "!public" {
type auxType struct {
Value string
}
var aux auxType
err = v.Decode(&aux)
if err != nil {
continue
}
m[k] = aux.Value
}
}
*w = StringWrapMap{
theMap: m,
parent: nil,
}
} else {
*w = StringWrapMap{
theMap: make(map[string]string),
parent: nil,
}
}
return err
}
func (w *StringWrapMap) IsHierarchyRoot() bool {
if w == nil || w.parent != nil {
return false
}
return true
}
func (w *StringWrapMap) HierarchyContains(m StringMap) bool {
if w == nil || w.parent == nil {
return false
}
if w.parent == m {
return true
}
return w.parent.HierarchyContains(m)
}
// Wraps this map around the gera.Map m, which becomes the new parent.
// Returns a pointer to the composite map (i.e. to itself in its new state).
func (w *StringWrapMap) Wrap(m StringMap) StringMap {
if w == nil {
return nil
}
w.parent = m
return w
}
// Unwraps this map from its parent.
// Returns a pointer to the former parent which was just unwrapped.
func (w *StringWrapMap) Unwrap() StringMap {
if w == nil {
return nil
}
p := w.parent
w.parent = nil
return p
}
func (w *StringWrapMap) Get(key string) (value string, ok bool) {
if w == nil || w.theMap == nil {
return "", false
}
w.mu.RLock()
defer w.mu.RUnlock()
if val, ok := w.theMap[key]; ok {
return val, true
}
if w.parent != nil {
return w.parent.Get(key)
}
return "", false
}
func (w *StringWrapMap) Set(key string, value string) (ok bool) {
if w == nil || w.theMap == nil {
return false
}
w.mu.Lock()
defer w.mu.Unlock()
w.theMap[key] = value
return true
}
func (w *StringWrapMap) Del(key string) (ok bool) {
if w == nil || w.theMap == nil {
return false
}
w.mu.Lock()
defer w.mu.Unlock()
if _, exists := w.theMap[key]; exists {
delete(w.theMap, key)
}
return true
}
func (w *StringWrapMap) Has(key string) bool {
_, ok := w.Get(key)
return ok
}
func (w *StringWrapMap) Len() int {
if w == nil || w.theMap == nil {
return 0
}
flattened, err := w.Flattened()
if err != nil {
return 0
}
return len(flattened)
}
func (w *StringWrapMap) Flattened() (map[string]string, error) {
if w == nil {
return nil, nil
}
w.mu.RLock()
defer w.mu.RUnlock()
out := make(map[string]string)
for k, v := range w.theMap {
out[k] = v
}
if w.parent == nil {
return out, nil
}
flattenedParent, err := w.parent.Flattened()
if err != nil {
return out, err
}
err = mergo.Merge(&out, flattenedParent)
return out, err
}
func (w *StringWrapMap) FlattenedParent() (map[string]string, error) {
if w == nil {
return nil, nil
}
if w.parent == nil {
return make(map[string]string), nil
}
return w.parent.Flattened()
}
func (w *StringWrapMap) WrappedAndFlattened(m StringMap) (map[string]string, error) {
if w == nil {
return nil, nil
}
w.mu.RLock()
out := make(map[string]string)
for k, v := range w.theMap {
out[k] = v
}
w.mu.RUnlock()
if m == nil {
return out, nil
}
flattenedM, err := m.Flattened()
if err != nil {
return out, err
}
err = mergo.Merge(&out, flattenedM)
return out, err
}
func (w *StringWrapMap) Raw() map[string]string { // allows unmutexed access to map, can be unsafe!
if w == nil {
return nil
}
return w.theMap
}
func (w *StringWrapMap) Copy() StringMap {
if w == nil {
return nil
}
w.mu.RLock()
defer w.mu.RUnlock()
newMap := &StringWrapMap{
theMap: make(map[string]string, len(w.theMap)),
parent: w.parent,
}
for k, v := range w.theMap {
newMap.theMap[k] = v
}
return newMap
}
func (w *StringWrapMap) RawCopy() map[string]string { // always safe
if w == nil {
return nil
}
return w.Copy().Raw()
}