-
Notifications
You must be signed in to change notification settings - Fork 1
/
array_property_diff.go
38 lines (32 loc) · 996 Bytes
/
array_property_diff.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
package main
// ArrayPropertyDiff represents an array property that should change for a specific node.
type ArrayPropertyDiff struct {
nodeID uint64
property *ArrayProperty
}
// NewArrayPropertyDiff creates a new array property diff
func NewArrayPropertyDiff(id uint64, prop *ArrayProperty) *ArrayPropertyDiff {
return &ArrayPropertyDiff{
nodeID: id,
property: prop,
}
}
// Apply will check the node for matching specific criteria and if it passes an
// entirely new node will be created that contains the proper diff.
func (d ArrayPropertyDiff) Apply(n *Node) (*Node, bool) {
if n.id != d.nodeID {
return n, false
}
patchedNode := n.ShallowCopy()
for i, p := range patchedNode.ArrayProperties {
if p.TypeCode == d.property.TypeCode {
patchedNode.ArrayProperties[i] = d.property
return patchedNode, true
}
}
return patchedNode, true
}
// NodeID is the id of the node we want to apply the dif too
func (d ArrayPropertyDiff) NodeID() uint64 {
return d.nodeID
}