-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathcustom_types.go
483 lines (397 loc) · 11.2 KB
/
custom_types.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
package scw
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net"
"strconv"
"strings"
"time"
"github.com/scaleway/scaleway-sdk-go/errors"
"github.com/scaleway/scaleway-sdk-go/logger"
)
// ServiceInfo contains API metadata
// These metadata are only here for debugging. Do not rely on these values
type ServiceInfo struct {
// Name is the name of the API
Name string `json:"name"`
// Description is a human readable description for the API
Description string `json:"description"`
// Version is the version of the API
Version string `json:"version"`
// DocumentationURL is the a web url where the documentation of the API can be found
DocumentationURL *string `json:"documentation_url"`
}
// File is the structure used to receive / send a file from / to the API
type File struct {
// Name of the file
Name string `json:"name"`
// ContentType used in the HTTP header `Content-Type`
ContentType string `json:"content_type"`
// Content of the file
Content io.Reader `json:"content"`
}
func (f *File) MarshalJSON() ([]byte, error) {
buf := new(bytes.Buffer)
if f.Content != nil {
_, err := io.Copy(buf, f.Content)
if err != nil {
return nil, err
}
}
tmpFile := struct {
Name string `json:"name"`
ContentType string `json:"content_type"`
Content string `json:"content"`
}{
Name: f.Name,
ContentType: f.ContentType,
Content: buf.String(),
}
return json.Marshal(tmpFile)
}
func (f *File) UnmarshalJSON(b []byte) error {
type file File
var tmpFile struct {
file
Content []byte `json:"content"`
}
err := json.Unmarshal(b, &tmpFile)
if err != nil {
return err
}
tmpFile.file.Content = bytes.NewReader(tmpFile.Content)
*f = File(tmpFile.file)
return nil
}
// Money represents an amount of money with its currency type.
type Money struct {
// CurrencyCode is the 3-letter currency code defined in ISO 4217.
CurrencyCode string `json:"currency_code"`
// Units is the whole units of the amount.
// For example if `currencyCode` is `"USD"`, then 1 unit is one US dollar.
Units int64 `json:"units"`
// Nanos is the number of nano (10^-9) units of the amount.
// The value must be between -999,999,999 and +999,999,999 inclusive.
// If `units` is positive, `nanos` must be positive or zero.
// If `units` is zero, `nanos` can be positive, zero, or negative.
// If `units` is negative, `nanos` must be negative or zero.
// For example $-1.75 is represented as `units`=-1 and `nanos`=-750,000,000.
Nanos int32 `json:"nanos"`
}
// NewMoneyFromFloat converts a float with currency to a Money.
//
// value: The float value.
// currencyCode: The 3-letter currency code defined in ISO 4217.
// precision: The number of digits after the decimal point used to parse the nanos part of the value.
//
// Examples:
// - (value = 1.3333, precision = 2) => Money{Units = 1, Nanos = 330000000}
// - (value = 1.123456789, precision = 9) => Money{Units = 1, Nanos = 123456789}
func NewMoneyFromFloat(value float64, currencyCode string, precision int) *Money {
if precision > 9 {
panic(errors.New("max precision is 9"))
}
strValue := strconv.FormatFloat(value, 'f', precision, 64)
units, nanos, err := splitFloatString(strValue)
if err != nil {
panic(err)
}
return &Money{
CurrencyCode: currencyCode,
Units: units,
Nanos: nanos,
}
}
// String returns the string representation of Money.
func (m Money) String() string {
currencySignsByCodes := map[string]string{
"EUR": "€",
"USD": "$",
}
currencySign, currencySignFound := currencySignsByCodes[m.CurrencyCode]
if !currencySignFound {
logger.Debugf("%s currency code is not supported\n", m.CurrencyCode)
currencySign = m.CurrencyCode
}
cents := fmt.Sprintf("%09d", m.Nanos)
cents = cents[:2] + strings.TrimRight(cents[2:], "0")
return fmt.Sprintf("%s %d.%s", currencySign, m.Units, cents)
}
// ToFloat converts a Money object to a float.
func (m Money) ToFloat() float64 {
return float64(m.Units) + float64(m.Nanos)/1e9
}
// Size represents a size in bytes.
type Size uint64
const (
B Size = 1
KB = 1000 * B
MB = 1000 * KB
GB = 1000 * MB
TB = 1000 * GB
PB = 1000 * TB
)
// String returns the string representation of a Size.
func (s Size) String() string {
return fmt.Sprintf("%d", s)
}
// TimeSeries represents a time series that could be used for graph purposes.
type TimeSeries struct {
// Name of the metric.
Name string `json:"name"`
// Points contains all the points that composed the series.
Points []*TimeSeriesPoint `json:"points"`
// Metadata contains some string metadata related to a metric.
Metadata map[string]string `json:"metadata"`
}
// TimeSeriesPoint represents a point of a time series.
type TimeSeriesPoint struct {
Timestamp time.Time
Value float32
}
func (tsp TimeSeriesPoint) MarshalJSON() ([]byte, error) {
timestamp := tsp.Timestamp.Format(time.RFC3339)
value, err := json.Marshal(tsp.Value)
if err != nil {
return nil, err
}
return []byte(`["` + timestamp + `",` + string(value) + "]"), nil
}
func (tsp *TimeSeriesPoint) UnmarshalJSON(b []byte) error {
point := [2]interface{}{}
err := json.Unmarshal(b, &point)
if err != nil {
return err
}
if len(point) != 2 {
return errors.New("invalid point array")
}
strTimestamp, isStrTimestamp := point[0].(string)
if !isStrTimestamp {
return fmt.Errorf("%s timestamp is not a string in RFC 3339 format", point[0])
}
timestamp, err := time.Parse(time.RFC3339, strTimestamp)
if err != nil {
return fmt.Errorf("%s timestamp is not in RFC 3339 format", point[0])
}
tsp.Timestamp = timestamp
// By default, JSON unmarshal a float in float64 but the TimeSeriesPoint is a float32 value.
value, isValue := point[1].(float64)
if !isValue {
return fmt.Errorf("%s is not a valid float32 value", point[1])
}
tsp.Value = float32(value)
return nil
}
// IPNet inherits net.IPNet and represents an IP network.
type IPNet struct {
net.IPNet
}
func (n IPNet) MarshalJSON() ([]byte, error) {
value := n.String()
if value == "<nil>" {
value = ""
}
return []byte(`"` + value + `"`), nil
}
func (n *IPNet) UnmarshalJSON(b []byte) error {
var str string
err := json.Unmarshal(b, &str)
if err != nil {
return err
}
if str == "" {
*n = IPNet{}
return nil
}
switch ip := net.ParseIP(str); {
case ip.To4() != nil:
str += "/32"
case ip.To16() != nil:
str += "/128"
}
ip, value, err := net.ParseCIDR(str)
if err != nil {
return err
}
value.IP = ip
n.IPNet = *value
return nil
}
// Duration represents a signed, fixed-length span of time represented as a
// count of seconds and fractions of seconds at nanosecond resolution. It is
// independent of any calendar and concepts like "day" or "month". It is related
// to Timestamp in that the difference between two Timestamp values is a Duration
// and it can be added or subtracted from a Timestamp.
// Range is approximately +-10,000 years.
type Duration struct {
Seconds int64
Nanos int32
}
func (d *Duration) ToTimeDuration() *time.Duration {
if d == nil {
return nil
}
timeDuration := time.Duration(d.Nanos) + time.Duration(d.Seconds)*time.Second
return &timeDuration
}
func (d Duration) MarshalJSON() ([]byte, error) {
nanos := d.Nanos
if nanos < 0 {
nanos = -nanos
}
return []byte(`"` + fmt.Sprintf("%d.%09d", d.Seconds, nanos) + `s"`), nil
}
func (d *Duration) UnmarshalJSON(b []byte) error {
if string(b) == "null" {
return nil
}
var str string
err := json.Unmarshal(b, &str)
if err != nil {
return err
}
if str == "" {
*d = Duration{}
return nil
}
seconds, nanos, err := splitFloatString(strings.TrimRight(str, "s"))
if err != nil {
return err
}
*d = Duration{
Seconds: seconds,
Nanos: nanos,
}
return nil
}
func NewDurationFromTimeDuration(t time.Duration) *Duration {
duration := Duration{
Seconds: int64(t.Seconds()),
}
duration.Nanos = int32(t.Nanoseconds() - (time.Duration(duration.Seconds) * time.Second).Nanoseconds())
return &duration
}
// splitFloatString splits a float represented in a string, and returns its units (left-coma part) and nanos (right-coma part).
// E.g.:
// "3" ==> units = 3 | nanos = 0
// "3.14" ==> units = 3 | nanos = 14*1e7
// "-3.14" ==> units = -3 | nanos = -14*1e7
func splitFloatString(input string) (units int64, nanos int32, err error) {
parts := strings.SplitN(input, ".", 2)
// parse units as int64
units, err = strconv.ParseInt(parts[0], 10, 64)
if err != nil {
return 0, 0, errors.Wrap(err, "invalid units")
}
// handle nanos
if len(parts) == 2 {
// add leading zeros
strNanos := parts[1] + "000000000"[len(parts[1]):]
// parse nanos as int32
n, err := strconv.ParseUint(strNanos, 10, 32)
if err != nil {
return 0, 0, errors.Wrap(err, "invalid nanos")
}
nanos = int32(n)
}
if units < 0 {
nanos = -nanos
}
return units, nanos, nil
}
// JSONObject represent any JSON object. See struct.proto.
// It will be marshaled into a json string.
// This type can be used just like any other map.
//
// Example:
//
// values := scw.JSONValue{
// "Foo": "Bar",
// }
// values["Baz"] = "Qux"
type JSONObject map[string]interface{}
// EscapeMode is the mode that should be use for escaping a value
type EscapeMode uint
// The modes for escaping a value before it is marshaled, and unmarshalled.
const (
NoEscape EscapeMode = iota
Base64Escape
QuotedEscape
)
// DecodeJSONObject will attempt to decode the string input as a JSONValue.
// Optionally decoding base64 the value first before JSON unmarshalling.
//
// Will panic if the escape mode is unknown.
func DecodeJSONObject(v string, escape EscapeMode) (JSONObject, error) {
var b []byte
var err error
switch escape {
case NoEscape:
b = []byte(v)
case Base64Escape:
b, err = base64.StdEncoding.DecodeString(v)
case QuotedEscape:
var u string
u, err = strconv.Unquote(v)
b = []byte(u)
default:
panic(fmt.Sprintf("DecodeJSONObject called with unknown EscapeMode, %v", escape))
}
if err != nil {
return nil, err
}
m := JSONObject{}
err = json.Unmarshal(b, &m)
if err != nil {
return nil, err
}
return m, nil
}
// EncodeJSONObject marshals the value into a JSON string, and optionally base64
// encodes the string before returning it.
//
// Will panic if the escape mode is unknown.
func EncodeJSONObject(v JSONObject, escape EscapeMode) (string, error) {
b, err := json.Marshal(v)
if err != nil {
return "", err
}
switch escape {
case NoEscape:
return string(b), nil
case Base64Escape:
return base64.StdEncoding.EncodeToString(b), nil
case QuotedEscape:
return strconv.Quote(string(b)), nil
}
panic(fmt.Sprintf("EncodeJSONObject called with unknown EscapeMode, %v", escape))
}
// Decimal is a representation of a decimal value, such as 2.5.
// Comparable to language-native decimal formats, such as Java's BigDecimal or
// Python's decimal.Decimal.
// Lookup protobuf google.type.Decimal for details.
type Decimal string
var _ fmt.Stringer = (*Decimal)(nil)
func (d Decimal) String() string {
return string(d)
}
func (d Decimal) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"value": d.String(),
})
}
func (d *Decimal) UnmarshalJSON(b []byte) error {
m := struct {
Value string `json:"value"`
}{}
err := json.Unmarshal(b, &m)
if err != nil {
return err
}
*d = Decimal(m.Value)
return nil
}