-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpath.go
81 lines (73 loc) · 1.58 KB
/
path.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
package hime
import (
"fmt"
"net/url"
"path"
"strings"
)
// Param is the query param when redirect
type Param struct {
Name string
Value any
}
// SafeRedirectPath filters domain out from path
func SafeRedirectPath(p string) string {
l, err := url.ParseRequestURI(p)
if err != nil {
return "/"
}
r := l.EscapedPath()
if len(r) == 0 {
r = "/"
}
if l.ForceQuery || l.RawQuery != "" {
r += "?" + l.RawQuery
}
return path.Clean(r)
}
func mergeValues(s, p url.Values) {
for k, v := range p {
for _, vv := range v {
s[k] = append(s[k], vv)
}
}
}
func mergeValueWithMapString(s url.Values, m map[string]string) {
for k, v := range m {
s[k] = append(s[k], v)
}
}
func mergeValueWithMapAny(s url.Values, m map[string]any) {
for k, v := range m {
s[k] = append(s[k], fmt.Sprint(v))
}
}
func buildPath(base string, params ...any) string {
baseURL, err := url.Parse(base)
if err != nil {
panicf("parse url error; %v", err)
}
xs := make([]string, 0, len(params))
ps := make(url.Values)
mergeValues(ps, baseURL.Query())
for _, p := range params {
switch v := p.(type) {
case url.Values:
mergeValues(ps, v)
case map[string]string:
mergeValueWithMapString(ps, v)
case map[string]any:
mergeValueWithMapAny(ps, v)
case *Param:
ps[v.Name] = append(ps[v.Name], fmt.Sprint(v.Value))
default:
xs = append(xs, strings.TrimPrefix(fmt.Sprint(p), "/"))
}
}
if baseURL.Path == "" || (len(xs) > 0 && !strings.HasSuffix(baseURL.Path, "/")) {
baseURL.Path += "/"
}
baseURL.Path += path.Join(xs...)
baseURL.RawQuery = ps.Encode()
return baseURL.String()
}