forked from s-mang/test2doc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl.go
65 lines (47 loc) · 1.15 KB
/
url.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
package doc
import (
"log"
"net/http"
"net/url"
"strings"
"github.com/adams-sarah/test2doc/doc/parse"
)
type URL struct {
rawURL *url.URL
ParameterizedPath string
Parameters []Parameter
}
func NewURL(req *http.Request, fn parse.URLVarExtractor) *URL {
u := &URL{
rawURL: req.URL,
}
u.ParameterizedPath, u.Parameters = paramPath(req, fn)
return u
}
func paramPath(req *http.Request, fn parse.URLVarExtractor) (string, []Parameter) {
uri, err := url.QueryUnescape(req.URL.Path)
if err != nil {
// fall back to unescaped uri
uri = req.URL.Path
}
vars := fn(req)
params := []Parameter{}
for k, v := range vars {
uri = strings.Replace(uri, "/"+v, "/{"+k+"}", 1)
params = append(params, MakeParameter(k, v))
}
var queryKeys []string
queryParams := req.URL.Query()
log.Println("Query Params: ", queryParams)
for k, vs := range queryParams {
queryKeys = append(queryKeys, k)
// just take first value
params = append(params, MakeParameter(k, vs[0]))
}
var queryKeysStr string
if len(queryKeys) > 0 {
queryKeysStr = "{?" + strings.Join(queryKeys, ",") + "}"
}
uri = uri + queryKeysStr
return uri, params
}