/
payload.go
283 lines (257 loc) · 8.43 KB
/
payload.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
/*
Copyright 2014 - 2015 Workiva, LLC
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 rest
import (
"fmt"
"time"
)
// Payload is the unmarshalled request body.
type Payload map[string]interface{}
// Get returns the value with the given key as an interface{}. If the key doesn't
// exist, nil is returned with an error.
func (p Payload) Get(key string) (interface{}, error) {
if value, ok := p[key]; ok {
return value, nil
}
return nil, fmt.Errorf("No value with key '%s'", key)
}
// GetInt returns the value with the given key as an int. If the key doesn't
// exist or is not an int, the zero value is returned with an error.
func (p Payload) GetInt(key string) (int, error) {
value, err := p.Get(key)
if err != nil {
return 0, err
}
if value, ok := value.(int); ok {
return value, nil
}
return 0, fmt.Errorf("Value with key '%s' not an int", key)
}
// GetInt8 returns the value with the given key as an int8. If the key doesn't
// exist or is not an int8, the zero value is returned with an error.
func (p Payload) GetInt8(key string) (int8, error) {
value, err := p.Get(key)
if err != nil {
return 0, err
}
if value, ok := value.(int8); ok {
return value, nil
}
return 0, fmt.Errorf("Value with key '%s' not an int8", key)
}
// GetInt16 returns the value with the given key as an int16. If the key doesn't
// exist or is not an int16, the zero value is returned with an error.
func (p Payload) GetInt16(key string) (int16, error) {
value, err := p.Get(key)
if err != nil {
return 0, err
}
if value, ok := value.(int16); ok {
return value, nil
}
return 0, fmt.Errorf("Value with key '%s' not an int16", key)
}
// GetInt32 returns the value with the given key as an int32. If the key doesn't
// exist or is not an int32, the zero value is returned with an error.
func (p Payload) GetInt32(key string) (int32, error) {
value, err := p.Get(key)
if err != nil {
return 0, err
}
if value, ok := value.(int32); ok {
return value, nil
}
return 0, fmt.Errorf("Value with key '%s' not an int32", key)
}
// GetInt64 returns the value with the given key as an int64. If the key doesn't
// exist or is not an int64, the zero value is returned with an error.
func (p Payload) GetInt64(key string) (int64, error) {
value, err := p.Get(key)
if err != nil {
return 0, err
}
if value, ok := value.(int64); ok {
return value, nil
}
return 0, fmt.Errorf("Value with key '%s' not an int64", key)
}
// GetUint returns the value with the given key as a uint. If the key doesn't
// exist or is not a uint, the zero value is returned with an error.
func (p Payload) GetUint(key string) (uint, error) {
value, err := p.Get(key)
if err != nil {
return 0, err
}
if value, ok := value.(uint); ok {
return value, nil
}
return 0, fmt.Errorf("Value with key '%s' not a uint", key)
}
// GetUint8 returns the value with the given key as a uint8. If the key doesn't
// exist or is not a uint8, the zero value is returned with an error.
func (p Payload) GetUint8(key string) (uint8, error) {
value, err := p.Get(key)
if err != nil {
return 0, err
}
if value, ok := value.(uint8); ok {
return value, nil
}
return 0, fmt.Errorf("Value with key '%s' not a uint8", key)
}
// GetUint16 returns the value with the given key as a uint16. If the key doesn't
// exist or is not a uint16, the zero value is returned with an error.
func (p Payload) GetUint16(key string) (uint16, error) {
value, err := p.Get(key)
if err != nil {
return 0, err
}
if value, ok := value.(uint16); ok {
return value, nil
}
return 0, fmt.Errorf("Value with key '%s' not a uint16", key)
}
// GetUint32 returns the value with the given key as a uint32. If the key doesn't
// exist or is not a uint32, the zero value is returned with an error.
func (p Payload) GetUint32(key string) (uint32, error) {
value, err := p.Get(key)
if err != nil {
return 0, err
}
if value, ok := value.(uint32); ok {
return value, nil
}
return 0, fmt.Errorf("Value with key '%s' not a uint32", key)
}
// GetUint64 returns the value with the given key as a uint64. If the key doesn't
// exist or is not a uint64, the zero value is returned with an error.
func (p Payload) GetUint64(key string) (uint64, error) {
value, err := p.Get(key)
if err != nil {
return 0, err
}
if value, ok := value.(uint64); ok {
return value, nil
}
return 0, fmt.Errorf("Value with key '%s' not a uint64", key)
}
// GetFloat32 returns the value with the given key as a Float32. If the key doesn't
// exist or is not a Float32, the zero value is returned with an error.
func (p Payload) GetFloat32(key string) (float32, error) {
value, err := p.Get(key)
if err != nil {
return 0, err
}
if value, ok := value.(float32); ok {
return value, nil
}
return 0, fmt.Errorf("Value with key '%s' not a float32", key)
}
// GetFloat64 returns the value with the given key as a Float64. If the key doesn't
// exist or is not a Float32, the zero value is returned with an error.
func (p Payload) GetFloat64(key string) (float64, error) {
value, err := p.Get(key)
if err != nil {
return 0, err
}
if value, ok := value.(float64); ok {
return value, nil
}
return 0, fmt.Errorf("Value with key '%s' not a float64", key)
}
// GetByte returns the value with the given key as a byte. If the key doesn't
// exist or is not a byte, the zero value is returned with an error.
func (p Payload) GetByte(key string) (byte, error) {
value, err := p.Get(key)
if err != nil {
return 0, err
}
if value, ok := value.(byte); ok {
return value, nil
}
return 0, fmt.Errorf("Value with key '%s' not a byte", key)
}
// GetString returns the value with the given key as a string. If the key doesn't
// exist or is not a string, the zero value is returned with an error.
func (p Payload) GetString(key string) (string, error) {
value, err := p.Get(key)
if err != nil {
return "", err
}
if value, ok := value.(string); ok {
return value, nil
}
return "", fmt.Errorf("Value with key '%s' not a string", key)
}
// GetBool returns the value with the given key as a bool. If the key doesn't
// exist or is not a bool, false is returned with an error.
func (p Payload) GetBool(key string) (bool, error) {
value, err := p.Get(key)
if err != nil {
return false, err
}
if value, ok := value.(bool); ok {
return value, nil
}
return false, fmt.Errorf("Value with key '%s' not a bool", key)
}
// GetSlice returns the value with the given key as an []interface{}. If the value
// doesn't exist or is not an []interface{}, nil is returned with an error.
func (p Payload) GetSlice(key string) ([]interface{}, error) {
value, err := p.Get(key)
if err != nil {
return nil, err
}
if value, ok := value.([]interface{}); ok {
return value, nil
}
return nil, fmt.Errorf("Value with key '%s' not a slice", key)
}
// GetMap returns the value with the given key as a map[string]interface{}. If the
// key doesn't exist or is not a map[string]interface{}, nil is returned with an
// error.
func (p Payload) GetMap(key string) (map[string]interface{}, error) {
value, err := p.Get(key)
if err != nil {
return nil, err
}
if value, ok := value.(map[string]interface{}); ok {
return value, nil
}
return nil, fmt.Errorf("Value with key '%s' not a map", key)
}
// GetDuration returns the value with the given key as a time.Duration. If the key
// doesn't exist or is not a time.Duration, the zero value is returned with an
// error.
func (p Payload) GetDuration(key string) (time.Duration, error) {
value, err := p.Get(key)
if err != nil {
return 0, err
}
if value, ok := value.(time.Duration); ok {
return value, nil
}
return 0, fmt.Errorf("Value with key '%s' not a time.Duration", key)
}
// GetTime returns the value with the given key as a time.Time. If the key doesn't
// exist or is not a time.Time, the zero value is returned with an error.
func (p Payload) GetTime(key string) (time.Time, error) {
value, err := p.Get(key)
if err != nil {
return time.Time{}, err
}
if value, ok := value.(time.Time); ok {
return value, nil
}
return time.Time{}, fmt.Errorf("Value with key '%s' not a time.Time", key)
}