From c0a0cde9868d7f3f2b0f3b17cfda882a672c97df Mon Sep 17 00:00:00 2001 From: rht <rhtbot@gmail.com> Date: Thu, 13 Aug 2015 14:18:50 +0700 Subject: [PATCH] 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> --- repo/common/common.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/repo/common/common.go b/repo/common/common.go index d5fd7afac..dc1e7c503 100644 --- a/repo/common/common.go +++ b/repo/common/common.go @@ -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) } } @@ -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] } -- GitLab