-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonptr.go
147 lines (132 loc) · 3.63 KB
/
jsonptr.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
package jsonptr
import (
"fmt"
"github.com/0x51-dev/jsonptr/abnf"
"github.com/0x51-dev/jsonptr/abnf/ir"
"github.com/0x51-dev/upeg/parser"
"github.com/0x51-dev/upeg/parser/op"
"strconv"
)
// DEPS: go install github.com/0x51-dev/upeg/cmd/abnf
//go:generate abnf --in=abnf/jsonptr.abnf --out=abnf/jsonptr.go --ignore=unescaped,escaped
type JsonPointer []string
func ParseJsonPointer(ptr string) (JsonPointer, error) {
p, err := parser.New([]rune(ptr))
if err != nil {
return nil, err
}
n, err := p.Parse(op.And{abnf.JsonPointer, op.EOF{}})
if err != nil {
return nil, err
}
return ir.ParseJsonPointer(n)
}
func (ptr JsonPointer) Eval(document map[string]any) (any, error) {
return ptr.evalMap(document)
}
func (ptr JsonPointer) evalAny(v any) (any, error) {
switch v := v.(type) {
case map[string]any:
return ptr.evalMap(v)
case []any:
return ptr.evalArray(v)
default:
return nil, fmt.Errorf("expected map or array, got %T", v)
}
}
func (ptr JsonPointer) evalArray(arr []any) (any, error) {
i, err := strconv.Atoi(ptr[0])
if err != nil {
return nil, err
}
if i < 0 || i >= len(arr) {
return nil, fmt.Errorf("index %d out of bounds", i)
}
v := arr[i]
if len(ptr) == 1 {
return v, nil
}
return ptr[1:].evalAny(v)
}
func (ptr JsonPointer) evalMap(document map[string]any) (any, error) {
if len(ptr) == 0 {
return nil, nil
}
v, ok := document[ptr[0]]
if !ok {
return nil, fmt.Errorf("key %s not found", ptr[0])
}
if len(ptr) == 1 {
return v, nil
}
return ptr[1:].evalAny(v)
}
type RelativeJsonPointer ir.RelativeJsonPointer
func ParseRelativeJsonPointer(ptr string) (*RelativeJsonPointer, error) {
p, err := parser.New([]rune(ptr))
if err != nil {
return nil, err
}
n, err := p.Parse(op.And{abnf.RelativeJsonPointer, op.EOF{}})
if err != nil {
return nil, err
}
r, err := ir.ParseRelativeJsonPointer(n)
if err != nil {
return nil, err
}
return (*RelativeJsonPointer)(r), nil
}
func (ptr RelativeJsonPointer) Eval(start JsonPointer, document map[string]any) (any, error) {
current := start[:]
// 1. Processing the non-negative-integer prefix.
switch ptr.NonNegativeInteger {
case 0: // Skip!
default:
for i := 0; i < ptr.NonNegativeInteger; i++ {
if len(current) == 0 {
// If the current referenced value is the root of the document, then evaluation fails.
return nil, fmt.Errorf("referencing root document")
}
// If the referenced value is an item within an array/object, then the new referenced value is that array/object.
current = current[:len(current)-1]
}
}
// 2. Processing the index-manipulation suffix.
if ptr.IndexManipulation != nil {
v, err := strconv.Atoi(current[len(current)-1])
if err != nil {
return nil, fmt.Errorf("referencing non-integer key")
}
v += *ptr.IndexManipulation
current[len(current)-1] = strconv.Itoa(v)
}
// 3. Processing the JSON Pointer suffix.
if ptr.JsonPointer != nil {
current = append(current, *ptr.JsonPointer...)
} else {
// The remainder of the Relative JSON Pointer is the character '#'.
if len(current) == 0 {
// If the current referenced value is the root of the document, then evaluation fails.
return nil, fmt.Errorf("referencing root document")
}
p, err := current[:len(current)-1].Eval(document)
if err != nil {
return nil, err
}
switch p.(type) {
case nil, map[string]any:
return current[len(current)-1], nil
case []any:
v := current[len(current)-1]
i, err := strconv.Atoi(v)
if err != nil {
return nil, fmt.Errorf("referencing non-integer key")
}
return float64(i), nil
default:
return nil, fmt.Errorf("referencing non-object, non-array")
}
}
return current.Eval(document)
}