forked from cloudevents/sdk-go
/
value.go
335 lines (308 loc) · 7.54 KB
/
value.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
Copyright 2021 The CloudEvents Authors
SPDX-License-Identifier: Apache-2.0
*/
package types
import (
"encoding/base64"
"fmt"
"math"
"net/url"
"reflect"
"strconv"
"time"
)
// FormatBool returns canonical string format: "true" or "false"
func FormatBool(v bool) string { return strconv.FormatBool(v) }
// FormatInteger returns canonical string format: decimal notation.
func FormatInteger(v int32) string { return strconv.Itoa(int(v)) }
// FormatBinary returns canonical string format: standard base64 encoding
func FormatBinary(v []byte) string { return base64.StdEncoding.EncodeToString(v) }
// FormatTime returns canonical string format: RFC3339 with nanoseconds
func FormatTime(v time.Time) string { return v.UTC().Format(time.RFC3339Nano) }
// ParseBool parse canonical string format: "true" or "false"
func ParseBool(v string) (bool, error) { return strconv.ParseBool(v) }
// ParseInteger parse canonical string format: decimal notation.
func ParseInteger(v string) (int32, error) {
// Accept floating-point but truncate to int32 as per CE spec.
f, err := strconv.ParseFloat(v, 64)
if err != nil {
return 0, err
}
if f > math.MaxInt32 || f < math.MinInt32 {
return 0, rangeErr(v)
}
return int32(f), nil
}
// ParseBinary parse canonical string format: standard base64 encoding
func ParseBinary(v string) ([]byte, error) { return base64.StdEncoding.DecodeString(v) }
// ParseTime parse canonical string format: RFC3339 with nanoseconds
func ParseTime(v string) (time.Time, error) {
t, err := time.Parse(time.RFC3339Nano, v)
if err != nil {
err := convertErr(time.Time{}, v)
err.extra = ": not in RFC3339 format"
return time.Time{}, err
}
return t, nil
}
// Format returns the canonical string format of v, where v can be
// any type that is convertible to a CloudEvents type.
func Format(v interface{}) (string, error) {
v, err := Validate(v)
if err != nil {
return "", err
}
switch v := v.(type) {
case bool:
return FormatBool(v), nil
case int32:
return FormatInteger(v), nil
case string:
return v, nil
case []byte:
return FormatBinary(v), nil
case URI:
return v.String(), nil
case URIRef:
// url.URL is often passed by pointer so allow both
return v.String(), nil
case Timestamp:
return FormatTime(v.Time), nil
default:
return "", fmt.Errorf("%T is not a CloudEvents type", v)
}
}
// Validate v is a valid CloudEvents attribute value, convert it to one of:
// bool, int32, string, []byte, types.URI, types.URIRef, types.Timestamp
func Validate(v interface{}) (interface{}, error) {
switch v := v.(type) {
case bool, int32, string, []byte:
return v, nil // Already a CloudEvents type, no validation needed.
case uint, uintptr, uint8, uint16, uint32, uint64:
u := reflect.ValueOf(v).Uint()
if u > math.MaxInt32 {
return nil, rangeErr(v)
}
return int32(u), nil
case int, int8, int16, int64:
i := reflect.ValueOf(v).Int()
if i > math.MaxInt32 || i < math.MinInt32 {
return nil, rangeErr(v)
}
return int32(i), nil
case float32, float64:
f := reflect.ValueOf(v).Float()
if f > math.MaxInt32 || f < math.MinInt32 {
return nil, rangeErr(v)
}
return int32(f), nil
case *url.URL:
if v == nil {
break
}
return URI{URL: *v}, nil
case url.URL:
return URI{URL: v}, nil
case *URIRef:
if v != nil {
return *v, nil
}
return nil, nil
case URIRef:
return v, nil
case *URI:
if v != nil {
return *v, nil
}
return nil, nil
case URI:
return v, nil
case time.Time:
return Timestamp{Time: v}, nil
case *time.Time:
if v == nil {
break
}
return Timestamp{Time: *v}, nil
case Timestamp:
return v, nil
}
rx := reflect.ValueOf(v)
if rx.Kind() == reflect.Ptr && !rx.IsNil() {
// Allow pointers-to convertible types
return Validate(rx.Elem().Interface())
}
return nil, fmt.Errorf("invalid CloudEvents value: %#v", v)
}
// Clone v clones a CloudEvents attribute value, which is one of the valid types:
// bool, int32, string, []byte, types.URI, types.URIRef, types.Timestamp
// Returns the same type
// Panics if the type is not valid
func Clone(v interface{}) interface{} {
if v == nil {
return nil
}
switch v := v.(type) {
case bool, int32, string, nil:
return v // Already a CloudEvents type, no validation needed.
case []byte:
clone := make([]byte, len(v))
copy(clone, v)
return v
case url.URL:
return URI{v}
case *url.URL:
return &URI{*v}
case URIRef:
return v
case *URIRef:
return &URIRef{v.URL}
case URI:
return v
case *URI:
return &URI{v.URL}
case time.Time:
return Timestamp{v}
case *time.Time:
return &Timestamp{*v}
case Timestamp:
return v
case *Timestamp:
return &Timestamp{v.Time}
}
panic(fmt.Errorf("invalid CloudEvents value: %#v", v))
}
// ToBool accepts a bool value or canonical "true"/"false" string.
func ToBool(v interface{}) (bool, error) {
v, err := Validate(v)
if err != nil {
return false, err
}
switch v := v.(type) {
case bool:
return v, nil
case string:
return ParseBool(v)
default:
return false, convertErr(true, v)
}
}
// ToInteger accepts any numeric value in int32 range, or canonical string.
func ToInteger(v interface{}) (int32, error) {
v, err := Validate(v)
if err != nil {
return 0, err
}
switch v := v.(type) {
case int32:
return v, nil
case string:
return ParseInteger(v)
default:
return 0, convertErr(int32(0), v)
}
}
// ToString returns a string value unaltered.
//
// This function does not perform canonical string encoding, use one of the
// Format functions for that.
func ToString(v interface{}) (string, error) {
v, err := Validate(v)
if err != nil {
return "", err
}
switch v := v.(type) {
case string:
return v, nil
default:
return "", convertErr("", v)
}
}
// ToBinary returns a []byte value, decoding from base64 string if necessary.
func ToBinary(v interface{}) ([]byte, error) {
v, err := Validate(v)
if err != nil {
return nil, err
}
switch v := v.(type) {
case []byte:
return v, nil
case string:
return base64.StdEncoding.DecodeString(v)
default:
return nil, convertErr([]byte(nil), v)
}
}
// ToURL returns a *url.URL value, parsing from string if necessary.
func ToURL(v interface{}) (*url.URL, error) {
v, err := Validate(v)
if err != nil {
return nil, err
}
switch v := v.(type) {
case *URI:
return &v.URL, nil
case URI:
return &v.URL, nil
case *URIRef:
return &v.URL, nil
case URIRef:
return &v.URL, nil
case string:
u, err := url.Parse(v)
if err != nil {
return nil, err
}
return u, nil
default:
return nil, convertErr((*url.URL)(nil), v)
}
}
// ToTime returns a time.Time value, parsing from RFC3339 string if necessary.
func ToTime(v interface{}) (time.Time, error) {
v, err := Validate(v)
if err != nil {
return time.Time{}, err
}
switch v := v.(type) {
case Timestamp:
return v.Time, nil
case string:
ts, err := time.Parse(time.RFC3339Nano, v)
if err != nil {
return time.Time{}, err
}
return ts, nil
default:
return time.Time{}, convertErr(time.Time{}, v)
}
}
func IsZero(v interface{}) bool {
// Fast path
if v == nil {
return true
}
if s, ok := v.(string); ok && s == "" {
return true
}
return reflect.ValueOf(v).IsZero()
}
type ConvertErr struct {
// Value being converted
Value interface{}
// Type of attempted conversion
Type reflect.Type
extra string
}
func (e *ConvertErr) Error() string {
return fmt.Sprintf("cannot convert %#v to %s%s", e.Value, e.Type, e.extra)
}
func convertErr(target, v interface{}) *ConvertErr {
return &ConvertErr{Value: v, Type: reflect.TypeOf(target)}
}
func rangeErr(v interface{}) error {
e := convertErr(int32(0), v)
e.extra = ": out of range"
return e
}