-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
62 lines (49 loc) · 1.36 KB
/
parser.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
package jrouter
import (
"regexp"
"strings"
)
const (
//finderMatchPattern regexp to support paramters /something/{id}
finderMatchPattern string = "(\\{[a-zA-Z\\d+:\\(\\)\\,]+\\})"
//defaultParamMatcher regexp per parameter
defaultParamMatcher string = "([a-zA-Z\\d+]+)"
)
//URLParser is the struct responsible to manage the url and how to parse it
type URLParser struct {
Base string
Pattern string
PatternMatcher *regexp.Regexp
Params []Parameter
}
//Parameter is a struct for each paramater to be read
type Parameter struct {
Matcher string
Param string
}
//NewURLParser Instance new struct
func NewURLParser() *URLParser {
return &URLParser{}
}
//Analyze methos allow us analyze the url
func (p *URLParser) Analyze(url string) *URLParser {
re := regexp.MustCompile(finderMatchPattern)
params := re.FindAllString(url, -1)
if params == nil {
p.PatternMatcher = regexp.MustCompile(url)
p.Base = url
} else {
var matchers []string
for _, param := range params {
paramStr := param[1 : len(param)-1]
pa := Parameter{Param: paramStr, Matcher: defaultParamMatcher}
p.Params = append(p.Params, pa)
matchers = append(matchers, pa.Matcher)
}
urlParts := strings.Split(url, "{")
p.Base = urlParts[0]
urlPattern := p.Base + strings.Join(matchers, "/")
p.PatternMatcher = regexp.MustCompile(urlPattern)
}
return p
}