Skip to content

Commit

Permalink
fixed: create path if not exists. (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
gdlxSong authored Mar 23, 2022
1 parent f28a65e commit c69d23d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
5 changes: 5 additions & 0 deletions collectjs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tdtl

import (
"bytes"
"errors"
"fmt"
"strings"
Expand Down Expand Up @@ -117,6 +118,10 @@ func (cc *Collect) Set(path string, value Node) {

func (cc *Collect) Append(path string, value Node) {
cc.value, cc.err = add(cc.value, path, value.Raw())
if errors.Is(cc.err, jsonparser.KeyPathNotFoundError) {
setValue := bytes.Join([][]byte{[]byte("["), value.Raw(), []byte("]")}, []byte{})
cc.value, cc.err = set(cc.value, path, setValue)
}
}

func (cc *Collect) Del(path ...string) {
Expand Down
12 changes: 11 additions & 1 deletion collectjs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
)

var raw = Byte(`{"cpu":1,"mem": ["lo0", "eth1", "eth2"],"a":[{"v":0},{"v":1},{"v":2}],"b":[{"v":{"cv":1}},{"v":{"cv":2}},{"v":{"cv":3}}],"where": 10,"metadata": {"name": "Light1", "price": 11.05}}`)
Expand All @@ -29,7 +31,7 @@ func TestCollect_New(t *testing.T) {
got := New("{}")
got.Set(tt.path, tt.raw)
t.Run(tt.name, func(t *testing.T) {
if !reflect.DeepEqual(string(got.Raw()), tt.want) {
if !reflect.DeepEqual(string(got.Raw()), tt.want) {
t.Errorf("Get() = %v, want %v", string(got.Raw()), tt.want)
}
})
Expand Down Expand Up @@ -101,6 +103,7 @@ func TestCollect_Set2(t *testing.T) {
want interface{}
}{
{"1", "metadata._name", New(`"abc"`), `{"metadata":{"_name":"abc"}}`},
{"2", "metadata._name", New(`[20]`), `{"metadata":{"_name":[20]}}`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -128,6 +131,7 @@ func TestCollect_Append(t *testing.T) {
{"4", raw, "a[0]", New(`0`), `Unknown value type`, raw},
{"5", raw, "a[0].v", New(`{"v":0}`), `Unknown value type`, raw},
{"5", rawEmptyArray, "", New(`{"v":0}`), nil, `[{"v":0}]`},
{"6", []byte(`{}`), "metrics.cpus", New(`20`), nil, `{"metrics":{"cpus":[20]}}`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -149,6 +153,12 @@ func TestCollect_Append(t *testing.T) {
}
}

func TestCollect_Append2(t *testing.T) {
cc := New([]byte(`{}`))
cc.Append("metrics.cpus", New(`20`))
assert.Equal(t, `{"metrics":{"cpus":[20]}}`, cc.String())
}

func TestCollect_Del(t *testing.T) {
tests := []struct {
name string
Expand Down
3 changes: 1 addition & 2 deletions pkg/json/jsonparser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -805,8 +805,7 @@ func Append(data []byte, addValue []byte, keys ...string) (value []byte, err err
var setValue []byte
value, _, _, err = Get(data, keys...)
if err != nil {
setValue = bytes.Join([][]byte{[]byte("["), addValue, []byte("]")}, []byte{})
return Set(data, setValue, keys...)
return data, err
} else {
size := len(value)
if value[0] == []byte("[")[0] && value[size-1] == []byte("]")[0] {
Expand Down

0 comments on commit c69d23d

Please sign in to comment.