Config: allow to set maps on null value
Also, now, if ipfs config foo.bar has value of anything that is not map (0, "0", 0.1), then ipfs config foo.bar.baz now returns an error instead of a panic License: MIT Signed-off-by: rht <rhtbot@gmail.com>
rht committed
Aug 13, 2015 at 14:18 UTC
c0a0cde9868d7f3f2b0f3b17cfda882a672c97df
1 file changed
+11
-3
repo/common/common.go
+11
-3
@@ -7,12 +7,20 @@ import (
7
8
func MapGetKV(v map[string]interface{}, key string) (interface{}, error) {
9
var ok bool
10
+ var mcursor map[string]interface{}
11
var cursor interface{} = v
12
+
13
parts := strings.Split(key, ".")
14
for i, part := range parts {
13
- cursor, ok = cursor.(map[string]interface{})[part]
15
+ sofar := strings.Join(parts[:i], ".")
16
+
17
+ mcursor, ok = cursor.(map[string]interface{})
18
+ if !ok {
19
+ return nil, fmt.Errorf("%s key is not a map", sofar)
20
+ }
21
+
22
+ cursor, ok = mcursor[part]
23
if !ok {
15
- sofar := strings.Join(parts[:i], ".")
24
return nil, fmt.Errorf("%s key has no attributes", sofar)
25
}
26
}
@@ -39,7 +47,7 @@ func MapSetKV(v map[string]interface{}, key string, value interface{}) error {
47
}
48
49
cursor, ok = mcursor[part]
42
- if !ok { // create map if this is empty
50
+ if !ok || cursor == nil { // create map if this is empty or is null
51
mcursor[part] = map[string]interface{}{}
52
cursor = mcursor[part]
53
}