Skip to content

Commit

Permalink
fix:(ast) SortKeys(true) panic when not loaded-all (#603)
Browse files Browse the repository at this point in the history
  • Loading branch information
AsterDY committed Feb 28, 2024
1 parent 618834f commit 3739ffe
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
2 changes: 1 addition & 1 deletion ast/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ func (self *linkedPairs) Swap(i, j int) {
}

func (self *linkedPairs) Sort() {
sort.Sort(self)
sort.Stable(self)
}

// Compare two strings from the pos d.
Expand Down
8 changes: 7 additions & 1 deletion ast/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ func (self *Node) SortKeys(recurse bool) error {
}
if self.itype() == types.V_OBJECT {
return self.sortKeys(recurse)
} else {
} else if self.itype() == types.V_ARRAY {
var err error
err2 := self.ForEach(func(path Sequence, node *Node) bool {
it := node.itype()
Expand All @@ -987,10 +987,16 @@ func (self *Node) SortKeys(recurse bool) error {
return err
}
return err2
} else {
return nil
}
}

func (self *Node) sortKeys(recurse bool) (err error) {
// check raw node first
if err := self.checkRaw(); err != nil {
return err
}
ps, err := self.unsafeMap()
if err != nil {
return err
Expand Down
28 changes: 25 additions & 3 deletions ast/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ func BenchmarkNodeSortKeys(b *testing.B) {
if err != nil {
b.Fatal(err)
}
if err := root.LoadAll(); err != nil {
b.Fatal(err)
}
// if err := root.LoadAll(); err != nil {
// b.Fatal(err)
// }

b.Run("single", func(b *testing.B) {
r := root.Get("statuses")
Expand All @@ -110,6 +110,28 @@ func BenchmarkNodeSortKeys(b *testing.B) {
})
}

func TestNodeSortKeys2(t *testing.T) {
root, err := NewSearcher(_TwitterJson).GetByPath()
if err != nil {
t.Fatal(err)
}
// if err := root.LoadAll(); err != nil {
// b.Fatal(err)
// }
t.Run("single", func(t *testing.T) {
r := root.Get("statuses")
if r.Check() != nil {
t.Fatal(r.Error())
}
require.NoError(t, root.SortKeys(false))
})
t.Run("recurse", func(t *testing.T) {
require.NoError(t, root.SortKeys(true))
})
}



//go:noinline
func stackObj() interface{} {
var a int = 1
Expand Down

0 comments on commit 3739ffe

Please sign in to comment.