-
Notifications
You must be signed in to change notification settings - Fork 1
/
ci_map.go
54 lines (41 loc) · 1.03 KB
/
ci_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
package alligotor
import (
"encoding/json"
"strings"
"gopkg.in/yaml.v3"
)
type ciMap struct {
m map[string]interface{}
}
func newCiMap() *ciMap {
return &ciMap{m: make(map[string]interface{})}
}
func (c ciMap) Get(base []string, name string) (b interface{}, ok bool) {
return c.get(append(base, name))
}
func (c ciMap) get(toIterate []string) (b interface{}, ok bool) {
// go through map keys and check if key.ToLower() matches, field.ToLower()
for key := range c.m {
if !strings.EqualFold(key, toIterate[0]) {
continue
}
val := c.m[key]
if len(toIterate) == 1 {
// no separator in the string -> reached end of search string
return val, true
}
// iterate further through nested fields
valAsMap, ok := val.(map[string]interface{})
if !ok {
return nil, false
}
return ciMap{m: valAsMap}.get(toIterate[1:])
}
return nil, false
}
func (c *ciMap) UnmarshalYAML(value *yaml.Node) error {
return value.Decode(&c.m)
}
func (c *ciMap) UnmarshalJSON(bytes []byte) error {
return json.Unmarshal(bytes, &c.m)
}