forked from go-swagger/go-swagger
-
Notifications
You must be signed in to change notification settings - Fork 1
/
node.go
47 lines (39 loc) · 949 Bytes
/
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
package diff
// Node is the position od a diff in a spec
type Node struct {
Field string `json:"name,omitempty"`
TypeName string `json:"type,omitempty"`
IsArray bool `json:"is_array,omitempty"`
ChildNode *Node `json:"child,omitempty"`
}
// String std string render
func (n *Node) String() string {
name := n.Field
if n.IsArray {
name = "array[" + n.TypeName + "]"
}
if n.ChildNode != nil {
return name + "." + n.ChildNode.String()
}
if len(n.TypeName) > 0 {
return name + " : " + n.TypeName
}
return name
}
// AddLeafNode Adds (recursive) a Child to the first non-nil child found
func (n *Node) AddLeafNode(toAdd *Node) *Node {
if n.ChildNode == nil {
n.ChildNode = toAdd
} else {
n.ChildNode.AddLeafNode(toAdd)
}
return n
}
//Copy deep copy of this node and children
func (n Node) Copy() *Node {
newNode := n
if newNode.ChildNode != nil {
n.ChildNode = newNode.ChildNode.Copy()
}
return &newNode
}