-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy patharray_map.go
201 lines (168 loc) · 4.15 KB
/
array_map.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
package encoding
import (
"encoding/json"
"fmt"
"sort"
"gopkg.in/yaml.v3"
)
// MapElement is the in-memory representation of the item when stored in a map.
type MapElement interface {
ToArrayEntry(key string) ArrayElement
}
// ArrayElement is the representation of the item when encoded to an array in yaml or json.
type ArrayElement interface {
GetKey() string
ToMapEntry() MapElement
}
// ArrayMap is a map that is represented as an array when marshaled.
type ArrayMap[T MapElement, K ArrayElement] struct {
items map[string]T
}
func MakeArrayMap[T MapElement, K ArrayElement](len int) ArrayMap[T, K] {
return ArrayMap[T, K]{
items: make(map[string]T, len),
}
}
func (m *ArrayMap[T, K]) Len() int {
if m == nil {
return 0
}
return len(m.items)
}
func (m *ArrayMap[T, K]) Items() map[string]T {
if m == nil {
return nil
}
result := make(map[string]T, len(m.items))
for k, v := range m.items {
result[k] = v
}
return result
}
func (m *ArrayMap[T, K]) ItemsSorted() []K {
if m == nil {
return nil
}
result := make([]K, len(m.items))
i := 0
for k, v := range m.items {
// I can't figure out how to constrain T such that ToArrayEntry returns K, so I'm doing a cast
result[i] = v.ToArrayEntry(k).(K)
i++
}
sort.SliceStable(result, func(i, j int) bool {
return result[i].GetKey() < result[j].GetKey()
})
return result
}
func (m *ArrayMap[T, K]) ItemsUnsafe() map[string]T {
if m == nil {
return nil
}
if m.items == nil {
m.items = make(map[string]T)
}
return m.items
}
func (m *ArrayMap[T, K]) Get(key string) (T, bool) {
if m == nil {
return *new(T), false
}
entry, ok := m.items[key]
return entry, ok
}
func (m *ArrayMap[T, K]) Set(key string, entry T) {
if m.items == nil {
m.items = make(map[string]T, 1)
}
m.items[key] = entry
}
func (m *ArrayMap[T, K]) Remove(key string) {
if m == nil {
return
}
delete(m.items, key)
}
func (m *ArrayMap[T, K]) MarshalRaw() interface{} {
if m == nil {
return nil
}
var raw []ArrayElement
if m.items == nil {
return raw
}
raw = make([]ArrayElement, 0, len(m.items))
for k, v := range m.items {
raw = append(raw, v.ToArrayEntry(k))
}
sort.SliceStable(raw, func(i, j int) bool {
return raw[i].GetKey() < raw[j].GetKey()
})
return raw
}
func (m *ArrayMap[T, K]) UnmarshalRaw(raw []K) error {
// If there's nothing to import, stop early and allow the map to keep its original value
// So if someone unmarshalled into a nil map, it stays nil.
// This more closely matches how the stdlib encoders work
if len(raw) == 0 {
return nil
}
if m == nil {
*m = ArrayMap[T, K]{}
}
m.items = make(map[string]T, len(raw))
for _, rawItem := range raw {
if _, hasKey := m.items[rawItem.GetKey()]; hasKey {
return fmt.Errorf("cannot unmarshal source map: duplicate key found '%s'", rawItem.GetKey())
}
item := rawItem.ToMapEntry()
typedItem, ok := item.(T)
if !ok {
return fmt.Errorf("invalid ArrayMap generic types, ArrayElement %T returned a %T from ToMapEntry(), when it should return %T", rawItem, item, *new(T))
}
m.items[rawItem.GetKey()] = typedItem
}
return nil
}
func (m *ArrayMap[T, K]) MarshalJSON() ([]byte, error) {
raw := m.MarshalRaw()
return json.Marshal(raw)
}
func (m *ArrayMap[T, K]) UnmarshalJSON(data []byte) error {
var raw []K
err := json.Unmarshal(data, &raw)
if err != nil {
return err
}
return m.UnmarshalRaw(raw)
}
func (m *ArrayMap[T, K]) UnmarshalYAML(value *yaml.Node) error {
var raw []K
if err := value.Decode(&raw); err != nil {
return err
}
return m.UnmarshalRaw(raw)
}
func (m *ArrayMap[T, K]) MarshalYAML() (interface{}, error) {
if m == nil {
return nil, nil
}
return m.MarshalRaw(), nil
}
// Merge applies the specified values on top of a base set of values. When a
// name exists in both sets, use the value from the overrides
func (m *ArrayMap[T, K]) Merge(overrides *ArrayMap[T, K]) *ArrayMap[T, K] {
result := make(map[string]T, m.Len())
if m != nil {
for k, v := range m.items {
result[k] = v
}
}
if overrides != nil {
// If the name is in the base, overwrite its value with the override provided
for k, v := range overrides.items {
result[k] = v
}
}
return &ArrayMap[T, K]{items: result}
}