-
Notifications
You must be signed in to change notification settings - Fork 1
/
walker.go
128 lines (115 loc) · 2.76 KB
/
walker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package structql
import (
"github.com/viant/xunsafe"
)
//Walker represents struct walker
type Walker struct {
root *Node
}
//Count counts leaf node
func (w *Walker) Count(value interface{}) int {
return w.count(w.root, value)
}
//Traverse walks the node
func (w *Walker) Traverse(aNode *Node, value interface{}, visitor interface{}) error {
nodeVisitor, _ := visitor.(NodeVisitor)
leafVisitor, _ := visitor.(Visitor)
return w.traverse(aNode, value, leafVisitor, nodeVisitor)
}
func (w *Walker) traverse(aNode *Node, value interface{}, visitor Visitor, nodeVisitor NodeVisitor) error {
if !aNode.When(value) {
return nil
}
if nodeVisitor != nil && aNode.kind == nodeKindObject {
if err := nodeVisitor(aNode, value); err != nil {
return err
}
}
if aNode.IsLeaf {
if visitor == nil {
return nil
}
return visitor(visitor)
}
ptr := xunsafe.AsPointer(value)
var item interface{}
switch aNode.kind {
case nodeKindObject:
item = aNode.xField.Interface(ptr)
return w.traverse(aNode.child, item, visitor, nodeVisitor)
case nodeKindArray:
sliceLen := aNode.xSlice.Len(ptr)
for i := 0; i < sliceLen; i++ {
item := aNode.xSlice.ValuePointerAt(ptr, i)
if err := w.traverse(aNode.child, item, visitor, nodeVisitor); err != nil {
return err
}
}
}
return nil
}
func (w *Walker) count(aNode *Node, value interface{}) int {
if !aNode.When(value) {
return 0
}
ptr := xunsafe.AsPointer(value)
if ptr == nil {
return 0
}
var result = 0
var item interface{}
if aNode.IsLeaf {
return 1
}
switch aNode.kind {
case nodeKindObject:
item = aNode.xField.Interface(ptr)
return w.count(aNode.child, item)
case nodeKindArray:
sliceLen := aNode.xSlice.Len(ptr)
for i := 0; i < sliceLen; i++ {
item := aNode.xSlice.ValuePointerAt(ptr, i)
result += w.count(aNode.child, item)
}
}
return result
}
func (w *Walker) mapNode(ctx *Context, aNode *Node, value interface{}) error {
if !aNode.When(value) {
return nil
}
srcPtr := xunsafe.AsPointer(value)
if srcPtr == nil {
return nil
}
if aNode.IsLeaf {
destItem := ctx.Next(value)
destItemPtr := xunsafe.AsPointer(destItem)
if err := ctx.mapper.MapStruct(srcPtr, destItemPtr); err != nil {
return err
}
return nil
}
var srcItem interface{}
switch aNode.kind {
case nodeKindObject:
srcItem = aNode.xField.Interface(srcPtr)
return w.mapNode(ctx, aNode.child, srcItem)
case nodeKindArray:
sliceLen := aNode.xSlice.Len(srcPtr)
for i := 0; i < sliceLen; i++ {
item := aNode.xSlice.ValuePointerAt(srcPtr, i)
if err := w.mapNode(ctx, aNode.child, item); err != nil {
return err
}
}
if sliceLen == 0 && ctx.mapper.aggregate {
ctx.Next(nil)
}
}
return nil
}
//NewWalker creates a struct walker
func NewWalker(root *Node) *Walker {
return &Walker{root: root}
}