forked from cnabio/cnab-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
36 lines (29 loc) · 811 Bytes
/
utils.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
package replacement
import (
"strings"
)
// Abstraction over map to permit generic traversal and substitution
type docmap interface {
get(key string) (interface{}, bool)
set(key string, value interface{})
asInstance(value interface{}) (docmap, bool)
}
func parseSelector(selector string) []string {
return strings.Split(selector, ".")
}
func replaceIn(dict docmap, selectorPath []string, value string) error {
entry, ok := dict.get(selectorPath[0])
if !ok {
return ErrSelectorNotFound
}
if len(selectorPath) == 1 {
dict.set(selectorPath[0], value)
return nil
}
entryDict, ok := dict.asInstance(entry)
if !ok {
return ErrSelectorNotFound // Because we have reached a terminal with some of the selectorPath to go
}
rest := selectorPath[1:]
return replaceIn(entryDict, rest, value)
}