Skip to content

Commit

Permalink
Merge branch 'main' into fix/test
Browse files Browse the repository at this point in the history
  • Loading branch information
liuq19 authored Aug 22, 2023
2 parents 9b6a266 + 1a770c0 commit 1be6a9f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
37 changes: 29 additions & 8 deletions ast/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,9 @@ func (self *Node) Add(node Node) error {
*self = NewArray([]Node{node})
return nil
}

if err := self.should(types.V_ARRAY, "an array"); err != nil {
return err
}
s, err := self.unsafeArray()
if err != nil {
return err
Expand Down Expand Up @@ -843,9 +845,6 @@ func (self *Node) MapUseNode() (map[string]Node, error) {
// }

func (self *Node) unsafeMap() (*linkedPairs, error) {
if err := self.should(types.V_OBJECT, "an object"); err != nil {
return nil, err
}
if err := self.skipAllKey(); err != nil {
return nil, err
}
Expand All @@ -858,6 +857,31 @@ func (self *Node) unsafeMap() (*linkedPairs, error) {
// SortKeys sorts children of a V_OBJECT node in ascending key-order.
// If recurse is true, it recursively sorts children's children as long as a V_OBJECT node is found.
func (self *Node) SortKeys(recurse bool) error {
if err := self.Check(); err != nil {
return err
}
if self.itype() == types.V_OBJECT {
return self.sortKeys(recurse)
} else {
var err error
err2 := self.ForEach(func(path Sequence, node *Node) bool {
it := self.itype()
if it == types.V_ARRAY || it == types.V_OBJECT {
err = node.SortKeys(recurse)
if err != nil {
return false
}
}
return true
})
if err != nil {
return err
}
return err2
}
}

func (self *Node) sortKeys(recurse bool) (err error) {
ps, err := self.unsafeMap()
if err != nil {
return err
Expand All @@ -867,7 +891,7 @@ func (self *Node) SortKeys(recurse bool) error {
var sc Scanner
sc = func(path Sequence, node *Node) bool {
if node.itype() == types.V_OBJECT {
if err := node.SortKeys(recurse); err != nil {
if err := node.sortKeys(recurse); err != nil {
return false
}
}
Expand Down Expand Up @@ -959,9 +983,6 @@ func (self *Node) ArrayUseNode() ([]Node, error) {
// }

func (self *Node) unsafeArray() (*linkedNodes, error) {
if err := self.should(types.V_ARRAY, "an array"); err != nil {
return nil, err
}
if err := self.skipAllIndex(); err != nil {
return nil, err
}
Expand Down
23 changes: 23 additions & 0 deletions ast/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,29 @@ func TestNodeSortKeys(t *testing.T) {
}
assert.Equal(t, len(exp), len(act))
assert.Equal(t, string(exp), string(act))

src = `[[1], {"b":1,"a":2,"c":3}, [], {}, [{"b":1,"a":2,"c":3,"d":[],"e":{}}]]`
root, err = NewSearcher(src).GetByPath()
if err != nil {
t.Fatal(err)
}
vv, err := root.Interface()
if err != nil {
t.Fatal(err)
}
exp, err = json.Marshal(vv)
if err != nil {
t.Fatal(err)
}
if err := root.SortKeys(true); err != nil {
t.Fatal(err)
}
act, err = root.MarshalJSON()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, string(exp), string(act))

}

func BenchmarkNodeSortKeys(b *testing.B) {
Expand Down

0 comments on commit 1be6a9f

Please sign in to comment.