Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:(ast) SortKeys(true) panic when not loaded-all #603

Merged
merged 2 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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