-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathpointer.go
More file actions
65 lines (55 loc) · 1.54 KB
/
pointer.go
File metadata and controls
65 lines (55 loc) · 1.54 KB
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
package jsonpatch
import (
"slices"
"strings"
)
// JSONPointer identifies a specific value within a JSON object specified in RFC 6901
type JSONPointer struct {
path []string
tags []string
}
func NewJSONPointerWithPrefix() JSONPointer {
return JSONPointer{
path: []string{},
tags: []string{},
}
}
// ParseJSONPointer converts a string into a JSONPointer
func ParseJSONPointer(str string) JSONPointer {
return JSONPointer{
path: strings.Split(str, separator),
tags: []string{},
}
}
// String returns a string representation of a JSONPointer
func (p JSONPointer) String() string {
return strings.Join(p.path, 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")
p.path = append(p.path, elem)
return p
}
// 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.path) || element != p.path[i] {
return false
}
}
return strings.HasSuffix(pattern, wildcard) || len(p.path) == len(elements)
}
// AddTags override tags referencing the JSONPointer
func (p JSONPointer) AddTags(tags []string) JSONPointer {
p.tags = tags
return p
}
// AddTags override tags referencing the JSONPointer
func (p JSONPointer) ShouldOmite() bool {
return slices.Contains(p.tags, "omitempty")
}