-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpointer.go
45 lines (37 loc) · 1.1 KB
/
pointer.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
package jsonpatch
import (
"strings"
)
const (
separator = "/"
wildcard = "*"
tilde = "~"
)
// JSONPointer identifies a specific value within a JSON object specified in RFC 6901
type JSONPointer []string
// ParseJSONPointer converts a string into a JSONPointer
func ParseJSONPointer(str string) JSONPointer {
return strings.Split(str, separator)
}
// String returns a string representation of a JSONPointer
func (p JSONPointer) String() string {
return strings.Join(p, separator)
}
// Add adds an element to the JSONPointer
func (p JSONPointer) Add(elem string) JSONPointer {
elem = strings.ReplaceAll(elem, tilde, "~0")
elem = strings.ReplaceAll(elem, separator, "~1")
return append(p, elem)
}
// Match matches a pattern which is a string JSONPointer which might also contains wildcards
func (p JSONPointer) Match(pattern string) bool {
elements := strings.Split(pattern, separator)
for i, element := range elements {
if element == wildcard {
continue
} else if i >= len(p) || element != p[i] {
return false
}
}
return strings.HasSuffix(pattern, wildcard) || len(p) == len(elements)
}