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
|
// Copyright (c) 2020, Maxime Soulé
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.
package util
import (
"strconv"
"strings"
)
var jsonPointerEsc = strings.NewReplacer("~0", "~", "~1", "/")
const (
ErrJSONPointerInvalid = "invalid JSON pointer"
ErrJSONPointerKeyNotFound = "key not found"
ErrJSONPointerArrayNoIndex = "array but not an index in JSON pointer"
ErrJSONPointerArrayOutOfRange = "out of array range"
ErrJSONPointerArrayBadType = "not a map nor an array"
)
type JSONPointerError struct {
Type string
Pointer string
}
func (e *JSONPointerError) Error() string {
if e.Pointer == "" {
return e.Type
}
return e.Type + " @" + e.Pointer
}
// JSONPointer returns the value corresponding to JSON pointer
// pointer in v as [RFC 6901] specifies it. To be searched, v has
// to contains map[string]any or []any values. All
// other types fail to be searched.
//
// [RFC 6901]: https://tools.ietf.org/html/rfc6901
func JSONPointer(v any, pointer string) (any, error) {
if !strings.HasPrefix(pointer, "/") {
if pointer == "" {
return v, nil
}
return nil, &JSONPointerError{Type: ErrJSONPointerInvalid}
}
pos := 0
for _, part := range strings.Split(pointer[1:], "/") {
pos += 1 + len(part)
part = jsonPointerEsc.Replace(part)
switch tv := v.(type) {
case map[string]any:
var ok bool
v, ok = tv[part]
if !ok {
return nil, &JSONPointerError{
Type: ErrJSONPointerKeyNotFound,
Pointer: pointer[:pos],
}
}
case []any:
i, err := strconv.Atoi(part)
if err != nil || i < 0 {
return nil, &JSONPointerError{
Type: ErrJSONPointerArrayNoIndex,
Pointer: pointer[:pos],
}
}
if i >= len(tv) {
return nil, &JSONPointerError{
Type: ErrJSONPointerArrayOutOfRange,
Pointer: pointer[:pos],
}
}
v = tv[i]
default:
return nil, &JSONPointerError{
Type: ErrJSONPointerArrayBadType,
Pointer: pointer[:pos],
}
}
}
return v, nil
}
|