-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathsimple_node.go
More file actions
225 lines (183 loc) · 5.27 KB
/
simple_node.go
File metadata and controls
225 lines (183 loc) · 5.27 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package gedcom
import (
"bytes"
"encoding/json"
"fmt"
"sync"
)
// SimpleNode is used as the default node type when there is no more appropriate
// or specific type to use.
type SimpleNode struct {
tag Tag
value string
pointer string
children Nodes
}
// newSimpleNode creates a non-specific node.
//
// Unlike all of the other node types this constructor is not public because it
// is used internally by NewNode if a specific node type can not be determined.
func newSimpleNode(tag Tag, value, pointer string, children ...Node) *SimpleNode {
return &SimpleNode{
tag: tag,
value: value,
pointer: pointer,
children: children,
}
}
// If the node is nil the result will be an empty tag.
func (node *SimpleNode) Tag() Tag {
if node == nil {
return Tag{}
}
return node.tag
}
// If the node is nil the result will be an empty string.
func (node *SimpleNode) Value() string {
if node == nil {
return ""
}
return node.value
}
// If the node is nil the result will be an empty string.
func (node *SimpleNode) Pointer() string {
if node == nil {
return ""
}
return node.pointer
}
// Equals compares two nodes for value equality.
//
// 1. If either or both nodes are nil then false is always returned.
// 2. Nodes are compared only by their root value (shallow) meaning any value
// for the child nodes is ignored.
// 3. The document the node belongs to is not taken into consideration to be
// able to compare nodes by value across different documents.
// 4. A node is considered to have the same value (and therefore be equal) is
// both nodes share the all of the same tag, value and pointer.
func (node *SimpleNode) Equals(node2 Node) bool {
if node == nil {
return false
}
if IsNil(node2) {
return false
}
tag := node2.Tag()
if node.tag != tag {
return false
}
value := node2.Value()
if node.value != value {
return false
}
return node.pointer == node2.Pointer()
}
// If the node is nil the result will also be nil.
func (node *SimpleNode) Nodes() Nodes {
if node == nil {
return nil
}
return node.children
}
func (node *SimpleNode) AddNode(n Node) {
node.children = append(node.children, n)
// This is pretty crude and nasty. I'm sorry if your workflow is to switch
// between small changes and large sweeping reads but this will do for now.
//
// We can't simply remove this node because we would have to make sure we
// work our way up the chain which we have no easy way of doing right now.
nodeCache = &sync.Map{}
}
func (node *SimpleNode) DeleteNode(n Node) (didDelete bool) {
node.children, didDelete = node.children.deleteNode(n)
return
}
// If the node is nil the result be an empty string.
func (node *SimpleNode) String() string {
if node == nil {
return ""
}
return node.value
}
func (node *SimpleNode) MarshalJSON() ([]byte, error) {
m := node.ObjectMap()
return json.Marshal(m)
}
func (node *SimpleNode) ObjectMap() map[string]interface{} {
m := map[string]interface{}{
"Tag": node.Tag().Tag(),
}
if node.Value() != "" {
m["Value"] = node.Value()
}
if node.Pointer() != "" {
m["Pointer"] = node.Pointer()
}
nodes := node.Nodes()
if len(nodes) > 0 {
m["Nodes"] = nodes
}
return m
}
// ShallowCopy returns a new node that has the same properties as the input node
// without any children.
//
// If the input node is nil then nil is also returned.
func (node *SimpleNode) ShallowCopy() Node {
if IsNil(node) {
return nil
}
tag := node.Tag()
value := node.Value()
pointer := node.Pointer()
return NewNode(tag, value, pointer)
}
// GEDCOMString is the recursive version of GEDCOMLine. It will render a node
// and all of its children (if any) as a multi-line GEDCOM string.
//
// GEDCOMString will not work with a nil value. You can use the package
// GEDCOMString function to gracefully handle nils.
//
// The indent will only be included if it is at least 0. If you want to use
// GEDCOMString to compare the string values of nodes or exclude the indent you
// should use the NoIndent constant.
func (node *SimpleNode) GEDCOMString(indent int) string {
document := NewDocumentWithNodes(Nodes{node})
return document.GEDCOMString(indent)
}
// GEDCOMLine converts a node into its single line GEDCOM value. It is used
// several places including the actual Encoder.
//
// GEDCOMLine, as the name would suggest, does not handle children. You can use
// GEDCOMString if you need the child nodes as well.
//
// GEDCOMLine will not work with a nil value. You can use the package GEDCOMLine
// function to gracefully handle nils.
//
// The indent will only be included if it is at least 0. If you want to use
// GEDCOMLine to compare the string values of nodes or exclude the indent you
// should use the NoIndent constant.
func (node *SimpleNode) GEDCOMLine(indent int) string {
buf := bytes.NewBufferString("")
if indent >= 0 {
buf.WriteString(fmt.Sprintf("%d ", indent))
}
if p := node.Pointer(); p != "" {
buf.WriteString(fmt.Sprintf("@%s@ ", p))
}
buf.WriteString(node.Tag().Tag())
if v := node.Value(); v != "" {
buf.WriteByte(' ')
buf.WriteString(v)
}
return buf.String()
}
// SetNodes replaces all of the child nodes.
//
// You can use SetNodes(nil) to remove all child nodes.
func (node *SimpleNode) SetNodes(nodes Nodes) {
node.children = nodes
}
func (node *SimpleNode) RawSimpleNode() *SimpleNode {
return node
}