-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.go
302 lines (282 loc) · 8.41 KB
/
content.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Copyright 2021 - 2023 PurpleSec Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package routex
import "encoding/base64"
// Content is an alias of a JSON data payload sent to the server.
type Content map[string]any
const (
// ErrNoBody is an error returned when there is no content passed to an HTTP
// request when it's required.
ErrNoBody = errStr("missing HTTP body")
// ErrNotExists is an error returned from any of the Content getter functions
// when the value by the supplied name does not exist in the Content map.
ErrNotExists = errStr("value does not exist")
// ErrInvalidType is an error returned from any of the Content getter functions
// when the value by the supplied name is not the requested value.
ErrInvalidType = errStr("incorrect value type")
)
// Raw returns the raw interface value with the supplied value name.
//
// This function returns nil if the name does not exist. This is similar to
// directly calling the name in a map.
func (c Content) Raw(s string) any {
return c[s]
}
// Bool attempts to return the value with the provided name as a boolean value.
//
// This function will return an 'ErrNotExists' error if the value by the specified
// name does not exist or 'ErrInvalidType' if the value does not represent a boolean
// type.
func (c Content) Bool(s string) (bool, error) {
v, ok := c[s]
if !ok {
return false, &errValue{s: s, e: ErrNotExists}
}
r, ok := v.(bool)
if !ok {
return false, &errValue{s: s, e: ErrInvalidType}
}
return r, nil
}
// Int attempts to return the value with the provided name as an integer value.
//
// This function will return an 'ErrNotExists' error if the value by the specified
// name does not exist or 'ErrInvalidType' if the value does not represent an integer
// type.
func (c Content) Int(s string) (int64, error) {
r, err := c.Float(s)
if err != nil {
return 0, err
}
return int64(r), nil
}
// Uint attempts to return the value with the provided name as an unsigned integer
// value.
//
// This function will return an 'ErrNotExists' error if the value by the specified
// name does not exist or 'ErrInvalidType' if the value does not represent an integer
// type.
func (c Content) Uint(s string) (uint64, error) {
r, err := c.Float(s)
if err != nil {
return 0, err
}
return uint64(r), nil
}
// Bytes attempts to return the value with the provided name as a byte slice value
// that is represented by a Base64-encoded string.
//
// This function will return an 'ErrNotExists' error if the value by the specified
// name does not exist or 'ErrInvalidType' if the value does not represent a bytes
// type.
//
// This will attempt to decode the Base64 string and will return the encoding
// errors if they occur.
func (c Content) Bytes(s string) ([]byte, error) {
v, ok := c[s]
if !ok {
return nil, &errValue{s: s, e: ErrNotExists}
}
r, ok := v.(string)
if !ok {
return nil, &errValue{s: s, e: ErrInvalidType}
}
return base64.StdEncoding.DecodeString(r)
}
// String attempts to return the value with the provided name as a string value.
//
// This function will return an 'ErrNotExists' error if the value by the specified
// name does not exist or 'ErrInvalidType' if the value does not represent a string
// type.
func (c Content) String(s string) (string, error) {
v, ok := c[s]
if !ok {
return "", &errValue{s: s, e: ErrNotExists}
}
r, ok := v.(string)
if !ok {
return "", &errValue{s: s, e: ErrInvalidType}
}
return r, nil
}
// Float attempts to return the value with the provided name as a floating point
// value.
//
// This function will return an 'ErrNotExists' error if the value by the specified
// name does not exist or 'ErrInvalidType' if the value does not represent a float
// type.
func (c Content) Float(s string) (float64, error) {
v, ok := c[s]
if !ok {
return 0, &errValue{s: s, e: ErrNotExists}
}
r, ok := v.(float64)
if !ok {
return 0, &errValue{s: s, e: ErrInvalidType}
}
return r, nil
}
// StringDefault attempts to return the value with the provided name as a string
// value.
//
// This function will return the default value specified if the value does not exist
// or is not a string type.
func (c Content) StringDefault(s, d string) string {
v, ok := c[s]
if !ok {
return d
}
r, ok := v.(string)
if !ok {
return d
}
return r
}
// Object attempts to return the value with the provided name as a complex object
// value (wrapped as a Content alias).
//
// This function will return an 'ErrNotExists' error if the value by the specified
// name does not exist or 'ErrInvalidType' if the value does not represent an object
// type.
func (c Content) Object(s string) (Content, error) {
v, ok := c[s]
if !ok {
return nil, &errValue{s: s, e: ErrNotExists}
}
r, ok := v.(map[string]any)
if !ok {
return nil, &errValue{s: s, e: ErrInvalidType}
}
return r, nil
}
// BoolDefault attempts to return the value with the provided name as a boolean
// value.
//
// This function will return the default value specified if the value does not exist
// or is not a boolean type.
func (c Content) BoolDefault(s string, d bool) bool {
v, ok := c[s]
if !ok {
return d
}
r, ok := v.(bool)
if !ok {
return d
}
return r
}
// IntDefault attempts to return the value with the provided name as an integer
// value.
//
// This function will return the default value specified if the value does not exist
// or is not an integer type.
func (c Content) IntDefault(s string, d int64) int64 {
r, err := c.Float(s)
if err != nil {
return d
}
return int64(r)
}
// BytesEmpty attempts to return the value with the provided name as a byte slice
// value that is represented by a Base64-encoded string.
//
// This function will return an 'ErrInvalidType' if the value does not represent
// a bytes type. Empty or missing values will simply return none.
//
// This function is different than the other 'Bytes' function as it allows for
// empty/missing byte slices but not invalid or improperly formatted ones.
//
// This will attempt to decode the Base64 string and will return the encoding
// errors if they occur.
func (c Content) BytesEmpty(s string) ([]byte, error) {
v, ok := c[s]
if !ok {
return nil, nil
}
r, ok := v.(string)
if !ok {
return nil, &errValue{s: s, e: ErrInvalidType}
}
if len(r) == 0 {
return nil, nil
}
return base64.StdEncoding.DecodeString(r)
}
// UintDefault attempts to return the value with the provided name as an unsigned
// integer value.
//
// This function will return the default value specified if the value does not exist
// or is not an unsigned integer type.
func (c Content) UintDefault(s string, d uint64) uint64 {
r, err := c.Float(s)
if err != nil {
return d
}
return uint64(r)
}
// BytesDefault to return the value with the provided name as a byte slice value
// that is represented by a Base64-encoded string.
//
// This function will return the default value specified if the value does not exist
// or is not a bytes type.
//
// This will attempt to decode the Base64 string and will return the default value
// if errors occur.
func (c Content) BytesDefault(s string, d []byte) []byte {
v, ok := c[s]
if !ok {
return d
}
r, ok := v.(string)
if !ok {
return d
}
if b, err := base64.StdEncoding.DecodeString(r); err == nil {
return b
}
return d
}
// FloatDefault attempts to return the value with the provided name as a floating
// point value.
//
// This function will return the default value specified if the value does not exist
// or is not a float type.
func (c Content) FloatDefault(s string, d float64) float64 {
v, ok := c[s]
if !ok {
return d
}
r, ok := v.(float64)
if !ok {
return d
}
return r
}
// ObjectDefault attempts to return the value with the provided name as an object
// value (wrapped as a Content alias).
//
// This function will return the default value specified if the value does not exist
// or is not an object type.
func (c Content) ObjectDefault(s string, d Content) Content {
v, ok := c[s]
if !ok {
return d
}
r, ok := v.(map[string]any)
if !ok {
return d
}
return r
}