/
null_bytesize_in_mb.go
88 lines (69 loc) · 1.4 KB
/
null_bytesize_in_mb.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
package types
import (
"encoding/json"
"strconv"
"code.cloudfoundry.org/bytefmt"
)
// NullByteSizeInMb represents size in a byte format in megabytes.
type NullByteSizeInMb struct {
IsSet bool
// Value is a size in MB
Value uint64
}
func (b NullByteSizeInMb) String() string {
if !b.IsSet {
return ""
}
return bytefmt.ByteSize(b.Value * bytefmt.MEGABYTE)
}
func (b *NullByteSizeInMb) ParseStringValue(value string) error {
if value == "" {
b.IsSet = false
b.Value = 0
return nil
}
byteSize, fmtErr := bytefmt.ToMegabytes(value)
if fmtErr != nil {
return fmtErr
}
b.IsSet = true
b.Value = byteSize
return nil
}
// ParseUint64Value is used to parse a user provided *uint64 argument.
func (b *NullByteSizeInMb) ParseUint64Value(val *uint64) {
if val == nil {
b.IsSet = false
b.Value = 0
return
}
b.Value = *val
b.IsSet = true
}
func (b *NullByteSizeInMb) UnmarshalJSON(rawJSON []byte) error {
if len(rawJSON) == 0 {
b.Value = 0
b.IsSet = false
return nil
}
var value json.Number
err := json.Unmarshal(rawJSON, &value)
if err != nil {
return err
}
if jsonNumberIsUninitialized(value) {
b.Value = 0
b.IsSet = false
return nil
}
valueInt, err := strconv.ParseUint(value.String(), 10, 64)
if err != nil {
return err
}
b.Value = valueInt
b.IsSet = true
return nil
}
func jsonNumberIsUninitialized(value json.Number) bool {
return value.String() == ""
}