-
Notifications
You must be signed in to change notification settings - Fork 0
/
queryparser.go
140 lines (100 loc) · 3.12 KB
/
queryparser.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package queryparser
import (
"fmt"
"strconv"
"strings"
"github.com/macinnir/dvc/core/lib"
)
func ExtractParamFromString(paramString string) (param lib.ControllerRouteParam) {
// Incase there are parts after the param, split on the closing bracket
pParts := strings.Split(paramString, "}")
paramString = pParts[0]
paramParts := strings.Split(paramString, ":")
param = lib.ControllerRouteParam{
Name: paramParts[0],
Pattern: paramParts[1],
}
param.Type = matchPatternToDataType(param.Pattern)
return
}
func matchPatternToDataType(pattern string) string {
if pattern == "[0-9]" || pattern == "[0-9]+" || pattern == "-?[0-9]+" {
return "int64"
}
return "string"
}
func ExtractQueryStringsFromRoutePath(routePath string) (string, []string) {
if !strings.Contains(routePath, "?") {
return routePath, []string{}
}
subParts := strings.SplitN(routePath, "?", 2)
return subParts[0], strings.Split(subParts[1], "&")
}
func ExtractQueriesFromRoutePath(routePath string) (string, []lib.ControllerRouteQuery) {
queries := []lib.ControllerRouteQuery{}
path, queryStrings := ExtractQueryStringsFromRoutePath(routePath)
for _, query := range queryStrings {
if !strings.Contains(query, "=") {
continue
}
o := ParseURLQuerySegment(query)
queries = append(queries, o)
}
return path, queries
}
// ParseURLQuerySegment parses a query segment in a URL query to a lib.ControllerRouteQueryObject
// e.g. foo={foo:[0-9]+}
func ParseURLQuerySegment(pattern string) lib.ControllerRouteQuery {
queryParts := strings.Split(pattern, "=")
o := lib.ControllerRouteQuery{
Name: queryParts[0],
ValueRaw: queryParts[1],
}
if strings.Contains(o.ValueRaw, ":") {
queryValueParts := strings.Split(o.ValueRaw, ":")
// Remove the starting "{"
o.VariableName = queryValueParts[0][1:]
// Remove the ending "}"
o.Pattern = strings.Join(queryValueParts[1:], ":")
o.Pattern = o.Pattern[0 : len(o.Pattern)-1]
// Check if the value isn't a constant value
o.Type = matchPatternToDataType(o.Pattern)
} else {
// Try to parse the value as an int64
// e.g. param=123
o.VariableName = o.Name
if _, e := strconv.ParseInt(o.ValueRaw, 10, 64); e != nil {
o.Type = "string"
} else {
o.Type = "int64"
}
}
return o
}
func ParseRouteString(route *lib.ControllerRoute, routeString string) error {
lineParts := strings.SplitN(routeString, " ", 4)
if len(lineParts) < 4 {
return fmt.Errorf("invalid route comment `%s`", routeString)
}
route.Method = lineParts[2]
route.Raw = lineParts[3]
route.Path, route.Queries = ExtractQueriesFromRoutePath(route.Raw)
params, _ := ExtractParamsFromRoutePath(route.Path)
route.Params = append(route.Params, params...)
return nil
}
func ExtractParamsFromRoutePath(routePath string) (params []lib.ControllerRouteParam, e error) {
params = []lib.ControllerRouteParam{}
// Params
if strings.Contains(routePath, "{") {
routeParts := strings.Split(routePath, "{")
for _, p := range routeParts[1:] {
if !strings.Contains(p, "}") || !strings.Contains(p, ":") {
continue
}
param := ExtractParamFromString(p)
params = append(params, param)
}
}
return
}