-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.go
166 lines (134 loc) · 3.72 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
package maputil
import (
"fmt"
"reflect"
)
// Map represents an instance of keys mapping.
//
// All the function returning values associated with keys will return a fallback value if the key is not present in the
// map or if the value can't be converted to the requested type.
type Map map[string]interface{}
// Clone returns a clone of the keys mapping instance.
func (m Map) Clone() Map {
clone := Map{}
for k, v := range m {
clone[k] = v
}
return clone
}
// Set sets the mapping key k to the value v.
func (m Map) Set(k string, v interface{}) {
m[k] = v
}
// Has returns true if key is present in the mapping, false otherwise.
func (m Map) Has(key string) bool {
_, ok := m[key]
return ok
}
// GetBool returns the boolean value associated with a key.
func (m Map) GetBool(key string, fallback bool) (bool, error) {
val, err := m.getKey(reflect.Bool, key, fallback)
if err != nil {
return fallback, err
}
return val.(bool), err
}
// GetFloat returns the floating-point number value associated with a key.
func (m Map) GetFloat(key string, fallback float64) (float64, error) {
val, err := m.getKey(reflect.Float64, key, fallback)
if err != nil {
return fallback, err
}
return val.(float64), err
}
// GetInt64 returns the 64-bit integer value associated with a key.
func (m Map) GetInt64(key string, fallback int64) (int64, error) {
val, err := m.getKey(reflect.Int64, key, fallback)
if err != nil {
return fallback, err
}
return val.(int64), err
}
// GetInt returns the integer value associated with a key.
func (m Map) GetInt(key string, fallback int) (int, error) {
val, err := m.getKey(reflect.Int, key, fallback)
if err != nil {
return fallback, err
}
return val.(int), err
}
// GetInterface returns the interface value associated with a key.
func (m Map) GetInterface(key string, fallback interface{}) (interface{}, error) {
val, ok := m[key]
if !ok {
val = fallback
}
return val, nil
}
// GetMap returns the map value associated with a key.
func (m Map) GetMap(key string, fallback Map) (Map, error) {
val, err := m.getKey(reflect.Map, key, fallback)
if err != nil {
return fallback, err
}
result := make(Map)
switch val.(type) {
case Map:
for k, v := range val.(Map) {
result[k] = v
}
default:
for k, v := range val.(map[string]interface{}) {
result[fmt.Sprintf("%v", k)] = v
}
}
return result, nil
}
// GetString returns the string value associated with a key.
func (m Map) GetString(key, fallback string) (string, error) {
val, err := m.getKey(reflect.String, key, fallback)
if err != nil {
return fallback, err
}
return val.(string), err
}
// GetStringSlice returns the string value associated with a key.
func (m Map) GetStringSlice(key string, fallback []string) ([]string, error) {
var out []string
val, err := m.getKey(reflect.Slice, key, fallback)
rv := reflect.ValueOf(val)
if !rv.IsValid() {
return nil, err
}
count := rv.Len()
for i := 0; i < count; i++ {
out = append(out, fmt.Sprintf("%v", rv.Index(i).Interface()))
}
return out, err
}
// Merge merges the source map entries into the receiver map. If replace parameter is true, existing entries in the
// receiver map will be replaced with entries from the source map.
func (m *Map) Merge(source Map, replace bool) {
if *m == nil {
*m = make(Map)
}
for sourceKey, sourceValue := range source {
if _, ok := map[string]interface{}(*m)[sourceKey]; !ok || replace {
map[string]interface{}(*m)[sourceKey] = sourceValue
}
}
}
func (m Map) getKey(k reflect.Kind, key string, fallback interface{}) (interface{}, error) {
if m == nil {
return fallback, nil
}
val, ok := m[key]
if !ok {
return fallback, nil
}
vk := reflect.ValueOf(val).Kind()
if vk != k {
return nil, ErrInvalidType
}
return val, nil
}