-
Notifications
You must be signed in to change notification settings - Fork 1
/
request.go
179 lines (150 loc) · 3.58 KB
/
request.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package dworkweb
import (
"fmt"
"io"
"net/http"
"github.com/Diegiwg/dwork-web/dw/router"
)
type ErrorParamNotFound struct {
Param string
}
func (err ErrorParamNotFound) Error() string {
return fmt.Sprint("The '", err.Param, "' param not found")
}
type ErrorParamNotIsT struct {
Param string
Type string
}
func (err ErrorParamNotIsT) Error() string {
return fmt.Sprint("The '", err.Param, "' param is not of type '", err.Type, "'")
}
type CtxRequest struct {
Raw *http.Request
Params CtxRequestParams
}
// Body returns the body data of the Request.
func (ctx *CtxRequest) Body() ([]byte, error) {
body, err := io.ReadAll(ctx.Raw.Body)
if err != nil {
return nil, err
}
return body, nil
}
type CtxRequestParams struct {
values *router.RouteParams
}
// String returns the value of a specified parameter as a string.
//
// Parameters:
//
// - param: the name of the parameter to retrieve.
//
// Return type:
//
// - string: the value of the parameter as a string.
//
// - error: an error of type ErrorParamNotFound if the parameter does not exist.
func (ctx *CtxRequestParams) String(param string) (string, error) {
// Check of the param exists
raw, ok := (*ctx.values)[param]
if !ok {
return "", ErrorParamNotFound{param}
}
return raw.(string), nil
}
// Int checks if the param exists and casts it to an int.
//
// Parameters:
//
// - param: the name of the parameter to retrieve.
//
// Return type:
//
// - int: the value of the parameter as an int.
//
// - error: an error of type ErrorParamNotFound if the parameter does not exist.
func (ctx *CtxRequestParams) Int(param string) (int, error) {
// Check of the param exists
raw, ok := (*ctx.values)[param]
if !ok {
return 0, ErrorParamNotFound{param}
}
// Cast to int
value, ok := raw.(int)
if !ok {
return 0, ErrorParamNotIsT{param, "int"}
}
return value, nil
}
// Float checks if the param exists and casts it to a float.
//
// Parameters:
//
// - param: the name of the parameter to retrieve.
//
// Return type:
//
// - float64: the value of the parameter as a float.
//
// - error: an error of type ErrorParamNotFound if the parameter does not exist.
func (ctx *CtxRequestParams) Float(param string) (float64, error) {
// Check of the param exists
raw, ok := (*ctx.values)[param]
if !ok {
return 0, ErrorParamNotFound{param}
}
// Cast to float
value, ok := raw.(float64)
if !ok {
return 0, ErrorParamNotIsT{param, "float"}
}
return value, nil
}
// Bool checks if the param exists and casts it to a bool.
//
// Parameters:
//
// - param: the name of the parameter to retrieve.
//
// Return type:
//
// - bool: the value of the parameter as a bool.
//
// - error: an error of type ErrorParamNotFound if the parameter does not exist.
func (ctx *CtxRequestParams) Bool(param string) (bool, error) {
// Check of the param exists
raw, ok := (*ctx.values)[param]
if !ok {
return false, ErrorParamNotFound{param}
}
// Cast to bool
value, ok := raw.(bool)
if !ok {
return false, ErrorParamNotIsT{param, "bool"}
}
return value, nil
}
// UUID checks if the param exists and casts it to a uuid.
//
// Parameters:
//
// - param: the name of the parameter to retrieve.
//
// Return type:
//
// - string: the value of the parameter as a uuid.
//
// - error: an error of type ErrorParamNotFound if the parameter does not exist.
func (ctx *CtxRequestParams) UUID(param string) (string, error) {
// Check of the param exists
raw, ok := (*ctx.values)[param]
if !ok {
return "", ErrorParamNotFound{param}
}
// Cast to uuid
value, ok := raw.(string)
if !ok {
return "", ErrorParamNotIsT{param, "uuid"}
}
return value, nil
}