This repository has been archived by the owner on May 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
map.go
278 lines (232 loc) · 5.48 KB
/
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
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
package support
import (
"github.com/confetti-framework/errors"
"github.com/spf13/cast"
"reflect"
)
type Map map[string]Value
func NewMap(itemsRange ...interface{}) Map {
result, err := NewMapE(itemsRange...)
if err != nil {
panic(err)
}
return result
}
func NewMapE(itemsRange ...interface{}) (Map, error) {
var err error
result := Map{}
for _, rawItems := range itemsRange {
v := reflect.ValueOf(rawItems)
if v.Kind() != reflect.Map {
err = errors.WithStack(errors.Wrap(CanNotCreateMapError, "type %s", v.Kind().String()))
continue
}
for _, key := range v.MapKeys() {
value := v.MapIndex(key).Interface()
key, err := cast.ToStringE(key.Interface())
if err != nil {
return nil, errors.WithStack(errors.Wrap(err, "invalid key in map"))
}
result[key] = NewValue(value)
}
}
return result, err
}
func (m Map) Raw() interface{} {
result := map[string]interface{}{}
for key, value := range m {
// Handle value
result[key] = value.Raw()
}
return result
}
func (m Map) Get(key string) Value {
result, err := m.GetE(key)
if err != nil {
panic(err)
}
return result
}
// GetE gets the first value associated with the given key.
// If there are no values associated with the key, GetE returns
// nil. To access multiple values, use GetCollection or Collection.
func (m Map) GetE(key string) (Value, error) {
if key == "" {
return NewValue(m), nil
}
currentKey, rest := splitKey(key)
// when you request something with an asterisk, you always develop a collection
if currentKey == "*" {
collection := Collection{}
for _, value := range m {
nestedValueRaw, err := value.GetE(joinRest(rest))
if err != nil {
return nestedValueRaw, err
}
switch nestedValues := nestedValueRaw.source.(type) {
case Collection:
for _, nestedValue := range nestedValues {
collection = collection.Push(nestedValue)
}
case Map:
for _, nestedValue := range nestedValues {
collection = collection.Push(nestedValue)
}
default:
// If there are no keys to search further, the nested value is the final value
collection = collection.Push(nestedValueRaw)
}
}
return NewValue(collection), nil
}
value, found := m[key]
if found {
return value, nil
}
value, found = m[currentKey]
if !found {
return Value{}, errors.Wrap(CanNotFoundValueError, "key '%s'%s", currentKey, getKeyInfo(key, currentKey))
}
switch value.Source().(type) {
case Collection:
return value.Collection().GetE(joinRest(rest))
case Map:
return value.Map().GetE(joinRest(rest))
default:
return value.GetE(joinRest(rest))
}
}
// SetE sets the key to value by dot notation
func (m Map) SetE(key string, input interface{}) (Map, error) {
currentKey, rest := splitKey(key)
value := NewValue(input)
// If we have a dot notation we want to set the value deeper
if key != currentKey {
currentValue := m[currentKey]
nestedValue, err := currentValue.SetE(joinRest(rest), value)
if err != nil {
return m, err
}
m[currentKey] = nestedValue
} else {
m[currentKey] = NewValue(input)
}
return m, nil
}
func (m Map) Only(keys ...string) Map {
result, err := m.OnlyE(keys...)
if err != nil {
panic(err)
}
return result
}
func (m Map) OnlyE(originKeys ...string) (Map, error) {
result := Map{}
var err error
keys := GetSearchableKeys(originKeys, NewValue(m))
for _, key := range keys {
item, err := m.GetE(key)
if err == nil {
result, err = result.SetE(key, item)
}
}
return result, err
}
func (m Map) Except(keys ...string) Map {
result := m.Copy()
for _, key := range keys {
delete(result, key)
}
return result
}
// Push adds the value to key. It appends to any existing values
// associated with key. If the value is in collection, push
// the value to the collection.
func (m Map) Push(key string, input interface{}) Map {
if rawValue, found := m[key]; found {
source := rawValue.Source()
switch source.(type) {
case Collection:
collection := source.(Collection)
m[key] = NewValue(collection.Push(input))
default:
m[key] = NewValue(input)
}
} else {
m[key] = NewValue(input)
}
return m
}
// Delete deletes the values associated with key.
func (m Map) Delete(key string) {
delete(m, key)
}
func (m Map) Collection() Collection {
collection := Collection{}
for _, value := range m {
collection = collection.Push(value)
}
return collection
}
func (m Map) Merge(maps ...Map) Map {
for _, bag := range maps {
for key, item := range bag {
m.Push(key, item)
}
}
return m
}
// Copy generates a new struct with the same data as the old struct
func (m Map) Copy() Map {
newMap := Map{}
for key, value := range m {
newMap[key] = value
}
return newMap
}
func (m Map) First() Value {
return m.Collection().First()
}
func (m Map) Has(keys ...string) bool {
for _, key := range keys {
_, err := m.GetE(key)
if err != nil {
return false
}
}
return true
}
func (m Map) HasAny(keys ...string) bool {
if len(keys) == 0 {
return true
}
for _, key := range keys {
result, err := m.GetE(key)
if err == nil && !result.Empty() {
return true
}
}
return false
}
func (m Map) Missing(keys ...string) bool {
return !m.Has(keys...)
}
func (m Map) Filled(keys ...string) bool {
for _, key := range keys {
result, err := m.GetE(key)
if err != nil || result.Empty() {
return false
}
}
return true
}
func (m Map) Empty() bool {
return len(m) == 0
}
func getKeyInfo(key string, currentKey string) string {
info := ""
if currentKey != key {
info = " ('" + key + "')"
}
return info
}