fix cnf/json set() method not to override already existing keys on same object level | update tests accordingly

This commit is contained in:
xdrm-brackets 2018-11-07 16:09:43 +01:00
parent fa647530ea
commit 204144bbd8
2 changed files with 38 additions and 20 deletions

View File

@ -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
}

View File

@ -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
}
}
}