-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute-utils.go
More file actions
33 lines (30 loc) · 884 Bytes
/
route-utils.go
File metadata and controls
33 lines (30 loc) · 884 Bytes
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
package wstf
import (
"regexp"
"strings"
)
// Find all the parameters in user defined pattern.
func findParamNames(pattern string) []string {
var paramNames []string
parameters := patternRegexp.FindAllStringSubmatch(pattern, -1)
for _, parameter := range parameters {
name := parameter[1]
paramNames = append(paramNames, name)
}
return paramNames
}
// Set up pattern for regexp.
func generatePatternForRegexp(pattern string) (string, error) {
pattern = patternRegexp.ReplaceAllStringFunc(pattern, func(subMatch string) string {
// The sub match string conforms the parameter pattern: `{parameter-name:regexp-expression}`.
foos := strings.SplitN(subMatch, ":", 2)
if len(foos) < 2 {
return `([^/]+)`
} else {
return "(" + foos[1][0:len(foos[1])-1] + ")"
}
})
// Checking for abnormal patterns.
_, err := regexp.Compile(pattern)
return pattern, err
}