This repository has been archived by the owner on Nov 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.go
254 lines (217 loc) · 6.03 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
/*************************************************************************
* MIT License
* Copyright (c) 2021 Model Rocket
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package generic
import (
"os"
"reflect"
"strconv"
"strings"
"unicode"
"github.com/spf13/cast"
)
type (
// Map provides a map wrapper interface for working easily with unknown map types
Map interface {
IsSet(key interface{}) bool
Set(key, value interface{})
Get(key interface{}) Value
Sub(key interface{}) Map
Delete(key interface{})
DeleteAll(keys ...interface{})
Copy() Map
With(keys ...interface{}) Map
Without(keys ...interface{}) Map
Values() interface{}
Keys() interface{}
Map() map[string]interface{}
ForEach(func(key, value interface{}))
}
reflectMap struct {
v reflect.Value
}
)
// MakeMap returns a Mapper from the passed map
func MakeMap(m interface{}) Map {
v := reflect.ValueOf(m)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
if v.Kind() != reflect.Map {
panic("not a map")
}
return &reflectMap{v: v}
}
func (m reflectMap) IsSet(key interface{}) bool {
return m.v.MapIndex(reflect.ValueOf(key)).IsValid()
}
// Set sets a value in the map
func (m reflectMap) Set(key, value interface{}) {
m.v.SetMapIndex(reflect.ValueOf(key), reflect.ValueOf(value))
}
// Keys returns the keys from the map
func (m reflectMap) Keys() interface{} {
keySlice := reflect.SliceOf(m.v.Type().Key())
keys := reflect.MakeSlice(keySlice, m.v.Len(), m.v.Len())
for i, key := range m.v.MapKeys() {
idx := keys.Index(i)
idx.Set(key)
}
return keys.Interface()
}
// Values returns the values from the map
func (m reflectMap) Values() interface{} {
valSlice := reflect.SliceOf(m.v.Type().Elem())
values := reflect.MakeSlice(valSlice, m.v.Len(), m.v.Len())
for i, key := range m.v.MapKeys() {
val := m.v.MapIndex(key)
idx := values.Index(i)
idx.Set(val)
}
return values.Interface()
}
// Get returns a value from a map
func (m reflectMap) Get(key interface{}) Value {
v := m.v.MapIndex(reflect.ValueOf(key))
if !v.CanInterface() {
return nil
}
return MakeValue(v.Interface())
}
// Sub returns a sub map
func (m reflectMap) Sub(key interface{}) Map {
v := m.v.MapIndex(reflect.ValueOf(key))
if !v.CanInterface() {
return nil
}
if v.Kind() != reflect.Map {
return nil
}
return MakeMap(v.Interface())
}
// Delete removes a value from a map
func (m reflectMap) Delete(key interface{}) {
m.v.SetMapIndex(reflect.ValueOf(key), reflect.Value{})
}
// DeleteAll removes a value from a map
func (m reflectMap) DeleteAll(keys ...interface{}) {
for _, key := range keys {
m.v.SetMapIndex(reflect.ValueOf(key), reflect.Value{})
}
}
// Copy returns a copy of a map
func (m reflectMap) Copy() Map {
dst := reflect.New(reflect.TypeOf(m.v))
for _, k := range m.v.MapKeys() {
ov := m.v.MapIndex(k)
dst.SetMapIndex(k, ov)
}
return &reflectMap{dst}
}
// Cast returns a Caster to convert a value to another type
func (m reflectMap) Cast(key interface{}) Value {
v := m.v.MapIndex(reflect.ValueOf(key))
if !v.CanInterface() {
return MakeValue(nil)
}
return MakeValue(v.Interface())
}
// Without returns a copy of the map without the specified keys
func (m reflectMap) Without(keys ...interface{}) Map {
dst := m.Copy()
for _, key := range keys {
dst.Delete(key)
}
return dst
}
// With returns a copy of the map with the specified keys
func (m reflectMap) With(keys ...interface{}) Map {
dst := reflect.New(reflect.TypeOf(m.v))
for _, key := range keys {
dst.SetMapIndex(reflect.ValueOf(key), reflect.ValueOf(m.Get(key)))
}
return MakeMap(dst.Interface())
}
func (m reflectMap) Map() map[string]interface{} {
rval := make(map[string]interface{})
m.ForEach(func(k, v interface{}) {
rval[cast.ToString(k)] = v
})
return rval
}
func (m reflectMap) ForEach(itr func(key, value interface{})) {
for _, key := range m.v.MapKeys() {
itr(key, m.Get(key))
}
}
// ParseString parses a mapping string and returns a params object
// Example string: foo=bar aparam="value"
func ParseString(s string) Map {
m := make(map[string]interface{})
lastQuote := rune(0)
f := func(c rune) bool {
switch {
case c == lastQuote:
lastQuote = rune(0)
return false
case lastQuote != rune(0):
return false
case unicode.In(c, unicode.Quotation_Mark):
lastQuote = c
return false
default:
return unicode.IsSpace(c)
}
}
// splitting string by space but considering quoted section
items := strings.FieldsFunc(s, f)
for _, item := range items {
x := strings.SplitN(item, "=", 2)
update := func(val interface{}) {
if m[x[0]] != nil {
if arr, ok := m[x[0]].([]interface{}); ok {
m[x[0]] = append(arr, val)
} else {
tmp := m[x[0]]
m[x[0]] = []interface{}{tmp, val}
}
} else {
m[x[0]] = val
}
}
if len(x) > 1 {
if v, err := strconv.Unquote(x[1]); err == nil {
update(v)
} else {
update(x[1])
}
} else {
update(true)
}
}
return MakeMap(m)
}
// ParseEnv retuns the environment as a params
func ParseEnv() Map {
env := strings.Join(os.Environ(), " ")
return ParseString(env)
}