Skip to content

Commit da75e92

Browse files
committed
Merge pull request ipfs#1570 from rht/config-null
Config: allow to set maps on null value
2 parents c1380f1 + c0a0cde commit da75e92

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-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
}

test/sharness/t0021-config.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ test_config_cmd() {
5757
test_config_cmd_set "--json" "beep3" "true"
5858
test_config_cmd_set "--json" "beep3" "false"
5959
test_config_cmd_set "--json" "Discovery" "$CONFIG_SET_JSON_TEST"
60+
test_config_cmd_set "--json" "deep-not-defined.prop" "true"
61+
test_config_cmd_set "--json" "deep-null" "null"
62+
test_config_cmd_set "--json" "deep-null.prop" "true"
6063

6164
}
6265

0 commit comments

Comments
 (0)