forked from helmfile/vals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
42 lines (37 loc) · 907 Bytes
/
config.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
package vals
import (
"fmt"
)
func setValue(m map[string]interface{}, v interface{}, path ...string) error {
var cur interface{}
cur = m
for i, k := range path {
if i == len(path)-1 {
switch typed := cur.(type) {
case map[string]interface{}:
typed[k] = v
case map[interface{}]interface{}:
typed[k] = v
default:
return fmt.Errorf("unexpected type: key=%v, value=%v, type=%T", path[:i+1], typed, typed)
}
return nil
} else {
switch typed := cur.(type) {
case map[string]interface{}:
if _, ok := typed[k]; !ok {
typed[k] = map[string]interface{}{}
}
cur = typed[k]
case map[interface{}]interface{}:
if _, ok := typed[k]; !ok {
typed[k] = map[string]interface{}{}
}
cur = typed[k]
default:
return fmt.Errorf("unexpected type: key=%v, value=%v, type=%T", path[:i+1], typed, typed)
}
}
}
panic("invalid state")
}