-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcopy.go
45 lines (40 loc) · 1.4 KB
/
copy.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
// Copying
//
// All nodes (since they implement the Node interface) also implement the
// NodeCopier interface which provides the ShallowCopy() function.
//
// A shallow copy returns a new node with all the same properties, but no
// children.
//
// On the other hand there is a DeepCopy function which returns a new node with
// all recursive children also copied. This ensures that the new returned node
// can be manipulated without affecting the original node or any of its
// children.
package gedcom
type NodeCopier interface {
// ShallowCopy returns a new node with all the same properties, but no
// children.
//
// You should assume that it is not safe to use ShallowCopy() on a nil
// value.
//
// See DeepCopy() for copying nodes recursively.
ShallowCopy() Node
}
// DeepCopy returns a new node with all recursive children also duplicated. The
// document provided will be where the new node will be attached. This can be
// the same document, but it must not be nil.
func DeepCopy(node Node, document *Document) Node {
if IsNil(node) {
return nil
}
// We must track the last family seen for nodes that require a family. For
// example, husband, wife and child nodes.
var family *FamilyNode
return Filter(node, document, func(node Node) (newNode Node, traverseChildren bool) {
if fam, ok := node.(*FamilyNode); ok {
family = fam
}
return shallowCopyNode(node, document, family), true
})
}