forked from wI2L/jsondiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pointer.go
50 lines (40 loc) · 953 Bytes
/
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
package jsondiff
import (
"strconv"
"strings"
)
type (
// pointer represents a RFC6901 JSON Pointer.
pointer string
jsonNode struct {
ptr pointer
val interface{}
}
)
const (
emptyPtr = pointer("")
)
var (
separator = "/"
// 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")
)
func UpdateSeparator(newSeparator string) {
separator = newSeparator
}
// String implements the fmt.Stringer interface.
func (p pointer) String() string {
return string(p)
}
func (p pointer) appendKey(key string) pointer {
return pointer(p.String() + separator + rfc6901Replacer.Replace(key))
}
func (p pointer) appendIndex(idx int) pointer {
return pointer(p.String() + separator + strconv.Itoa(idx))
}
func (p pointer) isRoot() bool {
return len(p) == 0
}