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

Revert "Break Change: drop ast.Node API UnsafeArrary|Map()" #555

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,3 @@ jobs:
- name: script
run: |
sh ./scripts/go_latest.sh master

22 changes: 0 additions & 22 deletions ast/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,28 +204,6 @@ func (self *linkedPairs) Get(key string) (*Pair, int) {
return nil, -1
}

func (self *linkedPairs) ToSlice(con []Pair) {
if len(con) < self.size {
return
}
i := self.size-1
a, b := i/_DEFAULT_NODE_CAP-1, i%_DEFAULT_NODE_CAP

if a < 0 {
copy(con, self.head[:b+1])
return
} else {
copy(con, self.head[:])
con = con[_DEFAULT_NODE_CAP:]
}

for i:=0; i<a; i++ {
copy(con, self.tail[i][:])
con = con[_DEFAULT_NODE_CAP:]
}
copy(con, self.tail[a][:b+1])
}

func (self *linkedPairs) ToMap(con map[string]Node) {
for i:=0; i<self.size; i++ {
n := self.At(i)
Expand Down
60 changes: 39 additions & 21 deletions ast/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -834,15 +834,15 @@ func (self *Node) MapUseNode() (map[string]Node, error) {
// WARN: don't use it unless you know what you are doing
//
// Deprecated: this API now returns copied nodes instead of directly reference,
// func (self *Node) UnsafeMap() ([]Pair, error) {
// if err := self.should(types.V_OBJECT, "an object"); err != nil {
// return nil, err
// }
// if err := self.skipAllKey(); err != nil {
// return nil, err
// }
// return self.toGenericObjectUsePair()
// }
func (self *Node) UnsafeMap() ([]Pair, error) {
if err := self.should(types.V_OBJECT, "an object"); err != nil {
return nil, err
}
if err := self.skipAllKey(); err != nil {
return nil, err
}
return self.toGenericObjectUsePair()
}

//go:nocheckptr
func (self *Node) unsafeMap() (*linkedPairs, error) {
Expand Down Expand Up @@ -974,15 +974,15 @@ func (self *Node) ArrayUseNode() ([]Node, error) {
//
// Deprecated: this API now returns copied nodes instead of directly reference,
// which has no difference with ArrayUseNode
// func (self *Node) UnsafeArray() ([]Node, error) {
// if err := self.should(types.V_ARRAY, "an array"); err != nil {
// return nil, err
// }
// if err := self.skipAllIndex(); err != nil {
// return nil, err
// }
// return self.toGenericArrayUseNode()
// }
func (self *Node) UnsafeArray() ([]Node, error) {
if err := self.should(types.V_ARRAY, "an array"); err != nil {
return nil, err
}
if err := self.skipAllIndex(); err != nil {
return nil, err
}
return self.toGenericArrayUseNode()
}

func (self *Node) unsafeArray() (*linkedNodes, error) {
if err := self.skipAllIndex(); err != nil {
Expand Down Expand Up @@ -1396,9 +1396,11 @@ func (self *Node) toGenericArrayUseNode() ([]Node, error) {
return []Node{}, nil
}

var s = (*linkedNodes)(self.p)
var out = make([]Node, nb)
s.ToSlice(out)
var out = make([]Node, 0, nb)
it := self.values()
for v := it.next(); v != nil; v = it.next() {
out = append(out, *v)
}

return out, nil
}
Expand Down Expand Up @@ -1460,6 +1462,22 @@ func (self *Node) toGenericObjectUseNode() (map[string]Node, error) {
return out, nil
}

func (self *Node) toGenericObjectUsePair() ([]Pair, error) {
var nb = self.len()
if nb == 0 {
return []Pair{}, nil
}

var out = make([]Pair, 0, nb)
it := self.properties()
for v := it.next(); v != nil; v = it.next() {
out = append(out, *v)
}

/* all done */
return out, nil
}

/**------------------------------------ Factory Methods ------------------------------------**/

var (
Expand Down
74 changes: 37 additions & 37 deletions ast/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -886,44 +886,44 @@ func TestUnset(t *testing.T) {

}

// func TestUnsafeNode(t *testing.T) {
// str, loop := getTestIteratorSample(_DEFAULT_NODE_CAP)

// root, err := NewSearcher(str).GetByPath("array")
// if err != nil {
// t.Fatal(err)
// }
// a, _ := root.UnsafeArray()
// if len(a) != loop {
// t.Fatalf("exp:%v, got:%v", loop, len(a))
// }
// for i := int64(0); i<int64(loop); i++{
// in := a[i]
// x, _ := in.Int64()
// if x != i {
// t.Fatalf("exp:%v, got:%v", i, x)
// }
// }
func TestUnsafeNode(t *testing.T) {
str, loop := getTestIteratorSample(_DEFAULT_NODE_CAP)

root, err := NewSearcher(str).GetByPath("array")
if err != nil {
t.Fatal(err)
}
a, _ := root.UnsafeArray()
if len(a) != loop {
t.Fatalf("exp:%v, got:%v", loop, len(a))
}
for i := int64(0); i<int64(loop); i++{
in := a[i]
x, _ := in.Int64()
if x != i {
t.Fatalf("exp:%v, got:%v", i, x)
}
}

// root, err = NewSearcher(str).GetByPath("object")
// if err != nil {
// t.Fatal(err)
// }
// b, _ := root.UnsafeMap()
// if len(b) != loop {
// t.Fatalf("exp:%v, got:%v", loop, len(b))
// }
// for i := int64(0); i<int64(loop); i++ {
// k := `k`+strconv.Itoa(int(i))
// if k != b[i].Key {
// t.Fatalf("unexpected element: %#v", b[i])
// }
// x, _ := b[i].Value.Int64()
// if x != i {
// t.Fatalf("exp:%v, got:%v", i, x)
// }
// }
// }
root, err = NewSearcher(str).GetByPath("object")
if err != nil {
t.Fatal(err)
}
b, _ := root.UnsafeMap()
if len(b) != loop {
t.Fatalf("exp:%v, got:%v", loop, len(b))
}
for i := int64(0); i<int64(loop); i++ {
k := `k`+strconv.Itoa(int(i))
if k != b[i].Key {
t.Fatalf("unexpected element: %#v", b[i])
}
x, _ := b[i].Value.Int64()
if x != i {
t.Fatalf("exp:%v, got:%v", i, x)
}
}
}

func TestUseNode(t *testing.T) {
str, loop := getTestIteratorSample(_DEFAULT_NODE_CAP)
Expand Down
27 changes: 11 additions & 16 deletions ast/visitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,7 @@ func (self *visitorNodeDiffTest) OnObjectEnd() error {
require.NotNil(self.t, object)

node := self.stk[self.sp-1].Node
ps, err := node.unsafeMap()
var pairs = make([]Pair, ps.Len())
ps.ToSlice(pairs)
pairs, err := node.UnsafeMap()
require.NoError(self.t, err)

keysGot := make([]string, 0, len(object))
Expand Down Expand Up @@ -269,10 +267,8 @@ func (self *visitorNodeDiffTest) OnArrayEnd() error {
require.NotNil(self.t, array)

node := self.stk[self.sp-1].Node
vs, err := node.unsafeArray()
values, err := node.UnsafeArray()
require.NoError(self.t, err)
var values = make([]Node, vs.Len())
vs.ToSlice(values)

require.EqualValues(self.t, len(values), len(array))

Expand Down Expand Up @@ -462,14 +458,13 @@ func (self *visitorUserNodeASTDecoder) decodeValue(root *Node) (visitorUserNode,
value, ierr, ferr)

case V_ARRAY:
nodes, err := root.unsafeArray()
nodes, err := root.UnsafeArray()
if err != nil {
return nil, err
}
values := make([]visitorUserNode, nodes.Len())
for i := 0; i<nodes.Len(); i++ {
n := nodes.At(i)
value, err := self.decodeValue(n)
values := make([]visitorUserNode, len(nodes))
for i := range nodes {
value, err := self.decodeValue(&nodes[i])
if err != nil {
return nil, err
}
Expand All @@ -478,17 +473,17 @@ func (self *visitorUserNodeASTDecoder) decodeValue(root *Node) (visitorUserNode,
return &visitorUserArray{Value: values}, nil

case V_OBJECT:
pairs, err := root.unsafeMap()
pairs, err := root.UnsafeMap()
if err != nil {
return nil, err
}
values := make(map[string]visitorUserNode, pairs.Len())
for i := 0; i < pairs.Len(); i++ {
value, err := self.decodeValue(&pairs.At(i).Value)
values := make(map[string]visitorUserNode, len(pairs))
for i := range pairs {
value, err := self.decodeValue(&pairs[i].Value)
if err != nil {
return nil, err
}
values[pairs.At(i).Key] = value
values[pairs[i].Key] = value
}
return &visitorUserObject{Value: values}, nil

Expand Down
1 change: 1 addition & 0 deletions tools/simde
Submodule simde added at 4d55fc
Loading