Debian Codegraph @

util/sprig/dict.go

raw
1 package sprig
2
3 // get retrieves a value from a map by its key.
4 // If the key exists, returns the corresponding value.
5 // If the key doesn't exist, returns an empty string.
6 //
7 // Parameters:
8 // - d: The map to retrieve the value from
9 // - key: The key to look up
10 //
11 // Returns:
12 // - any: The value associated with the key, or an empty string if not found
13 func get(d map[string]any, key string) any {
14 if val, ok := d[key]; ok {
15 return val
16 }
17 return ""
18 }
19
20 // set adds or updates a key-value pair in a map.
21 // Modifies the map in place and returns the modified map.
22 //
23 // Parameters:
24 // - d: The map to modify
25 // - key: The key to set
26 // - value: The value to associate with the key
27 //
28 // Returns:
29 // - map[string]any: The modified map (same instance as the input map)
30 func set(d map[string]any, key string, value any) map[string]any {
31 d[key] = value
32 return d
33 }
34
35 // unset removes a key-value pair from a map.
36 // If the key doesn't exist, the map remains unchanged.
37 // Modifies the map in place and returns the modified map.
38 //
39 // Parameters:
40 // - d: The map to modify
41 // - key: The key to remove
42 //
43 // Returns:
44 // - map[string]any: The modified map (same instance as the input map)
45 func unset(d map[string]any, key string) map[string]any {
46 delete(d, key)
47 return d
48 }
49
50 // hasKey checks if a key exists in a map.
51 //
52 // Parameters:
53 // - d: The map to check
54 // - key: The key to look for
55 //
56 // Returns:
57 // - bool: True if the key exists in the map, false otherwise
58 func hasKey(d map[string]any, key string) bool {
59 _, ok := d[key]
60 return ok
61 }
62
63 // pluck extracts values for a specific key from multiple maps.
64 // Only includes values from maps where the key exists.
65 //
66 // Parameters:
67 // - key: The key to extract values for
68 // - d: A variadic list of maps to extract values from
69 //
70 // Returns:
71 // - []any: A slice containing all values associated with the key across all maps
72 func pluck(key string, d ...map[string]any) []any {
73 var res []any
74 for _, dict := range d {
75 if val, ok := dict[key]; ok {
76 res = append(res, val)
77 }
78 }
79 return res
80 }
81
82 // keys collects all keys from one or more maps.
83 // The returned slice may contain duplicate keys if multiple maps contain the same key.
84 //
85 // Parameters:
86 // - dicts: A variadic list of maps to collect keys from
87 //
88 // Returns:
89 // - []string: A slice containing all keys from all provided maps
90 func keys(dicts ...map[string]any) []string {
91 var k []string
92 for _, dict := range dicts {
93 for key := range dict {
94 k = append(k, key)
95 }
96 }
97 return k
98 }
99
100 // pick creates a new map containing only the specified keys from the original map.
101 // If a key doesn't exist in the original map, it won't be included in the result.
102 //
103 // Parameters:
104 // - dict: The source map
105 // - keys: A variadic list of keys to include in the result
106 //
107 // Returns:
108 // - map[string]any: A new map containing only the specified keys and their values
109 func pick(dict map[string]any, keys ...string) map[string]any {
110 res := map[string]any{}
111 for _, k := range keys {
112 if v, ok := dict[k]; ok {
113 res[k] = v
114 }
115 }
116 return res
117 }
118
119 // omit creates a new map excluding the specified keys from the original map.
120 // The original map remains unchanged.
121 //
122 // Parameters:
123 // - dict: The source map
124 // - keys: A variadic list of keys to exclude from the result
125 //
126 // Returns:
127 // - map[string]any: A new map containing all key-value pairs except those specified
128 func omit(dict map[string]any, keys ...string) map[string]any {
129 res := map[string]any{}
130 omit := make(map[string]bool, len(keys))
131 for _, k := range keys {
132 omit[k] = true
133 }
134 for k, v := range dict {
135 if _, ok := omit[k]; !ok {
136 res[k] = v
137 }
138 }
139 return res
140 }
141
142 // dict creates a new map from a list of key-value pairs.
143 // The arguments are treated as key-value pairs, where even-indexed arguments are keys
144 // and odd-indexed arguments are values.
145 // If there's an odd number of arguments, the last key will be assigned an empty string value.
146 //
147 // Parameters:
148 // - v: A variadic list of alternating keys and values
149 //
150 // Returns:
151 // - map[string]any: A new map containing the specified key-value pairs
152 func dict(v ...any) map[string]any {
153 dict := map[string]any{}
154 lenv := len(v)
155 for i := 0; i < lenv; i += 2 {
156 key := strval(v[i])
157 if i+1 >= lenv {
158 dict[key] = ""
159 continue
160 }
161 dict[key] = v[i+1]
162 }
163 return dict
164 }
165
166 // values collects all values from a map into a slice.
167 // The order of values in the resulting slice is not guaranteed.
168 //
169 // Parameters:
170 // - dict: The map to collect values from
171 //
172 // Returns:
173 // - []any: A slice containing all values from the map
174 func values(dict map[string]any) []any {
175 var values []any
176 for _, value := range dict {
177 values = append(values, value)
178 }
179 return values
180 }
181
182 // dig safely accesses nested values in maps using a sequence of keys.
183 // If any key in the path doesn't exist, it returns the default value.
184 // The function expects at least 3 arguments: one or more keys, a default value, and a map.
185 //
186 // Parameters:
187 // - ps: A variadic list where:
188 // - The first N-2 arguments are string keys forming the path
189 // - The second-to-last argument is the default value to return if the path doesn't exist
190 // - The last argument is the map to traverse
191 //
192 // Returns:
193 // - any: The value found at the specified path, or the default value if not found
194 // - error: Any error that occurred during traversal
195 //
196 // Panics:
197 // - If fewer than 3 arguments are provided
198 func dig(ps ...any) (any, error) {
199 if len(ps) < 3 {
200 panic("dig needs at least three arguments")
201 }
202 dict := ps[len(ps)-1].(map[string]any)
203 def := ps[len(ps)-2]
204 ks := make([]string, len(ps)-2)
205 for i := 0; i < len(ks); i++ {
206 ks[i] = ps[i].(string)
207 }
208
209 return digFromDict(dict, def, ks)
210 }
211
212 // digFromDict is a helper function for dig that recursively traverses a map using a sequence of keys.
213 // If any key in the path doesn't exist, it returns the default value.
214 //
215 // Parameters:
216 // - dict: The map to traverse
217 // - d: The default value to return if the path doesn't exist
218 // - ks: A slice of string keys forming the path to traverse
219 //
220 // Returns:
221 // - any: The value found at the specified path, or the default value if not found
222 // - error: Any error that occurred during traversal
223 func digFromDict(dict map[string]any, d any, ks []string) (any, error) {
224 k, ns := ks[0], ks[1:]
225 step, has := dict[k]
226 if !has {
227 return d, nil
228 }
229 if len(ns) == 0 {
230 return step, nil
231 }
232 return digFromDict(step.(map[string]any), d, ns)
233 }