forked from wI2L/jsondiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpointer.go
57 lines (45 loc) · 1.28 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
46
47
48
49
50
51
52
53
54
55
56
57
package jsondiff
import (
"strconv"
"strings"
)
const jsonPointerSep = "/"
var (
// rfc6901Replacer is a replacer used to escape JSON
// pointer strings in compliance with the JavaScript
// Object Notation Pointer syntax.
// https://tools.ietf.org/html/rfc6901
rfc6901Replacer = strings.NewReplacer("~", "~0", "/", "~1")
// dotPathReplacer converts a RFC6901 JSON pointer to
// a JSON path, while also escaping any existing dot
// characters present in the original pointer.
dotPathReplacer = strings.NewReplacer(".", "\\.", "/", ".")
)
type jsonNode struct {
ptr pointer
val interface{}
}
// pointer represents a RFC6901 JSON Pointer.
type pointer string
const emptyPtr = pointer("")
// String implements the fmt.Stringer interface.
func (p pointer) String() string {
return string(p)
}
func (p pointer) toJSONPath() string {
if len(p) > 0 {
return dotPathReplacer.Replace(string(p)[1:])
}
// @this is a special modifier that can
// be used to retrieve the root path.
return "@this"
}
func (p pointer) appendKey(key string) pointer {
return pointer(string(p) + jsonPointerSep + rfc6901Replacer.Replace(key))
}
func (p pointer) appendIndex(idx int) pointer {
return pointer(string(p) + jsonPointerSep + strconv.Itoa(idx))
}
func (p pointer) isRoot() bool {
return len(p) == 0
}