-
Notifications
You must be signed in to change notification settings - Fork 6
/
node.go
140 lines (129 loc) · 3.05 KB
/
node.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
129
130
131
132
133
134
135
136
137
138
139
140
package ast
import (
"reflect"
"strings"
"github.com/dipdup-net/go-lib/tools/base"
"github.com/dipdup-net/go-lib/tools/consts"
"github.com/dipdup-net/go-lib/tools/types"
)
// Copy -
func Copy(node Node) Node {
el := reflect.ValueOf(node).Elem()
t := el.Type()
obj := reflect.New(t)
if obj.Kind() == reflect.Ptr {
obj = obj.Elem()
}
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
if !strings.HasSuffix(f.Name, "Type") {
fv := obj.FieldByName(f.Name)
if fv.CanSet() {
val := el.FieldByName(f.Name)
switch val.Kind() {
case reflect.Slice:
sl := reflect.MakeSlice(val.Type(), 0, 0)
for i := 0; i < val.Len(); i++ {
item := val.Index(i).Interface()
if n, ok := item.(Node); ok {
sl = reflect.Append(sl, reflect.ValueOf(Copy(n)))
} else {
sl = reflect.Append(sl, val.Index(i))
}
}
fv.Set(sl)
case reflect.Map:
fv.Set(reflect.MakeMap(val.Type()))
case reflect.Ptr:
if i, ok := val.Interface().(Node); ok {
fv.Set(reflect.ValueOf(Copy(i)))
} else if _, ok := val.Interface().(*OrderedMap); ok {
fv.Set(reflect.ValueOf(NewOrderedMap()))
} else {
fv.Set(val)
}
case reflect.Struct:
s := reflect.New(val.Type()).Elem()
for i := 0; i < val.NumField(); i++ {
sf := s.Field(i)
if sf.CanSet() {
sf.Set(val.Field(i))
}
}
fv.Set(s)
case reflect.Interface:
if i, ok := val.Interface().(Node); ok {
fv.Set(reflect.ValueOf(Copy(i)))
} else {
fv.Set(val)
}
default:
fv.Set(reflect.New(val.Type()).Elem())
}
}
continue
}
val := el.FieldByName(f.Name)
copyVal := Copy(val.Interface().(Node))
fv := obj.FieldByName(f.Name)
fv.Set(reflect.ValueOf(copyVal))
}
return obj.Addr().Interface().(Node)
}
func toBaseNodeInt(val *types.BigInt) *base.Node {
return &base.Node{
IntValue: val,
}
}
func toBaseNodeString(val string) *base.Node {
return &base.Node{
StringValue: &val,
}
}
func toBaseNodeBytes(val string) *base.Node {
return &base.Node{
BytesValue: &val,
}
}
func mapToBaseNodes(data *OrderedMap, optimized bool) (*base.Node, error) {
if data == nil {
return nil, nil
}
node := new(base.Node)
node.Prim = consts.PrimArray
node.Args = make([]*base.Node, 0)
err := data.Range(func(key, value Comparable) (bool, error) {
keyNode, err := key.(Node).ToBaseNode(optimized)
if err != nil {
return true, err
}
var valueNode *base.Node
if value != nil {
valueNode, err = value.(Node).ToBaseNode(optimized)
if err != nil {
return true, err
}
}
node.Args = append(node.Args, &base.Node{
Prim: consts.Elt,
Args: []*base.Node{
keyNode, valueNode,
},
})
return false, nil
})
return node, err
}
func arrayToBaseNode(data []Node, optimized bool) (*base.Node, error) {
node := new(base.Node)
node.Prim = consts.PrimArray
node.Args = make([]*base.Node, 0)
for i := range data {
arg, err := data[i].ToBaseNode(optimized)
if err != nil {
return nil, err
}
node.Args = append(node.Args, arg)
}
return node, nil
}