Skip to content

Commit

Permalink
fix: load length bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
AsterDY committed Dec 29, 2023
1 parent 0af3e8d commit d9b9e34
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 20 deletions.
4 changes: 2 additions & 2 deletions ast/iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func TestExist(t *testing.T) {
if path.Key != nil && *path.Key == "a" {
t.Fatal()
}
if path.Index == 1 {
if path.Index == 0 {
if *path.Key != "b" {
t.Fatal()
}
Expand All @@ -247,7 +247,7 @@ func TestExist(t *testing.T) {
})
}

if path.Index == 2 {
if path.Index == 1 {
if *path.Key != "c" {
t.Fatal()
}
Expand Down
17 changes: 13 additions & 4 deletions ast/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,13 +573,16 @@ func (self *Node) Unset(key string) (bool, error) {
if err := self.should(types.V_OBJECT, "an object"); err != nil {
return false, err
}
// NOTICE: must get acurate length before deduct
if err := self.skipAllKey(); err != nil {
return false, err
}
p, i := self.skipKey(key)
if !p.Exists() {
return false, nil
} else if err := p.Check(); err != nil {
return false, err
}

self.removePair(i)
return true, nil
}
Expand Down Expand Up @@ -619,23 +622,29 @@ func (self *Node) SetAnyByIndex(index int, val interface{}) (bool, error) {
// UnsetByIndex remove the node of given index
// WARN: After conducting `UnsetXX()`, the node's length WON'T change
func (self *Node) UnsetByIndex(index int) (bool, error) {
if err := self.Check(); err != nil {
if err := self.checkRaw(); err != nil {
return false, err
}

var p *Node
it := self.itype()

if it == types.V_ARRAY {
p = self.Index(index)
if err := self.skipAllIndex(); err != nil {
return false, err
}
p = self.skipIndex(index)

} else if it == types.V_OBJECT {
if err := self.checkRaw(); err != nil {
if err := self.skipAllKey(); err != nil {
return false, err
}
pr := self.skipIndexPair(index)
if pr == nil {
return false, ErrNotExist
}
p = &pr.Value

} else {
return false, ErrUnsupportType
}
Expand Down
36 changes: 22 additions & 14 deletions ast/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -780,15 +780,15 @@ func TestUnset(t *testing.T) {
if !entities.Exists() || entities.Check() != nil {
t.Fatal(entities.Check().Error())
}
exist, err := entities.Unset("urls") // NOTICE: Unset() won't change node.Len() here
exist, err := entities.Unset("urls")
if !exist || err != nil {
t.Fatal()
}
e := entities.Get("urls")
if e.Exists() {
t.Fatal()
}
if entities.len() != 3 {
if entities.len() != 2 {
t.Fatal(entities.len())
}

Expand Down Expand Up @@ -828,14 +828,8 @@ func TestUnset(t *testing.T) {
require.Equal(t,
`{"hashtags":[{"text":"freebandnames","indices":[20,34]}],"user_mentions":[],"urls":"a"}`, buf.String())

exist, err = entities.UnsetByIndex(entities.len()-1)
if !exist || err != nil {
t.Fatal()
}
e = entities.Get("urls")
if e.Exists() {
t.Fatal()
}
// reload entities
*entities = NewRaw(string(out))

hashtags := entities.Get("hashtags").Index(0)
hashtags.Set("text2", newRawNode(`{}`, types.V_OBJECT))
Expand All @@ -851,21 +845,35 @@ func TestUnset(t *testing.T) {
t.Fatal()
}

entities.Load()
exist, err = entities.UnsetByIndex(entities.len()-1)
if !exist || err != nil {
t.Fatal()
}
if entities.len() != 2 {
t.Fatal(entities.len())
}
e = entities.Index(entities.len())
if e.Exists() {
t.Fatal()
}


ums := entities.Get("user_mentions")
ums.Add(NewNull())
ums.Add(NewBool(true))
ums.Add(NewBool(false))
ums.Add(NewBool(true))
if ums.len() != 3 {
t.Fatal()
}
exist, err = ums.UnsetByIndex(2)
exist, err = ums.UnsetByIndex(1)
if !exist || err != nil {
t.Fatal()
}

require.Equal(t, 2, ums.len())
umses, err := ums.Interface()
require.NoError(t, err)
require.Equal(t, []interface{}{interface{}(nil), true},umses)
require.Equal(t, []interface{}{interface{}(nil), true}, umses)

v1, _ := ums.Index(0).Interface()
v2, _ := ums.Index(1).Interface() // NOTICE: unseted index 1 still can be find here
Expand Down

0 comments on commit d9b9e34

Please sign in to comment.