Skip to content

Commit

Permalink
Config: allow to set maps on null value
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
rht committed Aug 13, 2015
1 parent 183a524 commit 5c4effc
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions repo/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@ import (

func MapGetKV(v map[string]interface{}, key string) (interface{}, error) {
var ok bool
var mcursor map[string]interface{}
var cursor interface{} = v

parts := strings.Split(key, ".")
for i, part := range parts {
cursor, ok = cursor.(map[string]interface{})[part]
sofar := strings.Join(parts[:i], ".")

mcursor, ok = cursor.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("%s key is not a map", sofar)
}

cursor, ok = mcursor[part]
if !ok {
sofar := strings.Join(parts[:i], ".")
return nil, fmt.Errorf("%s key has no attributes", sofar)
}
}
Expand All @@ -39,7 +47,7 @@ func MapSetKV(v map[string]interface{}, key string, value interface{}) error {
}

cursor, ok = mcursor[part]
if !ok { // create map if this is empty
if !ok || cursor == nil { // create map if this is empty or is null
mcursor[part] = map[string]interface{}{}
cursor = mcursor[part]
}
Expand Down

0 comments on commit 5c4effc

Please sign in to comment.