-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpointer.go
176 lines (167 loc) · 4.61 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
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
package jsonpointergo
import (
"fmt"
"strconv"
"strings"
)
const (
JSONPointerEmptyPointer = ""
JSONPointerSeparatorToken = "/"
JSONPointerEscapeToken = "~"
JSONPointerSlashEncoded = "~1"
JSONPointerTildaEncoded = "~0"
)
// JSONObject is a type alias for a map with string keys and values of
// any type
type JSONObject = map[string]any
// JSONPointer struct holds the parsed reference tokens of a JSON
// Pointer
type JSONPointer struct {
// Slice of reference tokens derived from the JSON Pointer
referenceTokens []string
}
// NewJSONPointer creates a new JSONPointer instance from a JSON
// Pointer string
func NewJSONPointer(jsonPointer string) (*JSONPointer, error) {
tokens, err := parseJSONPointerString(jsonPointer)
if err != nil {
return nil, err
}
return &JSONPointer{
referenceTokens: tokens,
}, nil
}
// parseJSONPointerString parses a JSON Pointer string into its
// reference tokens
func parseJSONPointerString(jsonPointer string) ([]string, error) {
if jsonPointer == JSONPointerEmptyPointer {
return []string{}, nil
}
if !strings.HasPrefix(jsonPointer, JSONPointerSeparatorToken) {
return nil, fmt.Errorf(
"jsonpointer: a jsonpointer should start with a reference to the root value: %v",
JSONPointerSeparatorToken,
)
}
// Split the JSON Pointer into tokens
tokens := strings.Split(jsonPointer, JSONPointerSeparatorToken)
return tokens[1:], nil
}
// GetValue retrieves the value from the JSON document based on the
// JSON Pointer
func (jp *JSONPointer) GetValue(document JSONObject) (any, error) {
if document == nil {
return nil, fmt.Errorf(
"jsonpointer: the JSON document provided is nil",
)
}
var subDocument any
// Start with the root of the JSON document
subDocument = document
for _, tokenRefEncoded := range jp.referenceTokens {
tokenRef := decodeJSONPointerReference(tokenRefEncoded)
switch current := subDocument.(type) {
case JSONObject:
value, ok := current[tokenRef]
if !ok {
return nil, fmt.Errorf(
"jsonpointer: the document provided does not have the following reference: %v",
tokenRef,
)
}
subDocument = value
case []any:
index, err := strconv.Atoi(tokenRef)
if err != nil {
return nil, fmt.Errorf(
"jsonpointer: the reference is trying to access a field on an array: %v",
tokenRef,
)
}
if index < 0 || index >= len(current) {
return nil, fmt.Errorf(
"jsonpointer: the index provided [%v] is trying to access an out of bound item on an array of length %v",
index,
len(current),
)
}
subDocument = current[index]
default:
return nil, fmt.Errorf(
"jsonpointer: the reference is trying to access a single value: %v. Type of subdocument: %T",
tokenRef, subDocument,
)
}
}
return subDocument, nil
}
// SetValue sets the value in the JSON document based on the JSON
// Pointer
func (jp *JSONPointer) SetValue(document JSONObject, value any) error {
if document == nil {
return fmt.Errorf(
"jsonpointer: the JSON document provided is nil",
)
}
if len(jp.referenceTokens) == 0 {
document[JSONPointerEmptyPointer] = value
return nil
}
var subDocument any
subDocument = document
for _, tokenRefEncoded := range jp.referenceTokens {
tokenRef := decodeJSONPointerReference(tokenRefEncoded)
switch current := subDocument.(type) {
case JSONObject:
fmt.Printf("current: %v\n", current)
value, ok := current[tokenRef]
if !ok {
return fmt.Errorf(
"jsonpointer: the document provided does not have the following reference: %v",
tokenRef,
)
}
subDocument = value
case []any:
fmt.Printf("current 1: %v\n", current)
index, err := strconv.Atoi(tokenRef)
if err != nil {
return fmt.Errorf(
"jsonpointer: the reference is trying to access a field on an array: %v",
tokenRef,
)
}
if index < 0 || index >= len(current) {
return fmt.Errorf(
"jsonpointer: the index provided [%v] is trying to access an out of bound item on an array of length %v",
index,
len(current),
)
}
subDocument = current[index]
default:
fmt.Printf("current 2: %v\n", current)
return fmt.Errorf(
"jsonpointer: the reference is trying to access a single value: %v. Type of subdocument: %T",
tokenRef, subDocument,
)
}
}
return nil
}
// decodeJSONPointerReference decodes a reference token by replacing
// escape sequences
func decodeJSONPointerReference(ref string) string {
// Replace "~1" with "/"
ref = strings.ReplaceAll(
ref,
JSONPointerSlashEncoded,
JSONPointerSeparatorToken,
)
// Replace "~0" with "~"
return strings.ReplaceAll(
ref,
JSONPointerTildaEncoded,
JSONPointerEscapeToken,
)
}