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
|
package router
import (
"strings"
gstrings "github.com/savsgio/gotils/strings"
)
// cleanPath removes the '.' if it is the last character of the route
func cleanPath(path string) string {
return strings.TrimSuffix(path, ".")
}
// getOptionalPaths returns all possible paths when the original path
// has optional arguments
func getOptionalPaths(path string) []string {
paths := make([]string, 0)
start := 0
walk:
for {
if start >= len(path) {
return paths
}
c := path[start]
start++
if c != '{' {
continue
}
newPath := ""
hasRegex := false
questionMarkIndex := -1
brackets := 0
for end, c := range []byte(path[start:]) {
switch c {
case '{':
brackets++
case '}':
if brackets > 0 {
brackets--
continue
} else if questionMarkIndex == -1 {
continue walk
}
end++
newPath += path[questionMarkIndex+1 : start+end]
path = path[:questionMarkIndex] + path[questionMarkIndex+1:] // remove '?'
paths = append(paths, newPath)
start += end - 1
continue walk
case ':':
hasRegex = true
case '?':
if hasRegex {
continue
}
questionMarkIndex = start + end
newPath += path[:questionMarkIndex]
if len(path[:start-2]) == 0 {
// include the root slash because the param is in the first segment
paths = append(paths, "/")
} else if !gstrings.Include(paths, path[:start-2]) {
// include the path without the wildcard
// -2 due to remove the '/' and '{'
paths = append(paths, path[:start-2])
}
}
}
}
}
|