Skip to content

Commit c0a0cde

Browse files
committed
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 <[email protected]>
1 parent afbdedb commit c0a0cde

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

repo/common/common.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,20 @@ import (
77

88
func MapGetKV(v map[string]interface{}, key string) (interface{}, error) {
99
var ok bool
10+
var mcursor map[string]interface{}
1011
var cursor interface{} = v
12+
1113
parts := strings.Split(key, ".")
1214
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]
1423
if !ok {
15-
sofar := strings.Join(parts[:i], ".")
1624
return nil, fmt.Errorf("%s key has no attributes", sofar)
1725
}
1826
}
@@ -39,7 +47,7 @@ func MapSetKV(v map[string]interface{}, key string, value interface{}) error {
3947
}
4048

4149
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
4351
mcursor[part] = map[string]interface{}{}
4452
cursor = mcursor[part]
4553
}

0 commit comments

Comments
 (0)