Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
AsterDY committed Jan 17, 2024
1 parent 71ade9f commit dc7ce40
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
7 changes: 3 additions & 4 deletions ast/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ func (self *Value) SetByPath(val Value, allowAppend bool, path ...interface{}) (
if self.Check() != nil {
return exist, self
}

if val.Check() != nil {
return exist, val
}
Expand All @@ -244,6 +243,9 @@ func (self *Value) SetByPath(val Value, allowAppend bool, path ...interface{}) (
for i, k := range path {
if id, ok := k.(int); ok && id >= 0 {
if _, err = p.searchIndex(id); err != 0 {
if !allowAppend {
return false, p.ExportError(err)
}
if err != _ERR_NOT_FOUND {
return exist, p.ExportError(err)
}
Expand All @@ -252,9 +254,6 @@ func (self *Value) SetByPath(val Value, allowAppend bool, path ...interface{}) (
}
} else if key, ok := k.(string); ok {
if _, err = p.searchKey(key); err != 0 {
if !allowAppend {
return false, p.ExportError(err)
}
if err != _ERR_NOT_FOUND {
return exist, p.ExportError(err)
}
Expand Down
33 changes: 16 additions & 17 deletions ast/raw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,36 +493,35 @@ func TestRawNode_SetByPath(t *testing.T) {
tests := []struct{
name string
js string
allowInsert bool
paths []interface{}
val Value
exist bool
err string
out string
}{
{"exist object",`{"a":1}`,[]interface{}{"a"},NewValueJSON(`2`),true,"",`{"a":2}`},
{"not-exist object",`{"b":1}`,[]interface{}{"a"},NewValueJSON(`2`),false,"",`{"b":1,"a":2}`},
{"empty object",`{}`,[]interface{}{"a"},NewValueJSON(`2`),false,"",`{"a":2}`},
{"empty object 2",`{}`,[]interface{}{"a",1},NewValueJSON(`2`),false,"",`{"a":[2]}`},
{"empty object 3",`{}`,[]interface{}{"a",1,"a"},NewValueJSON(`2`),false,"",`{"a":[{"a":2}]}`},
{"exist array",`[1]`,[]interface{}{0},NewValueJSON(`2`),true,"",`[2]`},
{"not exist array",`[1]`,[]interface{}{1},NewValueJSON(`2`),false,"",`[1,2]`},
{"empty array",`[]`,[]interface{}{1},NewValueJSON(`2`),false,"",`[2]`},
{"empty array 2",`[]`,[]interface{}{1,1},NewValueJSON(`2`),false,"",`[[2]]`},
{"empty array 3",`[]`,[]interface{}{1,"a",1},NewValueJSON(`2`),false,"",`[{"a":[2]}]`},
{"empty array 3",`[]`,[]interface{}{1,"a","a"},NewValueJSON(`2`),false,"",`[{"a":{"a":2}}]`},
{"exist object",`{"a":1}`,true,[]interface{}{"a"},NewValueJSON(`2`),true,"",`{"a":2}`},
{"not-exist object",`{"b":1}`,true,[]interface{}{"a"},NewValueJSON(`2`),false,"",`{"b":1,"a":2}`},
{"empty object",`{}`,true,[]interface{}{"a"},NewValueJSON(`2`),false,"",`{"a":2}`},
{"empty object 2",`{}`,true,[]interface{}{"a",1},NewValueJSON(`2`),false,"",`{"a":[2]}`},
{"empty object 3",`{}`,true,[]interface{}{"a",1,"a"},NewValueJSON(`2`),false,"",`{"a":[{"a":2}]}`},
{"exist array",`[1]`,true,[]interface{}{0},NewValueJSON(`2`),true,"",`[2]`},
{"not exist array",`[1]`,true,[]interface{}{1},NewValueJSON(`2`),false,"",`[1,2]`},
{"not exist array",`[1]`,false,[]interface{}{1},NewValueJSON(`2`),false,"value not exists",`[1]`},
{"empty array",`[]`,true,[]interface{}{1},NewValueJSON(`2`),false,"",`[2]`},
{"empty array",`[]`,false,[]interface{}{1},NewValueJSON(`2`),false,"value not exists",`[]`},
{"empty array 2",`[]`,true,[]interface{}{1,1},NewValueJSON(`2`),false,"",`[[2]]`},
{"empty array 3",`[]`,true,[]interface{}{1,"a",1},NewValueJSON(`2`),false,"",`[{"a":[2]}]`},
{"empty array 3",`[]`,true,[]interface{}{1,"a","a"},NewValueJSON(`2`),false,"",`[{"a":{"a":2}}]`},
}
for _, c := range tests {
println(c.name)
root := NewValueJSON(c.js)
exist, err := root.SetByPath(c.val, true, c.paths...)
exist, err := root.SetByPath(c.val, c.allowInsert, c.paths...)
if err != nil && err.Error() != c.err {
t.Fatal(err)
}
if out := root.raw(); err != nil {
t.Fatal()
} else {
require.Equal(t, c.out, out)
}
require.Equal(t, c.out, root.raw())
require.Equal(t, c.exist, exist)
}
}
Expand Down

0 comments on commit dc7ce40

Please sign in to comment.