forked from Masterminds/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdict.go
More file actions
209 lines (185 loc) · 4.57 KB
/
dict.go
File metadata and controls
209 lines (185 loc) · 4.57 KB
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
package sprig
import (
"fmt"
"reflect"
"dario.cat/mergo"
"github.com/mitchellh/copystructure"
)
func get(d map[string]interface{}, key string) interface{} {
if val, ok := d[key]; ok {
return val
}
return ""
}
func set(d map[string]interface{}, key string, value interface{}) map[string]interface{} {
d[key] = value
return d
}
func unset(d map[string]interface{}, key string) map[string]interface{} {
delete(d, key)
return d
}
func hasKey(d interface{}, key string) bool {
v := reflect.ValueOf(d)
if v.Kind() == reflect.Map {
if v.Type().Key().Kind() != reflect.String {
return false
}
return v.MapIndex(reflect.ValueOf(key)).IsValid()
}
if m, ok := d.(map[string]interface{}); ok {
_, found := m[key]
return found
}
return false
}
func pluck(key string, d ...map[string]interface{}) []interface{} {
res := []interface{}{}
for _, dict := range d {
if val, ok := dict[key]; ok {
res = append(res, val)
}
}
return res
}
func keys(dicts ...interface{}) []string {
k := []string{}
for _, dictIface := range dicts {
v := reflect.ValueOf(dictIface)
if v.Kind() == reflect.Map {
if v.Type().Key().Kind() != reflect.String {
panic(fmt.Sprintf("keys: map key type must be string, got %s", v.Type().Key().Kind()))
}
for _, key := range v.MapKeys() {
k = append(k, key.String())
}
}
}
return k
}
func pick(dictIface interface{}, keys ...string) map[string]interface{} {
res := map[string]interface{}{}
v := reflect.ValueOf(dictIface)
if v.Kind() == reflect.Map {
if v.Type().Key().Kind() != reflect.String {
panic(fmt.Sprintf("pick: map key type must be string, got %s", v.Type().Key().Kind()))
}
for _, k := range keys {
val := v.MapIndex(reflect.ValueOf(k))
if val.IsValid() {
res[k] = val.Interface()
}
}
}
return res
}
func omit(dictIface interface{}, keys ...string) map[string]interface{} {
res := map[string]interface{}{}
omitSet := make(map[string]bool, len(keys))
for _, k := range keys {
omitSet[k] = true
}
v := reflect.ValueOf(dictIface)
if v.Kind() == reflect.Map {
if v.Type().Key().Kind() != reflect.String {
panic(fmt.Sprintf("omit: map key type must be string, got %s", v.Type().Key().Kind()))
}
for _, key := range v.MapKeys() {
k := key.String()
if !omitSet[k] {
res[k] = v.MapIndex(key).Interface()
}
}
}
return res
}
func dict(v ...interface{}) map[string]interface{} {
dict := map[string]interface{}{}
lenv := len(v)
for i := 0; i < lenv; i += 2 {
key := strval(v[i])
if i+1 >= lenv {
dict[key] = ""
continue
}
dict[key] = v[i+1]
}
return dict
}
func merge(dst map[string]interface{}, srcs ...map[string]interface{}) interface{} {
for _, src := range srcs {
if err := mergo.Merge(&dst, src); err != nil {
// Swallow errors inside of a template.
return ""
}
}
return dst
}
func mustMerge(dst map[string]interface{}, srcs ...map[string]interface{}) (interface{}, error) {
for _, src := range srcs {
if err := mergo.Merge(&dst, src); err != nil {
return nil, err
}
}
return dst, nil
}
func mergeOverwrite(dst map[string]interface{}, srcs ...map[string]interface{}) interface{} {
for _, src := range srcs {
if err := mergo.MergeWithOverwrite(&dst, src); err != nil {
// Swallow errors inside of a template.
return ""
}
}
return dst
}
func mustMergeOverwrite(dst map[string]interface{}, srcs ...map[string]interface{}) (interface{}, error) {
for _, src := range srcs {
if err := mergo.MergeWithOverwrite(&dst, src); err != nil {
return nil, err
}
}
return dst, nil
}
func values(dictIface interface{}) []interface{} {
vals := []interface{}{}
v := reflect.ValueOf(dictIface)
if v.Kind() == reflect.Map {
for _, key := range v.MapKeys() {
vals = append(vals, v.MapIndex(key).Interface())
}
}
return vals
}
func deepCopy(i interface{}) interface{} {
c, err := mustDeepCopy(i)
if err != nil {
panic("deepCopy error: " + err.Error())
}
return c
}
func mustDeepCopy(i interface{}) (interface{}, error) {
return copystructure.Copy(i)
}
func dig(ps ...interface{}) (interface{}, error) {
if len(ps) < 3 {
panic("dig needs at least three arguments")
}
dict := ps[len(ps)-1].(map[string]interface{})
def := ps[len(ps)-2]
ks := make([]string, len(ps)-2)
for i := 0; i < len(ks); i++ {
ks[i] = ps[i].(string)
}
return digFromDict(dict, def, ks)
}
func digFromDict(dict map[string]interface{}, d interface{}, ks []string) (interface{}, error) {
k, ns := ks[0], ks[1:len(ks)]
step, has := dict[k]
if !has {
return d, nil
}
if len(ns) == 0 {
return step, nil
}
return digFromDict(step.(map[string]interface{}), d, ns)
}