@@ -65,48 +65,40 @@ func (jp *JSONPointer) GetValue(document JSONObject) (any, error) {
65
65
var subDocument any
66
66
// Start with the root of the JSON document
67
67
subDocument = document
68
- for i , tokenRefEncoded := range jp .referenceTokens {
68
+ for _ , tokenRefEncoded := range jp .referenceTokens {
69
69
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 ]
74
73
if ! ok {
75
74
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" ,
77
76
tokenRef ,
78
- i ,
79
77
)
80
78
}
81
79
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 :
87
81
index , err := strconv .Atoi (tokenRef )
88
82
if err != nil {
89
83
return nil , fmt .Errorf (
90
84
"jsonpointer: the reference is trying to access a field on an array: %v" ,
91
85
tokenRef ,
92
86
)
93
87
}
94
- if index < 0 || index >= len (jsonArray ) {
88
+ if index < 0 || index >= len (current ) {
95
89
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" ,
97
91
index ,
98
- len (jsonArray ),
92
+ len (current ),
99
93
)
100
94
}
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
+ )
103
101
}
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
- )
110
102
}
111
103
return subDocument , nil
112
104
}
0 commit comments