forked from go-fuego/fuego
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparams.go
More file actions
47 lines (39 loc) · 1.04 KB
/
params.go
File metadata and controls
47 lines (39 loc) · 1.04 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
package fuego
import (
"regexp"
"strings"
)
var pathStdParamRegex = regexp.MustCompile(`{(.+?)}`)
// parsePathParams gives the list of path parameters in a path.
// Example : /item/{user}/{id} -> [user, id]
func parseStdPathParams(path string) []string {
matches := pathStdParamRegex.FindAllString(path, -1)
for i, match := range matches {
matches[i] = strings.Trim(match, "{}")
}
return matches
}
func parseGinPathParams(path string) []string {
params := []string{}
for {
idx := strings.IndexAny(path, "*:")
if idx == -1 {
break
}
var name string
name, path, _ = strings.Cut(path[idx:], "/")
params = append(params, name)
}
return params
}
func convertGinPathToStdPath(ginPath string) string {
segments := strings.Split(ginPath, "/")
for i, segment := range segments {
if strings.HasPrefix(segment, ":") {
segments[i] = "{" + strings.TrimPrefix(segment, ":") + "}"
} else if strings.HasPrefix(segment, "*") {
segments[i] = "{" + strings.TrimPrefix(segment, "*") + "...}"
}
}
return strings.Join(segments, "/")
}