Skip to content

Commit e8beed2

Browse files
committed
refactor: make the document type analysis a switch
1 parent f48838c commit e8beed2

File tree

1 file changed

+15
-23
lines changed

1 file changed

+15
-23
lines changed

pointer.go

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -65,48 +65,40 @@ func (jp *JSONPointer) GetValue(document JSONObject) (any, error) {
6565
var subDocument any
6666
// Start with the root of the JSON document
6767
subDocument = document
68-
for i, tokenRefEncoded := range jp.referenceTokens {
68+
for _, tokenRefEncoded := range jp.referenceTokens {
6969
tokenRef := decodeJSONPointerReference(tokenRefEncoded)
70-
// Handle the case where the value is a JSON object
71-
jsonDoc, ok := subDocument.(JSONObject)
72-
if ok {
73-
value, ok := jsonDoc[tokenRef]
70+
switch current := subDocument.(type) {
71+
case JSONObject:
72+
value, ok := current[tokenRef]
7473
if !ok {
7574
return nil, fmt.Errorf(
76-
"jsonpointer: the document provided does not have the following reference: %v, %v",
75+
"jsonpointer: the document provided does not have the following reference: %v",
7776
tokenRef,
78-
i,
7977
)
8078
}
8179
subDocument = value
82-
continue
83-
}
84-
// Handle the case where the value is an Array
85-
jsonArray, ok := subDocument.([]any)
86-
if ok {
80+
case []any:
8781
index, err := strconv.Atoi(tokenRef)
8882
if err != nil {
8983
return nil, fmt.Errorf(
9084
"jsonpointer: the reference is trying to access a field on an array: %v",
9185
tokenRef,
9286
)
9387
}
94-
if index < 0 || index >= len(jsonArray) {
88+
if index < 0 || index >= len(current) {
9589
return nil, fmt.Errorf(
96-
"jsonpointer: the index provided [%v] is trying to access an out of bond item on an array of length %v",
90+
"jsonpointer: the index provided [%v] is trying to access an out of bound item on an array of length %v",
9791
index,
98-
len(jsonArray),
92+
len(current),
9993
)
10094
}
101-
subDocument = jsonArray[index]
102-
continue
95+
subDocument = current[index]
96+
default:
97+
return nil, fmt.Errorf(
98+
"jsonpointer: the reference is trying to access a single value: %v. Type of subdocument: %T",
99+
tokenRef, subDocument,
100+
)
103101
}
104-
// Handle the case where the value is a single value
105-
return nil, fmt.Errorf(
106-
"jsonpointer: the reference is trying to access a single value: %v. Type of subdocument: %T",
107-
tokenRef,
108-
subDocument,
109-
)
110102
}
111103
return subDocument, nil
112104
}

0 commit comments

Comments
 (0)