-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
57 lines (48 loc) · 1.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
package st2
import (
"strings"
)
type Node struct {
Field string
Type Type
Children []*Node
fingerprint string
}
func (node Node) FieldCamel() string {
return Camel(node.Field)
}
func (node *Node) Fingerprint() string {
if node.fingerprint == "" {
node.fingerprint = node.fingerprintHelper()
}
return node.fingerprint
}
func (node *Node) fingerprintHelper() string {
switch node.Type {
case ArrayVal:
child := "null"
if len(node.Children) > 0 {
child = node.Children[0].Field + ":" + node.Children[0].Fingerprint()
}
return "[" + child + "]"
case StructLikeVal:
children := make([]string, 0)
for _, child := range node.Children {
finger := child.Fingerprint()
children = append(children, child.Field+":"+finger)
}
return "{" + strings.Join(children, ";") + "}"
default:
return node.Type.Json()
}
}
type NodeList []*Node
func (l NodeList) Len() int {
return len(l)
}
func (l NodeList) Less(i, j int) bool {
return l[i].Field < l[j].Field
}
func (l NodeList) Swap(i, j int) {
l[i], l[j] = l[j], l[i]
}