-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjson.go
More file actions
194 lines (180 loc) · 4.96 KB
/
json.go
File metadata and controls
194 lines (180 loc) · 4.96 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package nflex
import (
"strconv"
"github.com/pkg/errors"
"github.com/valyala/fastjson"
)
type parsedJSON struct {
debugID int
value *fastjson.Value
pathToHere []string
}
func UnmarshalJSON(data []byte) (Source, error) {
var parser fastjson.Parser
value, err := parser.ParseBytes(data)
if err != nil {
return nil, errors.Wrap(err, "json")
}
return parsedJSON{
debugID: debugID(),
value: value,
}, nil
}
func (p parsedJSON) Exists(key ...string) bool {
v := p.value.Get(key...)
return v != nil
}
func (p parsedJSON) Recurse(key ...string) Source {
if len(key) == 0 {
debug("nflex/json: Recurse() ->", id(p))
return p
}
v := p.value.Get(key...)
if v == nil {
debug("nflex/json: Recurse(", key, ")", id(p), "-> nil")
return nil
}
n := parsedJSON{
value: v,
pathToHere: combine(p.pathToHere, key),
debugID: debugID(),
}
debug("nflex/json: Recurse(", key, ")", id(p), "->", id(n))
return n
}
func (p parsedJSON) GetBool(key ...string) (bool, error) {
v := p.value.Get(key...)
if v == nil {
return false, errors.Wrapf(ErrDoesNotExist, "key %v does not exist", combine(p.pathToHere, key))
}
switch v.Type() {
case fastjson.TypeTrue:
return true, nil
case fastjson.TypeFalse:
return false, nil
default:
return false, errors.Wrapf(ErrWrongType, "key %v is a %s (not a boolean)", combine(p.pathToHere, key), v.Type())
}
}
func (p parsedJSON) GetInt(key ...string) (int64, error) {
v := p.value.Get(key...)
if v == nil {
return 0, errors.Errorf("key %v does not exist", combine(p.pathToHere, key))
}
switch v.Type() {
case fastjson.TypeString:
i, err := strconv.ParseInt(string(v.String()), 10, 64)
if err != nil {
return 0, errors.Wrapf(ErrWrongType, "parse int '%s' at %v: %s", string(v.String()), combine(p.pathToHere, key), err)
}
return i, nil
case fastjson.TypeNumber:
return v.GetInt64(), nil
default:
return 0, errors.Wrapf(ErrWrongType, "key %v is a %s (not a number)", combine(p.pathToHere, key), v.Type())
}
}
func (p parsedJSON) GetUInt(key ...string) (uint64, error) {
v := p.value.Get(key...)
if v == nil {
return 0, errors.Errorf("key %v does not exist", combine(p.pathToHere, key))
}
switch v.Type() {
case fastjson.TypeString:
i, err := strconv.ParseUint(string(v.String()), 10, 64)
if err != nil {
return 0, errors.Wrapf(ErrWrongType, "parse int '%s' at %v: %s", string(v.String()), combine(p.pathToHere, key), err)
}
return i, nil
case fastjson.TypeNumber:
return v.GetUint64(), nil
default:
return 0, errors.Wrapf(ErrWrongType, "key %v is a %s (not a number)", combine(p.pathToHere, key), v.Type())
}
}
func (p parsedJSON) GetFloat(key ...string) (float64, error) {
v := p.value.Get(key...)
if v == nil {
return 0, errors.Wrapf(ErrDoesNotExist, "key %v does not exist", combine(p.pathToHere, key))
}
switch v.Type() {
case fastjson.TypeNumber:
return v.GetFloat64(), nil
default:
return 0, errors.Wrapf(ErrWrongType, "key %v is a %s (not a number)", combine(p.pathToHere, key), v.Type())
}
}
func (p parsedJSON) GetString(key ...string) (string, error) {
v := p.value.Get(key...)
if v == nil {
return "", errors.Wrapf(ErrDoesNotExist, "key %v does not exist", combine(p.pathToHere, key))
}
switch v.Type() {
case fastjson.TypeString:
return string(v.GetStringBytes()), nil
default:
return "", errors.Wrapf(ErrWrongType, "key %v is a %s (not a string)", combine(p.pathToHere, key), v.Type())
}
}
func (p parsedJSON) Keys(key ...string) ([]string, error) {
v := p.value.Get(key...)
if v == nil {
return nil, errors.Wrapf(ErrDoesNotExist, "key %v does not exist", combine(p.pathToHere, key))
}
switch v.Type() {
case fastjson.TypeNull:
return nil, nil
case fastjson.TypeObject:
o := v.GetObject()
if o == nil {
return nil, errors.New("internal error: expected a non-nil object")
}
keys := make([]string, 0, o.Len())
o.Visit(func(key []byte, _ *fastjson.Value) {
keys = append(keys, string(key))
})
return keys, nil
default:
return nil, errors.Wrapf(ErrWrongType, "key %v is a %s (not a string)", combine(p.pathToHere, key), v.Type())
}
}
func (p parsedJSON) Len(key ...string) (int, error) {
v := p.value.Get(key...)
if v == nil {
return 0, errors.Wrapf(ErrDoesNotExist, "key %v does not exist", combine(p.pathToHere, key))
}
switch v.Type() {
case fastjson.TypeNull:
return 0, nil
case fastjson.TypeArray:
a := v.GetArray()
return len(a), nil
default:
return 0, errors.Wrapf(ErrWrongType, "key %v is a %s (not a string)", combine(p.pathToHere, key), v.Type())
}
}
func (p parsedJSON) Type(key ...string) NodeType {
v := p.value.Get(key...)
if v == nil {
return Undefined
}
switch v.Type() {
case fastjson.TypeNull:
return Nil
case fastjson.TypeObject:
return Map
case fastjson.TypeArray:
return Slice
case fastjson.TypeString:
return String
case fastjson.TypeNumber:
if intRE.MatchString(v.String()) {
return Int
}
return Float
case fastjson.TypeTrue, fastjson.TypeFalse:
return Bool
default:
return Undefined
}
}