-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathparth.go
More file actions
362 lines (293 loc) · 8 KB
/
parth.go
File metadata and controls
362 lines (293 loc) · 8 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// Package parth provides path parsing for segment slicing and unmarshaling. In
// other words, parth provides simple and flexible access to (URL) path
// parameters.
//
// Valid values are:
// - builtin: *string, *bool, *int, *int64, *int32, *int16, *int8, *uint,
// *uint64, *uint32, *uint16, *uint8, *float64, *float32
// - stdlib: [*time.Duration], [encoding.TextUnmarshaler], [flag.Value]
//
// When handling any size of int, uint, or float, the first valid value within
// the specified segment will be used. Three important terms used in this
// package are "segment", "sequent", and "span". A segment is any single path
// section. A sequent is a segment that follows a "key" path section. Segments
// are able to be unmarshaled into variables. A span is any multiple path
// sections, and is handled as a string.
package parth
import (
"encoding"
"errors"
"flag"
"time"
)
// Err{Name} values facilitate error identification.
var (
ErrUnknownType = errors.New("unknown type provided")
ErrFirstSegNotFound = errors.New("first segment not found by index")
ErrLastSegNotFound = errors.New("last segment not found by index")
ErrSegOrderReversed = errors.New("first segment must precede last segment")
ErrKeySegNotFound = errors.New("segment not found by key")
ErrDataUnparsable = errors.New("data cannot be parsed")
)
// Segment locates the path segment indicated by index i. If the index is
// negative, the negative count begins with the last segment.
func Segment(v any, path string, i int) error {
var err error
switch v := v.(type) {
case *bool:
*v, err = segmentToBool(path, i)
case *float32:
var f float64
f, err = segmentToFloatN(path, i, 32)
*v = float32(f)
case *float64:
*v, err = segmentToFloatN(path, i, 64)
case *int:
var n int64
n, err = segmentToIntN(path, i, 0)
*v = int(n)
case *int16:
var n int64
n, err = segmentToIntN(path, i, 16)
*v = int16(n)
case *int32:
var n int64
n, err = segmentToIntN(path, i, 32)
*v = int32(n)
case *int64:
*v, err = segmentToIntN(path, i, 64)
case *int8:
var n int64
n, err = segmentToIntN(path, i, 8)
*v = int8(n)
case *string:
*v, err = segmentToString(path, i)
case *uint:
var n uint64
n, err = segmentToUintN(path, i, 0)
*v = uint(n)
case *uint16:
var n uint64
n, err = segmentToUintN(path, i, 16)
*v = uint16(n)
case *uint32:
var n uint64
n, err = segmentToUintN(path, i, 32)
*v = uint32(n)
case *uint64:
*v, err = segmentToUintN(path, i, 64)
case *uint8:
var n uint64
n, err = segmentToUintN(path, i, 8)
*v = uint8(n)
case *time.Duration:
var s string
s, err = segmentToString(path, i)
if err == nil {
d, err := time.ParseDuration(s)
if err == nil {
*v = d
}
}
case encoding.TextUnmarshaler:
var s string
s, err = segmentToString(path, i)
if err == nil {
err = v.UnmarshalText([]byte(s))
}
case flag.Value:
var s string
s, err = segmentToString(path, i)
if err == nil {
err = v.Set(s)
}
default:
err = ErrUnknownType
}
return err
}
// Sequent is similar to [Segment], except that it locates the segment that is
// subsequent to the "key" segment.
func Sequent(v any, path, key string) error {
return SubSeg(v, path, key, 0)
}
// Span returns the path segments between indexes i and j, including the segment
// indicated by index i. If an index is negative, the negative count begins with
// the last segment. Providing a 0 for index j is a special case which acts as
// an alias for the end of the path. If the first segment does not begin with a
// slash and it is part of the requested span, no slash will be added. Index i
// must not precede index j.
func Span(path string, i, j int) (string, error) {
var f, l int
var ok bool
if i < 0 {
f, ok = segStartIndexFromEnd(path, i)
} else {
f, ok = segStartIndexFromStart(path, i)
}
if !ok {
return "", ErrFirstSegNotFound
}
if j > 0 {
l, ok = segEndIndexFromStart(path, j)
} else {
l, ok = segEndIndexFromEnd(path, j)
}
if !ok {
return "", ErrLastSegNotFound
}
if f == l {
return "", nil
}
if f > l {
return "", ErrSegOrderReversed
}
return path[f:l], nil
}
// SubSeg is similar to both [Sequent] and [Segment]. It first locates the
// "key", then uses index i to locate a segment. For example, to access the
// segment immediately after the "key", an index of 0 should be provided (which
// is how [Sequent] is implemented). Technically, a negative index is valid,
// but it is nonsensical in this function.
func SubSeg(v any, path, key string, i int) error {
var err error
switch v := v.(type) {
case *bool:
*v, err = subSegToBool(path, key, i)
case *float32:
var f float64
f, err = subSegToFloatN(path, key, i, 32)
*v = float32(f)
case *float64:
*v, err = subSegToFloatN(path, key, i, 64)
case *int:
var n int64
n, err = subSegToIntN(path, key, i, 0)
*v = int(n)
case *int16:
var n int64
n, err = subSegToIntN(path, key, i, 16)
*v = int16(n)
case *int32:
var n int64
n, err = subSegToIntN(path, key, i, 32)
*v = int32(n)
case *int64:
*v, err = subSegToIntN(path, key, i, 64)
case *int8:
var n int64
n, err = subSegToIntN(path, key, i, 8)
*v = int8(n)
case *string:
*v, err = subSegToString(path, key, i)
case *uint:
var n uint64
n, err = subSegToUintN(path, key, i, 0)
*v = uint(n)
case *uint16:
var n uint64
n, err = subSegToUintN(path, key, i, 16)
*v = uint16(n)
case *uint32:
var n uint64
n, err = subSegToUintN(path, key, i, 32)
*v = uint32(n)
case *uint64:
*v, err = subSegToUintN(path, key, i, 64)
case *uint8:
var n uint64
n, err = subSegToUintN(path, key, i, 8)
*v = uint8(n)
case encoding.TextUnmarshaler:
var s string
s, err = subSegToString(path, key, i)
if err == nil {
err = v.UnmarshalText([]byte(s))
}
default:
err = ErrUnknownType
}
return err
}
// SubSpan is similar to [Span], but only handles the portion of the path
// subsequent to the "key".
func SubSpan(path, key string, i, j int) (string, error) {
si, ok := segIndexByKey(path, key)
if !ok {
return "", ErrKeySegNotFound
}
if i >= 0 {
i++
}
if j > 0 {
j++
}
s, err := Span(path[si:], i, j)
if err != nil {
return "", err
}
return s, nil
}
// Parth manages path and error data for processing a single path multiple
// times while handling errors only once. Only the first encountered error is
// stored since all subsequent calls to Parth methods will have no effect.
type Parth struct {
path string
err error
}
// New constructs a pointer to an instance of [Parth] around the provided path.
func New(path string) *Parth {
return &Parth{path: path}
}
// NewBySpan constructs a pointer to an instance of [Parth] after preprocessing
// the provided path with [Span].
func NewBySpan(path string, i, j int) *Parth {
s, err := Span(path, i, j)
return &Parth{s, err}
}
// NewBySubSpan constructs a pointer to an instance of [Parth] after
// preprocessing the provided path with [SubSpan].
func NewBySubSpan(path, key string, i, j int) *Parth {
s, err := SubSpan(path, key, i, j)
return &Parth{s, err}
}
// Err returns the first error encountered by the [*Parth] instance.
func (p *Parth) Err() error {
return p.err
}
// Segment operates the same as the package-level function [Segment].
func (p *Parth) Segment(v any, i int) {
if p.err != nil {
return
}
p.err = Segment(v, p.path, i)
}
// Sequent operates the same as the package-level function [Sequent].
func (p *Parth) Sequent(v any, key string) {
p.SubSeg(v, key, 0)
}
// Span operates the same as the package-level function [Span].
func (p *Parth) Span(i, j int) string {
if p.err != nil {
return ""
}
s, err := Span(p.path, i, j)
p.err = err
return s
}
// SubSeg operates the same as the package-level function [SubSeg].
func (p *Parth) SubSeg(v any, key string, i int) {
if p.err != nil {
return
}
p.err = SubSeg(v, p.path, key, i)
}
// SubSpan operates the same as the package-level function [SubSpan].
func (p *Parth) SubSpan(key string, i, j int) string {
if p.err != nil {
return ""
}
s, err := SubSpan(p.path, key, i, j)
p.err = err
return s
}