This repository was archived by the owner on Jan 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcontainer.go
183 lines (140 loc) · 3.16 KB
/
container.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package yamlpatch
import (
"fmt"
"strconv"
"strings"
)
// Container is the interface for performing operations on Nodes
type Container interface {
Get(keyOrIndex string) (*Node, error)
Set(keyOrIndex string, val *Node) error
Add(keyOrIndex string, val *Node) error
Remove(keyOrIndex string) error
}
type nodeMap map[interface{}]*Node
func (n *nodeMap) setAtRoot(val *Node) error {
switch vt := val.Container().(type) {
case *nodeMap:
for k, v := range *vt {
(*n)[k] = v
}
}
return nil
}
func (n *nodeMap) Set(key string, val *Node) error {
if len(key) == 0 {
return n.setAtRoot(val)
}
(*n)[key] = val
return nil
}
func (n *nodeMap) Add(key string, val *Node) error {
if len(key) == 0 {
return n.setAtRoot(val)
}
(*n)[key] = val
return nil
}
func (n *nodeMap) Get(key string) (*Node, error) {
return (*n)[key], nil
}
func (n *nodeMap) Remove(key string) error {
_, ok := (*n)[key]
if !ok {
return fmt.Errorf("Unable to remove nonexistent key: %s", key)
}
delete(*n, key)
return nil
}
type nodeSlice []*Node
func (n *nodeSlice) Set(index string, val *Node) error {
i, err := strconv.Atoi(index)
if err != nil {
return err
}
sz := len(*n)
if i+1 > sz {
sz = i + 1
}
ary := make([]*Node, sz)
cur := *n
copy(ary, cur)
if i >= len(ary) {
return fmt.Errorf("Unable to access invalid index: %d", i)
}
ary[i] = val
*n = ary
return nil
}
func (n *nodeSlice) Add(index string, val *Node) error {
if index == "-" {
*n = append(*n, val)
return nil
}
i, err := strconv.Atoi(index)
if err != nil {
return err
}
ary := make([]*Node, len(*n)+1)
cur := *n
copy(ary[0:i], cur[0:i])
ary[i] = val
copy(ary[i+1:], cur[i:])
*n = ary
return nil
}
func (n *nodeSlice) Get(index string) (*Node, error) {
i, err := strconv.Atoi(index)
if err != nil {
return nil, err
}
if i >= 0 && i <= len(*n)-1 {
return (*n)[i], nil
}
return nil, fmt.Errorf("Unable to access invalid index: %d", i)
}
func (n *nodeSlice) Remove(index string) error {
i, err := strconv.Atoi(index)
if err != nil {
return err
}
cur := *n
if i >= len(cur) {
return fmt.Errorf("Unable to remove invalid index: %d", i)
}
ary := make([]*Node, len(cur)-1)
copy(ary[0:i], cur[0:i])
copy(ary[i:], cur[i+1:])
*n = ary
return nil
}
func findContainer(c Container, path *OpPath) (Container, string, error) {
parts, key, err := path.Decompose()
if err != nil {
return nil, "", err
}
foundContainer := c
for _, part := range parts {
node, err := foundContainer.Get(decodePatchKey(part))
if err != nil {
return nil, "", err
}
if node == nil {
return nil, "", fmt.Errorf("path does not exist: %s", path)
}
foundContainer = node.Container()
}
return foundContainer, decodePatchKey(key), nil
}
// From http://tools.ietf.org/html/rfc6901#section-4 :
//
// Evaluation of each reference token begins by decoding any escaped
// character sequence. This is performed by first transforming any
// occurrence of the sequence '~1' to '/', and then transforming any
// occurrence of the sequence '~0' to '~'.
var (
rfc6901Decoder = strings.NewReplacer("~1", "/", "~0", "~")
)
func decodePatchKey(k string) string {
return rfc6901Decoder.Replace(k)
}