提交 da75e92d 作者: Juan Benet

Merge pull request #1570 from rht/config-null

Config: allow to set maps on null value
...@@ -7,12 +7,20 @@ import ( ...@@ -7,12 +7,20 @@ import (
func MapGetKV(v map[string]interface{}, key string) (interface{}, error) { func MapGetKV(v map[string]interface{}, key string) (interface{}, error) {
var ok bool var ok bool
var mcursor map[string]interface{}
var cursor interface{} = v var cursor interface{} = v
parts := strings.Split(key, ".") parts := strings.Split(key, ".")
for i, part := range parts { for i, part := range parts {
cursor, ok = cursor.(map[string]interface{})[part]
if !ok {
sofar := strings.Join(parts[:i], ".") 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 {
return nil, fmt.Errorf("%s key has no attributes", sofar) 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 { ...@@ -39,7 +47,7 @@ func MapSetKV(v map[string]interface{}, key string, value interface{}) error {
} }
cursor, ok = mcursor[part] 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{}{} mcursor[part] = map[string]interface{}{}
cursor = mcursor[part] cursor = mcursor[part]
} }
......
...@@ -57,6 +57,9 @@ test_config_cmd() { ...@@ -57,6 +57,9 @@ test_config_cmd() {
test_config_cmd_set "--json" "beep3" "true" test_config_cmd_set "--json" "beep3" "true"
test_config_cmd_set "--json" "beep3" "false" test_config_cmd_set "--json" "beep3" "false"
test_config_cmd_set "--json" "Discovery" "$CONFIG_SET_JSON_TEST" test_config_cmd_set "--json" "Discovery" "$CONFIG_SET_JSON_TEST"
test_config_cmd_set "--json" "deep-not-defined.prop" "true"
test_config_cmd_set "--json" "deep-null" "null"
test_config_cmd_set "--json" "deep-null.prop" "true"
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论