-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhandy.go
More file actions
190 lines (175 loc) · 4.98 KB
/
Copy pathhandy.go
File metadata and controls
190 lines (175 loc) · 4.98 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
package astjson
import (
"github.com/wundergraph/go-arena"
)
// GetString returns string value for the field identified by keys path
// in JSON data.
//
// Array indexes may be represented as decimal numbers in keys.
//
// An empty string is returned on error. Use Parser for proper error handling.
//
// Parser is faster for obtaining multiple fields from JSON.
func GetString(data []byte, keys ...string) string {
var p Parser
v, err := p.ParseBytes(data)
if err != nil {
return ""
}
sb := v.GetStringBytes(keys...)
str := string(sb)
return str
}
// GetBytes returns string value for the field identified by keys path
// in JSON data.
//
// Array indexes may be represented as decimal numbers in keys.
//
// nil is returned on error. Use Parser for proper error handling.
//
// Parser is faster for obtaining multiple fields from JSON.
func GetBytes(data []byte, keys ...string) []byte {
var p Parser
v, err := p.ParseBytes(data)
if err != nil {
return nil
}
sb := v.GetStringBytes(keys...)
// Make a copy of sb, since sb belongs to p.
var b []byte
if sb != nil {
b = append(b, sb...)
}
return b
}
// GetInt returns int value for the field identified by keys path
// in JSON data.
//
// Array indexes may be represented as decimal numbers in keys.
//
// 0 is returned on error. Use Parser for proper error handling.
//
// Parser is faster for obtaining multiple fields from JSON.
func GetInt(data []byte, keys ...string) int {
var p Parser
v, err := p.ParseBytes(data)
if err != nil {
return 0
}
n := v.GetInt(keys...)
return n
}
// GetFloat64 returns float64 value for the field identified by keys path
// in JSON data.
//
// Array indexes may be represented as decimal numbers in keys.
//
// 0 is returned on error. Use Parser for proper error handling.
//
// Parser is faster for obtaining multiple fields from JSON.
func GetFloat64(data []byte, keys ...string) float64 {
var p Parser
v, err := p.ParseBytes(data)
if err != nil {
return 0
}
f := v.GetFloat64(keys...)
return f
}
// GetBool returns boolean value for the field identified by keys path
// in JSON data.
//
// Array indexes may be represented as decimal numbers in keys.
//
// False is returned on error. Use Parser for proper error handling.
//
// Parser is faster for obtaining multiple fields from JSON.
func GetBool(data []byte, keys ...string) bool {
var p Parser
v, err := p.ParseBytes(data)
if err != nil {
return false
}
b := v.GetBool(keys...)
return b
}
// Exists returns true if the field identified by keys path exists in JSON data.
//
// Array indexes may be represented as decimal numbers in keys.
//
// False is returned on error. Use Parser for proper error handling.
//
// Parser is faster when multiple fields must be checked in the JSON.
func Exists(data []byte, keys ...string) bool {
var p Parser
v, err := p.ParseBytes(data)
if err != nil {
return false
}
ok := v.Exists(keys...)
return ok
}
// Parse parses json string s.
//
// The function is slower than the Parser.Parse for re-used Parser.
func Parse(s string) (*Value, error) {
var p Parser
return p.Parse(s)
}
// ParseWithArena parses json string s, allocating all values on the arena.
//
// The input string is copied onto the arena before parsing, so the caller may
// drop references to s immediately after this call returns. All parsed values
// live entirely in arena memory and are valid for the arena's lifetime.
//
// When a is nil, behaves identically to Parse (heap allocation).
//
// The function is slower than Parser.ParseWithArena for re-used Parser.
func ParseWithArena(a arena.Arena, s string) (*Value, error) {
var p Parser
return p.ParseWithArena(a, s)
}
// MustParse parses json string s.
//
// The function panics if s cannot be parsed.
// The function is slower than the Parser.Parse for re-used Parser.
func MustParse(s string) *Value {
v, err := Parse(s)
if err != nil {
panic(err)
}
return v
}
// ParseBytes parses b containing json.
//
// The function is slower than the Parser.ParseBytes for re-used Parser.
func ParseBytes(b []byte) (*Value, error) {
var p Parser
return p.ParseBytes(b)
}
// ParseBytesWithArena parses b containing json, allocating all values on the
// arena.
//
// The input bytes are copied onto the arena before parsing, so the caller may
// reuse or discard b immediately after this call returns. All parsed values
// live entirely in arena memory and are valid for the arena's lifetime.
//
// When a is nil, behaves identically to ParseBytes (heap allocation). In that
// case the caller must not modify b while the returned Value is in use.
//
// The function is slower than Parser.ParseBytesWithArena for re-used Parser.
func ParseBytesWithArena(a arena.Arena, b []byte) (*Value, error) {
var p Parser
return p.ParseBytesWithArena(a, b)
}
// MustParseBytes parses b containing json.
//
// The function panics if b cannot be parsed.
// The function is slower than the Parser.ParseBytes for re-used Parser.
func MustParseBytes(b []byte) *Value {
v, err := ParseBytes(b)
if err != nil {
panic(err)
}
return v
}