forked from vmware-archive/yaml-patch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pathfinder.go
109 lines (90 loc) · 2.29 KB
/
pathfinder.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
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
package yamlpatch
import (
"fmt"
"strings"
)
// PathFinder can be used to find RFC6902-standard paths given non-standard
// (key=value) pointer syntax
type PathFinder struct {
root Container
}
// NewPathFinder takes an interface that represents a YAML document and returns
// a new PathFinder
func NewPathFinder(container Container) *PathFinder {
return &PathFinder{
root: container,
}
}
// Find expands the given path into all matching paths, returning the canonical
// versions of those matching paths
func (p *PathFinder) Find(path string) []string {
parts := strings.Split(path, "/")
if parts[1] == "" {
return []string{"/"}
}
routes := map[string]Container{
"": p.root,
}
for _, part := range parts[1:] {
routes = find(decodePatchKey(part), routes)
}
var paths []string
for k := range routes {
paths = append(paths, k)
}
return paths
}
func find(part string, routes map[string]Container) map[string]Container {
matches := map[string]Container{}
for prefix, container := range routes {
if part == "-" {
for k := range routes {
matches[fmt.Sprintf("%s/-", k)] = routes[k]
}
return matches
}
if kv := strings.Split(part, "="); len(kv) == 2 {
if newMatches := findAll(prefix, kv[0], kv[1], container); len(newMatches) > 0 {
matches = newMatches
}
continue
}
if node, err := container.Get(part); err == nil {
path := fmt.Sprintf("%s/%s", prefix, part)
if node == nil {
matches[path] = container
} else {
matches[path] = node.Container()
}
}
}
return matches
}
func findAll(prefix, findKey, findValue string, container Container) map[string]Container {
if container == nil {
return nil
}
if v, err := container.Get(findKey); err == nil && v != nil {
if vs, ok := v.Value().(string); ok && vs == findValue {
return map[string]Container{
prefix: container,
}
}
}
matches := map[string]Container{}
switch it := container.(type) {
case *nodeMap:
for k, v := range *it {
for route, match := range findAll(fmt.Sprintf("%s/%s", prefix, k), findKey, findValue, v.Container()) {
matches[route] = match
}
}
case *nodeSlice:
for i, v := range *it {
for route, match := range findAll(fmt.Sprintf("%s/%d", prefix, i), findKey, findValue, v.Container()) {
matches[route] = match
}
}
}
return matches
}