2018-11-07 13:03:54 +00:00
|
|
|
package cnf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2018-11-10 16:33:27 +00:00
|
|
|
func TestJsonGet(t *testing.T) {
|
2018-11-07 13:03:54 +00:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
raw string
|
|
|
|
key string
|
|
|
|
}{
|
|
|
|
{`{ "key": "value" }`, "key"},
|
|
|
|
{`{ "ignore": "xxx", "key": "value" }`, "key"},
|
|
|
|
{`{ "parent": { "child": "value" } }`, "parent.child"},
|
|
|
|
{`{ "ignore": "xxx", "parent": { "child": "value" } }`, "parent.child"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
|
2018-11-11 14:34:00 +00:00
|
|
|
parser := new(json)
|
2018-11-07 13:03:54 +00:00
|
|
|
reader := bytes.NewBufferString(test.raw)
|
|
|
|
|
|
|
|
// try to extract value
|
2018-11-11 00:05:14 +00:00
|
|
|
_, err := parser.ReadFrom(reader)
|
2018-11-07 13:03:54 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("parse error: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// extract value
|
|
|
|
value, found := parser.Get(test.key)
|
|
|
|
if !found {
|
|
|
|
t.Errorf("expected a result, got none")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// check value
|
|
|
|
if value != "value" {
|
|
|
|
t.Errorf("expected 'value' got '%s'", value)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-10 16:33:27 +00:00
|
|
|
func TestJsonGetNotString(t *testing.T) {
|
2018-11-07 13:03:54 +00:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
raw string
|
|
|
|
key string
|
|
|
|
}{
|
|
|
|
{`{ "key": ["value"] }`, "key"},
|
|
|
|
{`{ "key": {"subkey": "value"} }`, "key"},
|
|
|
|
{`{ "parent": { "child": [ "value" ] } }`, "parent.child"},
|
|
|
|
{`{ "parent": { "child": { "subkey": "value" } } }`, "parent.child"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
|
2018-11-11 14:34:00 +00:00
|
|
|
parser := new(json)
|
2018-11-07 13:03:54 +00:00
|
|
|
reader := bytes.NewBufferString(test.raw)
|
|
|
|
|
|
|
|
// try to extract value
|
2018-11-11 00:05:14 +00:00
|
|
|
_, err := parser.ReadFrom(reader)
|
2018-11-07 13:03:54 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("parse error: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// extract value
|
|
|
|
value, found := parser.Get(test.key)
|
|
|
|
if found || len(value) > 0 {
|
|
|
|
t.Errorf("expected no result, got '%s'", value)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-11-07 14:51:54 +00:00
|
|
|
|
2018-11-10 16:33:27 +00:00
|
|
|
func TestJsonSetPathExistsAndIsString(t *testing.T) {
|
2018-11-07 14:51:54 +00:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
raw string
|
|
|
|
key string
|
|
|
|
value string
|
|
|
|
}{
|
|
|
|
{`{ "key": "value" }`, "key", "newvalue"},
|
|
|
|
{`{ "ignore": "xxx", "key": "value" }`, "key", "newvalue"},
|
|
|
|
{`{ "parent": { "child": "value" } }`, "parent.child", "newvalue"},
|
|
|
|
{`{ "ignore": "xxx", "parent": { "child": "value" } }`, "parent.child", "newvalue"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
|
2018-11-11 14:34:00 +00:00
|
|
|
parser := new(json)
|
2018-11-07 14:51:54 +00:00
|
|
|
reader := bytes.NewBufferString(test.raw)
|
|
|
|
|
|
|
|
// try to extract value
|
2018-11-11 00:05:14 +00:00
|
|
|
_, err := parser.ReadFrom(reader)
|
2018-11-07 14:51:54 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("parse error: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// update value
|
|
|
|
if !parser.Set(test.key, test.value) {
|
|
|
|
t.Errorf("cannot set '%s' to '%s'", test.key, test.value)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// check new value
|
|
|
|
value, found := parser.Get(test.key)
|
|
|
|
if !found {
|
|
|
|
t.Errorf("expected a result, got none")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// check value
|
|
|
|
if value != test.value {
|
|
|
|
t.Errorf("expected '%s' got '%s'", test.value, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-10 16:33:27 +00:00
|
|
|
func TestJsonSetCreatePath(t *testing.T) {
|
2018-11-07 14:51:54 +00:00
|
|
|
|
|
|
|
tests := []struct {
|
2018-11-07 15:09:43 +00:00
|
|
|
raw string
|
|
|
|
key string
|
|
|
|
ignore string // path to field that must be present after transformation
|
|
|
|
value string
|
2018-11-07 14:51:54 +00:00
|
|
|
}{
|
2018-11-07 15:09:43 +00:00
|
|
|
{`{ "ignore": "xxx" }`, "key", "ignore", "newvalue"},
|
|
|
|
{`{ "ignore": "xxx" }`, "parent.child", "ignore", "newvalue"},
|
|
|
|
{`{ "ignore": "xxx" }`, "parent.child.subchild", "ignore", "newvalue"},
|
2018-11-07 14:51:54 +00:00
|
|
|
|
2018-11-07 15:09:43 +00:00
|
|
|
{`{ "ignore": "xxx" }`, "key", "ignore", "newvalue"},
|
|
|
|
{`{ "parent": { "ignore": "xxx" } }`, "parent.child", "parent.ignore", "newvalue"},
|
|
|
|
{`{ "ignore": "xxx", "parent": { } }`, "parent.child", "ignore", "newvalue"},
|
2018-11-07 14:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
|
2018-11-11 14:34:00 +00:00
|
|
|
parser := new(json)
|
2018-11-07 14:51:54 +00:00
|
|
|
reader := bytes.NewBufferString(test.raw)
|
|
|
|
|
|
|
|
// try to extract value
|
2018-11-11 00:05:14 +00:00
|
|
|
_, err := parser.ReadFrom(reader)
|
2018-11-07 14:51:54 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] parse error: %s", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// update value
|
|
|
|
if !parser.Set(test.key, test.value) {
|
|
|
|
t.Errorf("[%d] cannot set '%s' to '%s'", i, test.key, test.value)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// check new value
|
|
|
|
value, found := parser.Get(test.key)
|
|
|
|
if !found {
|
|
|
|
t.Errorf("[%d] expected a result, got none", i)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// check value
|
|
|
|
if value != test.value {
|
|
|
|
t.Errorf("[%d] expected '%s' got '%s'", i, test.value, value)
|
2018-11-07 15:09:43 +00:00
|
|
|
continue
|
2018-11-07 14:51:54 +00:00
|
|
|
}
|
|
|
|
|
2018-11-07 15:09:43 +00:00
|
|
|
// 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
|
|
|
|
}
|
2018-11-07 14:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|