From 204144bbd8d3fc4cf7326cc1d29c90bc95f4464e Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Wed, 7 Nov 2018 16:09:43 +0100 Subject: [PATCH] fix cnf/json set() method not to override already existing keys on same object level | update tests accordingly --- internal/cnf/json.go | 26 +++++++++++++++----------- internal/cnf/json_test.go | 32 +++++++++++++++++++++++--------- 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/internal/cnf/json.go b/internal/cnf/json.go index e9a3ae8..d617d3a 100644 --- a/internal/cnf/json.go +++ b/internal/cnf/json.go @@ -2,7 +2,6 @@ package cnf import ( "encoding/json" - "fmt" "io" "strings" ) @@ -77,6 +76,7 @@ func (d *Json) Set(_path, _value string) bool { // 1. browse path + create it if does not exist path := strings.Split(_path, ".") + lp := len(path) chain, found := d.browse(_path) // 2. if found -> set value @@ -85,7 +85,7 @@ func (d *Json) Set(_path, _value string) bool { if !ok { // impossible return false } - key := path[len(path)-1] + key := path[lp-1] mapWrapper[key] = _value return true } @@ -95,32 +95,36 @@ func (d *Json) Set(_path, _value string) bool { current := root // create children until second to last - fmt.Printf("%d / %d\n", len(chain)-1, len(path)) - for i, l := len(chain)-1, len(path)-1; i < l; i++ { + for i, l := len(chain)-1, lp-1; i < l; i++ { child := make(map[string]interface{}) - fmt.Printf("add '%s'\n", path[i]) current[path[i]] = child current = child } // set value - current[path[len(path)-1]] = _value + current[path[lp-1]] = _value // replace whole object if empty if len(chain) < 2 { - d.data = root + wrapper, ok := d.data.(map[string]interface{}) + if !ok { // impossible + return false + } + key := path[0] + wrapper[key] = root[key] // store with key ; eitherway it will erase all brother keys return true } // update last found object to add the value - wrapper, ok := chain[len(chain)-2].(map[string]interface{}) + wrapper, ok := chain[len(chain)-1].(map[string]interface{}) if !ok { // impossible return false } - key := path[len(chain)-2] - wrapper[key] = root - + // add each subkey + for subkey, subvalue := range root { + wrapper[subkey] = subvalue + } return true } diff --git a/internal/cnf/json_test.go b/internal/cnf/json_test.go index 2347f9f..3204385 100644 --- a/internal/cnf/json_test.go +++ b/internal/cnf/json_test.go @@ -130,17 +130,18 @@ func TestSetPathExistsAndIsString(t *testing.T) { func TestSetCreatePath(t *testing.T) { tests := []struct { - raw string - key string - value string + raw string + key string + ignore string // path to field that must be present after transformation + value string }{ - {`{ }`, "key", "newvalue"}, - {`{ }`, "parent.child", "newvalue"}, - {`{ }`, "parent.child.subchild", "newvalue"}, + {`{ "ignore": "xxx" }`, "key", "ignore", "newvalue"}, + {`{ "ignore": "xxx" }`, "parent.child", "ignore", "newvalue"}, + {`{ "ignore": "xxx" }`, "parent.child.subchild", "ignore", "newvalue"}, - {`{ "ignore": "xxx" }`, "key", "newvalue"}, - {`{ "parent": { } }`, "parent.child", "newvalue"}, - {`{ "ignore": "xxx", "parent": { } }`, "parent.child", "newvalue"}, + {`{ "ignore": "xxx" }`, "key", "ignore", "newvalue"}, + {`{ "parent": { "ignore": "xxx" } }`, "parent.child", "parent.ignore", "newvalue"}, + {`{ "ignore": "xxx", "parent": { } }`, "parent.child", "ignore", "newvalue"}, } for i, test := range tests { @@ -171,8 +172,21 @@ func TestSetCreatePath(t *testing.T) { // check value if value != test.value { t.Errorf("[%d] expected '%s' got '%s'", i, test.value, value) + continue } + // check that ignore field is still there + value, found = parser.Get(test.ignore) + if !found { + t.Errorf("[%d] expected ignore field, got none", i) + continue + } + + // check value + if value != "xxx" { + t.Errorf("[%d] expected ignore value to be '%s' got '%s'", i, "xxx", value) + continue + } } }