/
selector.go
106 lines (85 loc) · 1.98 KB
/
selector.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
package silverhttp
import (
"net/http"
"github.com/clubpay/ronykit/kit"
)
// Selector implements kit.RouteSelector and
// also kit.RPCRouteSelector and kit.RESTRouteSelector
type Selector struct {
Method string
Path string
Predicate string
Decoder DecoderFunc
Encoding kit.Encoding
}
var (
_ kit.RouteSelector = (*Selector)(nil)
_ kit.RESTRouteSelector = (*Selector)(nil)
_ kit.RPCRouteSelector = (*Selector)(nil)
)
// REST returns a Selector which acts on http requests.
func REST(method, path string) Selector {
return Selector{
Method: method,
Path: path,
}
}
// POST a shortcut for REST(http.MethodPost, path)
func POST(path string) Selector {
return REST(http.MethodPost, path)
}
// GET a shortcut for REST(http.MethodGet, path)
func GET(path string) Selector {
return REST(http.MethodGet, path)
}
// PATCH a shortcut for REST(http.MethodPatch, path)
func PATCH(path string) Selector {
return REST(http.MethodPatch, path)
}
// PUT a shortcut for REST(http.MethodPut, path)
func PUT(path string) Selector {
return REST(http.MethodPut, path)
}
// DELETE a shortcut for REST(http.MethodDelete, path)
func DELETE(path string) Selector {
return REST(http.MethodDelete, path)
}
// RPC returns a Selector which acts on websocket requests
func RPC(predicate string) Selector {
return Selector{
Predicate: predicate,
}
}
func (r Selector) GetEncoding() kit.Encoding {
return r.Encoding
}
func (r Selector) SetEncoding(enc kit.Encoding) Selector {
r.Encoding = enc
return r
}
func (r Selector) SetDecoder(f DecoderFunc) Selector {
r.Decoder = f
return r
}
func (r Selector) GetMethod() string {
return r.Method
}
func (r Selector) GetPath() string {
return r.Path
}
func (r Selector) GetPredicate() string {
return r.Predicate
}
func (r Selector) Query(q string) interface{} {
switch q {
case queryDecoder:
return r.Decoder
case queryMethod:
return r.Method
case queryPath:
return r.Path
case queryPredicate:
return r.Predicate
}
return nil
}