fix cnf/json set() method not to override already existing keys on same object level | update tests accordingly
This commit is contained in:
parent
fa647530ea
commit
204144bbd8
|
@ -2,7 +2,6 @@ package cnf
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
@ -77,6 +76,7 @@ func (d *Json) Set(_path, _value string) bool {
|
||||||
|
|
||||||
// 1. browse path + create it if does not exist
|
// 1. browse path + create it if does not exist
|
||||||
path := strings.Split(_path, ".")
|
path := strings.Split(_path, ".")
|
||||||
|
lp := len(path)
|
||||||
chain, found := d.browse(_path)
|
chain, found := d.browse(_path)
|
||||||
|
|
||||||
// 2. if found -> set value
|
// 2. if found -> set value
|
||||||
|
@ -85,7 +85,7 @@ func (d *Json) Set(_path, _value string) bool {
|
||||||
if !ok { // impossible
|
if !ok { // impossible
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
key := path[len(path)-1]
|
key := path[lp-1]
|
||||||
mapWrapper[key] = _value
|
mapWrapper[key] = _value
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -95,32 +95,36 @@ func (d *Json) Set(_path, _value string) bool {
|
||||||
current := root
|
current := root
|
||||||
|
|
||||||
// create children until second to last
|
// create children until second to last
|
||||||
fmt.Printf("%d / %d\n", len(chain)-1, len(path))
|
for i, l := len(chain)-1, lp-1; i < l; i++ {
|
||||||
for i, l := len(chain)-1, len(path)-1; i < l; i++ {
|
|
||||||
child := make(map[string]interface{})
|
child := make(map[string]interface{})
|
||||||
fmt.Printf("add '%s'\n", path[i])
|
|
||||||
current[path[i]] = child
|
current[path[i]] = child
|
||||||
current = child
|
current = child
|
||||||
}
|
}
|
||||||
|
|
||||||
// set value
|
// set value
|
||||||
current[path[len(path)-1]] = _value
|
current[path[lp-1]] = _value
|
||||||
|
|
||||||
// replace whole object if empty
|
// replace whole object if empty
|
||||||
if len(chain) < 2 {
|
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
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// update last found object to add the value
|
// 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
|
if !ok { // impossible
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
key := path[len(chain)-2]
|
// add each subkey
|
||||||
wrapper[key] = root
|
for subkey, subvalue := range root {
|
||||||
|
wrapper[subkey] = subvalue
|
||||||
|
}
|
||||||
return true
|
return true
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -132,15 +132,16 @@ func TestSetCreatePath(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
raw string
|
raw string
|
||||||
key string
|
key string
|
||||||
|
ignore string // path to field that must be present after transformation
|
||||||
value string
|
value string
|
||||||
}{
|
}{
|
||||||
{`{ }`, "key", "newvalue"},
|
{`{ "ignore": "xxx" }`, "key", "ignore", "newvalue"},
|
||||||
{`{ }`, "parent.child", "newvalue"},
|
{`{ "ignore": "xxx" }`, "parent.child", "ignore", "newvalue"},
|
||||||
{`{ }`, "parent.child.subchild", "newvalue"},
|
{`{ "ignore": "xxx" }`, "parent.child.subchild", "ignore", "newvalue"},
|
||||||
|
|
||||||
{`{ "ignore": "xxx" }`, "key", "newvalue"},
|
{`{ "ignore": "xxx" }`, "key", "ignore", "newvalue"},
|
||||||
{`{ "parent": { } }`, "parent.child", "newvalue"},
|
{`{ "parent": { "ignore": "xxx" } }`, "parent.child", "parent.ignore", "newvalue"},
|
||||||
{`{ "ignore": "xxx", "parent": { } }`, "parent.child", "newvalue"},
|
{`{ "ignore": "xxx", "parent": { } }`, "parent.child", "ignore", "newvalue"},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, test := range tests {
|
for i, test := range tests {
|
||||||
|
@ -171,8 +172,21 @@ func TestSetCreatePath(t *testing.T) {
|
||||||
// check value
|
// check value
|
||||||
if value != test.value {
|
if value != test.value {
|
||||||
t.Errorf("[%d] expected '%s' got '%s'", i, test.value, 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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue