-
Notifications
You must be signed in to change notification settings - Fork 4
/
miguel.go
95 lines (86 loc) · 2.03 KB
/
miguel.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
package ast
import (
"fmt"
"reflect"
"strings"
"github.com/dipdup-net/go-lib/tools/consts"
)
// MiguelNode -
type MiguelNode struct {
Prim string `json:"prim,omitempty"`
Type string `json:"type,omitempty"`
Name *string `json:"name,omitempty"`
From interface{} `json:"from,omitempty"`
DiffType string `json:"diff_type,omitempty"`
Value interface{} `json:"value,omitempty"`
Children []*MiguelNode `json:"children,omitempty"`
}
// String -
func (node *MiguelNode) String() string {
return node.print(0)
}
func (node *MiguelNode) print(depth int) string {
var s strings.Builder
s.WriteString(strings.Repeat(consts.DefaultIndent, depth))
if node.Value == nil {
s.WriteString("option=nil")
} else {
s.WriteString(fmt.Sprintf("prim=%s", node.Prim))
}
if node.Name != nil {
s.WriteString(fmt.Sprintf(" name=%s", *node.Name))
}
if node.Type != "" {
s.WriteString(fmt.Sprintf(" type=%s", node.Type))
}
if node.Value != nil {
s.WriteString(fmt.Sprintf(" value=%v", node.Value))
}
s.WriteByte('\n')
for i := range node.Children {
s.WriteString(node.Children[i].print(depth + 1))
}
return s.String()
}
// Compare -
func (node *MiguelNode) Compare(second *MiguelNode) bool {
if second == nil {
return false
}
if node.Prim != second.Prim || node.Type != second.Type {
return false
}
if !isInterfaceEqual(node.Name, second.Name) {
return false
}
if !isInterfaceEqual(node.Value, second.Value) {
return false
}
if len(node.Children) != len(second.Children) {
return false
}
for i := 0; i < len(node.Children); i++ {
if !node.Children[i].Compare(second.Children[i]) {
return false
}
}
return true
}
func (node *MiguelNode) setDiffType(typ string) {
node.DiffType = typ
for i := range node.Children {
node.Children[i].setDiffType(typ)
}
}
func isInterfaceEqual(x, y interface{}) bool {
switch {
case x == nil && y == nil:
return true
case x != nil && y == nil:
return false
case x == nil && y != nil:
return false
default:
return reflect.DeepEqual(x, y)
}
}