-
Notifications
You must be signed in to change notification settings - Fork 230
/
paths.go
467 lines (422 loc) · 10.8 KB
/
paths.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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// Package resourcepaths parses API and other resource paths.
package resourcepaths
import (
"fmt"
"go/ast"
"go/token"
"net/url"
"strings"
meta "encr.dev/proto/encore/parser/meta/v1"
"encr.dev/v2/internals/perr"
"encr.dev/v2/internals/schema"
)
// Path represents a parsed path.
type Path struct {
StartPos token.Pos
Segments []Segment
}
var _ ast.Node = (*Path)(nil)
func (p *Path) Pos() token.Pos {
return p.StartPos
}
func (p *Path) End() token.Pos {
if len(p.Segments) > 0 {
return p.Segments[len(p.Segments)-1].End()
}
return p.StartPos
}
// String returns the path's string representation.
func (p *Path) String() string {
var b strings.Builder
for _, s := range p.Segments {
b.WriteByte('/')
switch s.Type {
case Param:
b.WriteByte(':')
case Wildcard:
b.WriteByte('*')
case Fallback:
b.WriteByte('!')
}
b.WriteString(s.Value)
}
return b.String()
}
// NumParams reports the number of parameterized (non-literal) segments in the path.
func (p *Path) NumParams() int {
n := 0
for _, s := range p.Segments {
if s.Type != Literal {
n++
}
}
return n
}
// Params returns the segments that are not literals.
func (p *Path) Params() []Segment {
var params []Segment
for _, s := range p.Segments {
if s.Type != Literal {
params = append(params, s)
}
}
return params
}
// HasFallback is true if the path contains a fallback segment.
func (p *Path) HasFallback() bool {
for _, s := range p.Segments {
if s.Type == Fallback {
return true
}
}
return false
}
// Segment represents a parsed path segment.
type Segment struct {
Type SegmentType
Value string // literal if Type == Literal; name of parameter otherwise
ValueType schema.BuiltinKind
StartPos token.Pos
EndPos token.Pos
}
var _ ast.Node = Segment{}
func (s *Segment) String() string {
switch s.Type {
case Param:
return ":" + s.Value
case Wildcard:
return "*" + s.Value
case Fallback:
return "!" + s.Value
default:
return s.Value
}
}
func (s Segment) Pos() token.Pos {
return s.StartPos
}
func (s Segment) End() token.Pos {
return s.EndPos
}
// SegmentType represents the different types of path segments recognized by the parser.
type SegmentType int
const (
// Literal is a literal string path segment.
Literal SegmentType = iota
// Param represents a single path segment of any (non-empty) value.
Param
// Wildcard represents zero or more path segments of any value.
Wildcard
// Fallback represents zero or more path segments of any value
// that are lower priority than any other path.
Fallback
)
type Options struct {
// AllowWildcard indicates whether the parser should allow wildcard segments.
AllowWildcard bool
// AllowFallback indicates whether the parser should allow fallback segments.
AllowFallback bool
// PrefixSlash indicates whether the parser should require a leading slash
// or require that it's not present
PrefixSlash bool
}
// Parse parses a slash-separated path into path segments.
//
// strPos is the position of where the path string was found in the source code.
func Parse(errs *perr.List, startPos token.Pos, path string, options Options) (parsedPath *Path, ok bool) {
endPos := token.Pos(len([]byte(path))) + startPos
if path == "" {
errs.Add(errEmptyPath.AtGoPos(startPos, endPos))
return nil, false
} else if path[0] != '/' && options.PrefixSlash {
errs.Add(errInvalidPathMissingPrefix.AtGoPos(startPos, endPos))
return nil, false
} else if path[0] == '/' && !options.PrefixSlash {
errs.Add(errInvalidPathPrefix.AtGoPos(startPos, endPos))
return nil, false
}
urlPath := path
if !options.PrefixSlash {
urlPath = "/" + urlPath
}
if _, err := url.ParseRequestURI(urlPath); err != nil {
errs.Add(errInvalidPathURI.AtGoPos(startPos, endPos).Wrapping(err))
return nil, false
} else if idx := strings.IndexByte(path, '?'); idx != -1 {
errs.Add(errPathContainedQuery.AtGoPos(startPos, endPos))
return nil, false
}
var segs []Segment
segStart := startPos
for path != "" {
if options.PrefixSlash || len(segs) > 0 {
path = path[1:] // drop leading '/'
segStart++
}
// Find the next path segment
var val string
idx := strings.IndexByte(path, '/')
segEnd := segStart
switch idx {
case 0:
errs.Add(errEmptySegment.AtGoPos(segStart-1, segStart))
return nil, false
case -1:
val = path
path = ""
default:
val = path[:idx]
path = path[idx:]
}
segEnd += token.Pos(len([]byte(val))) - 1
segType := Literal
if val != "" && val[0] == ':' {
segType = Param
val = val[1:]
} else if val != "" && val[0] == '*' && options.AllowWildcard {
segType = Wildcard
val = val[1:]
} else if val != "" && val[0] == '!' && options.AllowFallback {
segType = Fallback
val = val[1:]
}
segs = append(segs, Segment{
Type: segType, Value: val, ValueType: schema.String,
StartPos: segStart - 1, EndPos: segEnd,
})
segStart = segEnd + 1
}
// Validate the segments
for i, s := range segs {
switch s.Type {
case Literal:
if s.Value == "" {
errs.Add(errTrailingSlash.AtGoNode(s))
return nil, false
}
case Param:
switch {
case s.Value == "":
errs.Add(errParameterMissingName.AtGoNode(s))
return nil, false
case !token.IsIdentifier(s.Value):
errs.Add(errInvalidParamIdentifier.AtGoNode(s))
return nil, false
}
case Wildcard:
switch {
case s.Value == "":
errs.Add(errParameterMissingName.AtGoNode(s))
return nil, false
case !token.IsIdentifier(s.Value):
errs.Add(errInvalidParamIdentifier.AtGoNode(s))
return nil, false
case len(segs) > (i + 1):
errs.Add(errWildcardNotLastSegment.AtGoNode(s))
return nil, false
}
case Fallback:
switch {
case s.Value == "":
errs.Add(errParameterMissingName.AtGoNode(s))
return nil, false
case !token.IsIdentifier(s.Value):
errs.Add(errInvalidParamIdentifier.AtGoNode(s))
return nil, false
case len(segs) > (i + 1):
errs.Add(errFallbackNotLastSegment.AtGoNode(s))
return nil, false
}
}
}
return &Path{StartPos: startPos, Segments: segs}, true
}
func (p *Path) ToProto() *meta.Path {
mp := &meta.Path{}
mp.Type = meta.Path_URL
for _, seg := range p.Segments {
s := &meta.PathSegment{Value: seg.Value}
switch seg.Type {
case Literal:
s.Type = meta.PathSegment_LITERAL
case Param:
s.Type = meta.PathSegment_PARAM
case Wildcard:
s.Type = meta.PathSegment_WILDCARD
case Fallback:
s.Type = meta.PathSegment_FALLBACK
default:
panic(fmt.Sprintf("unhandled path segment type %v", seg.Type))
}
if s.Type != meta.PathSegment_LITERAL {
switch seg.ValueType {
case schema.String:
s.ValueType = meta.PathSegment_STRING
case schema.Bool:
s.ValueType = meta.PathSegment_BOOL
case schema.Int:
s.ValueType = meta.PathSegment_INT
case schema.Int8:
s.ValueType = meta.PathSegment_INT8
case schema.Int16:
s.ValueType = meta.PathSegment_INT16
case schema.Int32:
s.ValueType = meta.PathSegment_INT32
case schema.Int64:
s.ValueType = meta.PathSegment_INT64
case schema.Uint:
s.ValueType = meta.PathSegment_UINT
case schema.Uint8:
s.ValueType = meta.PathSegment_UINT8
case schema.Uint16:
s.ValueType = meta.PathSegment_UINT16
case schema.Uint32:
s.ValueType = meta.PathSegment_UINT32
case schema.Uint64:
s.ValueType = meta.PathSegment_UINT64
case schema.UUID:
s.ValueType = meta.PathSegment_UUID
default:
panic(fmt.Sprintf("unhandled path segment value type %v", seg.ValueType))
}
}
mp.Segments = append(mp.Segments, s)
}
return mp
}
// Set tracks a set of paths, ensuring they are compatible with each other.
// The zero value is ready to use.
type Set struct {
methods map[string]*node
}
func NewSet() *Set {
return &Set{
methods: make(map[string]*node),
}
}
// Add adds a path to the set of paths.
func (s *Set) Add(errs *perr.List, method string, path *Path) (ok bool) {
if s.methods == nil {
s.methods = make(map[string]*node)
}
var candidates []string
if method == "*" {
// Check all defined methods
for m := range s.methods {
if m != method {
candidates = append(candidates, m)
}
}
} else {
candidates = []string{"*"}
}
// Always check the target method last, so we don't add to the set if there's an error
// for another method.
candidates = append(candidates, method)
CandidateLoop:
for _, m := range candidates {
curr := s.methods[m]
if curr == nil {
curr = &node{}
s.methods[m] = curr
}
for _, seg := range path.Segments {
next, ok := s.match(errs, path, seg, curr)
if !ok {
return false
} else if next != nil {
curr = next
} else {
// Could not find a match; add it to the tree (if this is the target method)
if m != method {
continue CandidateLoop
}
curr.children = append(curr.children, &node{s: seg})
curr = curr.children[len(curr.children)-1]
}
}
if curr.p != nil {
errs.Add(errDuplicatePath.AtGoNode(path).AtGoNode(curr.p))
return false
} else if m == method {
curr.p = path
}
}
return true
}
func (s *Set) match(errs *perr.List, path *Path, seg Segment, curr *node) (next *node, ok bool) {
for _, ch := range curr.children {
switch ch.s.Type {
case Wildcard:
switch seg.Type {
case Param:
errs.Add(errConflictingParameterizedPath(seg.Value, ch.findPath()).
AtGoNode(path).
AtGoNode(ch.findPath()))
return nil, false
case Wildcard:
errs.Add(errConflictingWildcardPath(seg.Value, ch.findPath()).
AtGoNode(path).
AtGoNode(ch.findPath()))
return nil, false
case Literal:
errs.Add(errConflictingLiteralPath(seg.Value, ch.findPath()).
AtGoNode(path).
AtGoNode(ch.findPath()))
return nil, false
}
case Param:
switch seg.Type {
case Param:
return ch, true
case Wildcard:
errs.Add(errConflictingWildcardPath(seg.Value, ch.findPath()).
AtGoNode(path).
AtGoNode(ch.findPath()))
return nil, false
case Literal:
errs.Add(errConflictingLiteralPath(seg.Value, ch.findPath()).
AtGoNode(path).
AtGoNode(ch.findPath()))
return nil, false
}
case Literal:
switch seg.Type {
case Wildcard:
errs.Add(errConflictingWildcardPath(seg.Value, ch.findPath()).
AtGoNode(path).
AtGoNode(ch.findPath()))
return nil, false
case Param:
errs.Add(errConflictingLiteralPath(seg.Value, ch.findPath()).
AtGoNode(path).
AtGoNode(ch.findPath()))
return nil, false
case Literal:
if seg.Value == ch.s.Value {
return ch, true
}
}
case Fallback:
switch seg.Type {
case Fallback:
errs.Add(errConflictingFallbackPath(seg.Value, ch.findPath()).
AtGoNode(path).
AtGoNode(ch.findPath()))
return nil, false
}
}
}
return nil, true
}
type node struct {
s Segment
children []*node
p *Path // leaf path, if any
}
func (n *node) findPath() *Path {
for n.p == nil {
n = n.children[0]
}
return n.p
}