-
Notifications
You must be signed in to change notification settings - Fork 147
/
apply.go
49 lines (42 loc) · 1.12 KB
/
apply.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
package core
import (
"strings"
"github.com/pkg/errors"
)
// Apply applies a series of changes to a base entry. This function ignores the
// Old value for changes and it assumes all changes are valid to apply against
// the base.
func Apply(base *Entry, changes []*Change) (*Entry, error) {
// Create a mutable copy of base.
result := base.Copy()
// Apply changes.
for _, c := range changes {
// Handle the special case of a root path.
if c.Path == "" {
result = c.New
continue
}
// Crawl down the tree until we reach the parent of the target location.
parent := result
components := strings.Split(c.Path, "/")
for len(components) > 1 {
child, ok := parent.Contents[components[0]]
if !ok {
return nil, errors.New("unable to resolve parent path")
}
parent = child
components = components[1:]
}
// Depending on the new value, either set or remove the entry.
if c.New == nil {
delete(parent.Contents, components[0])
} else {
if parent.Contents == nil {
parent.Contents = make(map[string]*Entry)
}
parent.Contents[components[0]] = c.New
}
}
// Done.
return result, nil
}