-
Notifications
You must be signed in to change notification settings - Fork 16
/
extends.go
134 lines (108 loc) · 2.63 KB
/
extends.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
package rux
import (
"io"
"net/url"
"strings"
"github.com/gookit/goutil"
)
/*************************************************************
* Extends interfaces definition
*************************************************************/
// Renderer interface
type Renderer interface {
Render(io.Writer, string, any, *Context) error
}
// Validator interface
type Validator interface {
Validate(i any) error
}
/*************************************************************
* Quick build uri by route name
*************************************************************/
// BuildRequestURL struct
type BuildRequestURL struct {
queries url.Values
params M
path string
scheme string
host string
user *url.Userinfo
}
// NewBuildRequestURL get new obj
func NewBuildRequestURL() *BuildRequestURL {
return &BuildRequestURL{
queries: make(url.Values),
params: make(M),
}
}
// Queries set Queries
func (b *BuildRequestURL) Queries(queries url.Values) *BuildRequestURL {
b.queries = queries
return b
}
// Params set Params
func (b *BuildRequestURL) Params(params M) *BuildRequestURL {
b.params = params
return b
}
// Scheme set Scheme
func (b *BuildRequestURL) Scheme(scheme string) *BuildRequestURL {
b.scheme = scheme
return b
}
// User set User
func (b *BuildRequestURL) User(username, password string) *BuildRequestURL {
b.user = url.UserPassword(username, password)
return b
}
// Host set Host
func (b *BuildRequestURL) Host(host string) *BuildRequestURL {
b.host = host
return b
}
// Path set Path
func (b *BuildRequestURL) Path(path string) *BuildRequestURL {
b.path = path
return b
}
// Build url
func (b *BuildRequestURL) Build(withParams ...M) *url.URL {
var path = b.path
if len(withParams) > 0 {
for k, d := range withParams[0] {
if strings.IndexByte(k, '{') == -1 && strings.IndexByte(k, '}') == -1 {
b.queries.Add(k, goutil.String(d))
} else {
b.params[k] = goutil.String(d)
}
}
}
var u = new(url.URL)
u.Scheme = b.scheme
u.User = b.user
u.Host = b.host
u.Path = path
u.RawQuery = b.queries.Encode()
ss := varRegex.FindAllString(path, -1)
if len(ss) == 0 {
return u
}
var n string
var varParams = make(map[string]string)
// TODO should optimize ...
for _, str := range ss {
nvStr := str[1 : len(str)-1]
if strings.IndexByte(nvStr, ':') > 0 {
nv := strings.SplitN(nvStr, ":", 2)
n, _ = strings.TrimSpace(nv[0]), strings.TrimSpace(nv[1])
varParams[str] = "{" + n + "}"
} else {
varParams[str] = str
}
}
for paramRegex, name := range varParams {
path = strings.NewReplacer(paramRegex, goutil.String(b.params[name])).Replace(path)
}
u.Path = path
return u
}