-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathpath.go
More file actions
65 lines (53 loc) · 1.24 KB
/
path.go
File metadata and controls
65 lines (53 loc) · 1.24 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
package diff
import "strings"
/*
normalizeTemplatedPath converts a path to its normalized form, without parameter names
For example:
/person/{personName} -> /person/{}
Return values:
1. The normalized path
2. Number of params
3. List of param names
This implementation is based on Paths.normalizeTemplatedPath in openapi3
*/
func normalizeTemplatedPath(path string) (string, uint, []string) {
if strings.IndexByte(path, '{') < 0 {
return path, 0, nil
}
var buffTpl strings.Builder
buffTpl.Grow(len(path))
var (
cc rune
count uint
isVariable bool
vars = []string{}
buffVar strings.Builder
)
for i, c := range path {
if isVariable {
if c == '}' {
// End path variable
isVariable = false
vars = append(vars, buffVar.String())
buffVar = strings.Builder{}
// First append possible '*' before this character
// The character '}' will be appended
if i > 0 && cc == '*' {
buffTpl.WriteRune(cc)
}
} else {
buffVar.WriteRune(c)
continue
}
} else if c == '{' {
// Begin path variable
isVariable = true
// The character '{' will be appended
count++
}
// Append the character
buffTpl.WriteRune(c)
cc = c
}
return buffTpl.String(), count, vars
}