-
Notifications
You must be signed in to change notification settings - Fork 4
/
encode.go
86 lines (80 loc) · 1.69 KB
/
encode.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
package ddt
import (
"encoding/json"
"errors"
)
// MarshalJSON ...
func (t *Tree) MarshalJSON() ([]byte, error) {
type TreeAlias Tree
allNodes := getAllNodes(t.Root)
return json.Marshal(&struct {
Nodes []*Node `json:"nodes"`
*TreeAlias
}{
Nodes: allNodes,
TreeAlias: (*TreeAlias)(t),
})
}
// UnmarshalJSON ...
func (t *Tree) UnmarshalJSON(data []byte) error {
type TreeAlias Tree
auxTree := &struct {
Nodes []*Node `json:"nodes"`
*TreeAlias
}{
Nodes: []*Node{},
TreeAlias: (*TreeAlias)(t),
}
err := json.Unmarshal(data, auxTree)
if err != nil {
return err
}
keyParentOf := map[int][]*Node{}
for _, n := range auxTree.Nodes {
if err := addPreprocessFn(t, n); err != nil {
return err
}
if isRoot(n) {
t.Root = n
continue
}
children := keyParentOf[n.ParentID]
keyParentOf[n.ParentID] = append(children, n)
}
setChildrenToParentNodes(t.Root, keyParentOf)
return nil
}
func getAllNodes(root *Node) []*Node {
var res []*Node
queue := []*Node{root}
for len(queue) != 0 {
top := queue[0]
queue = queue[1:]
res = append(res, top)
for _, c := range top.Children {
queue = append(queue, c)
}
}
return res
}
func setChildrenToParentNodes(root *Node, keyParentOf map[int][]*Node) {
queue := []*Node{root}
for len(queue) != 0 {
top := queue[0]
queue = queue[1:]
for _, n := range keyParentOf[top.ID] {
top.Children = append(top.Children, n)
queue = append(queue, n)
}
}
}
func addPreprocessFn(t *Tree, n *Node) error {
if !n.PreProcessFn.Empty() {
preProcessFn, ok := t.Functions[n.PreProcessFn.Name]
if !ok {
return errors.New("function name not found")
}
n.PreProcessFn.Function = preProcessFn.Function
}
return nil
}