Skip to content

Commit b1b3286

Browse files
authored
Merge pull request #4 from MyBlackJay/DEVELOP
added Unmarshaler/Marshaler for json/yaml, handling empty values
2 parents 41a829d + 2f689a7 commit b1b3286

File tree

1 file changed

+67
-3
lines changed

1 file changed

+67
-3
lines changed

parser.go

+67-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ import (
99
"strings"
1010
)
1111

12+
// available errors
13+
var (
14+
UnsupportedFormat = errors.New("unsupported data size format")
15+
UnexpectedError = errors.New("unable convert")
16+
)
17+
18+
// regexp parsing string expression
1219
const (
1320
measurePattern string = `^([bB]|[bB]ytes|[kmgtpeKMGTPE]|[kmgtpeKMGTPE]?[iI]|[kmgtpeKMGTPE][iI]?[bB])?$`
1421
sizePattern string = `^([0-9]+|[0-9]*\.[0-9]+)([bB]|[bB]ytes|[kmgtpeKMGTPE]|[kmgtpeKMGTPE]?[iI]|[kmgtpeKMGTPE][iI]?[bB])?$`
@@ -40,13 +47,21 @@ func (rs *ReadableSize) GetRaw() big.Float {
4047

4148
// Get returns the compiled data size in big.Int.
4249
func (rs *ReadableSize) Get() big.Int {
50+
if rs.compiled == nil {
51+
return *big.NewInt(0)
52+
}
53+
4354
res, _ := rs.compiled.Int(new(big.Int))
4455
return *res
4556
}
4657

4758
// GetCompiledUInt64 returns the compiled data size in uint64.
4859
// Warning: Possible rounding overflow, use with relatively small numbers.
4960
func (rs *ReadableSize) GetCompiledUInt64() uint64 {
61+
if rs.compiled == nil {
62+
return 0
63+
}
64+
5065
res, _ := rs.compiled.Uint64()
5166
return res
5267
}
@@ -62,7 +77,56 @@ func (rs *ReadableSize) GetCompiledInMeasure(measure string) (float64, error) {
6277
return tmp, nil
6378
}
6479

65-
return 0, errors.New("unsupported measure format")
80+
return 0, UnsupportedFormat
81+
}
82+
83+
// UnmarshalJSON designed to serialize a string in data size expression to *ReadableSize, defined in user code via the json library
84+
func (rs *ReadableSize) UnmarshalJSON(source []byte) error {
85+
value := string(source)
86+
87+
if len(value) < 2 {
88+
return UnsupportedFormat
89+
}
90+
91+
if value == "null" {
92+
return nil
93+
}
94+
95+
if parsed, err := Compile(value[1 : len(value)-1]); err == nil {
96+
*rs = *parsed
97+
return nil
98+
} else {
99+
return err
100+
}
101+
}
102+
103+
// MarshalJSON designed to deserialize *ReadableSize to original data size expression defined in user code via the json library
104+
func (rs ReadableSize) MarshalJSON() ([]byte, error) {
105+
return []byte("\"" + rs.input + "\""), nil
106+
}
107+
108+
// UnmarshalYAML designed to serialize a string in data size expression to *ReadableSize, defined in user code via the gopkg.in/yaml.v3 library
109+
func (rs *ReadableSize) UnmarshalYAML(unmarshal func(interface{}) error) error {
110+
var str string
111+
if err := unmarshal(&str); err != nil {
112+
return UnsupportedFormat
113+
}
114+
115+
if str == "null" {
116+
return nil
117+
}
118+
119+
if parsed, err := Compile(str); err == nil {
120+
*rs = *parsed
121+
return nil
122+
} else {
123+
return err
124+
}
125+
}
126+
127+
// MarshalYAML designed to deserialize *ReadableSize in original data size expression defined in user code via the gopkg.in/yaml.v3 library
128+
func (rs ReadableSize) MarshalYAML() (interface{}, error) {
129+
return rs.input, nil
66130
}
67131

68132
// compileMeasuring returns a numeric representation of a data unit in int64.
@@ -110,7 +174,7 @@ func Compile(input string) (*ReadableSize, error) {
110174
}
111175
}
112176

113-
return nil, errors.New("unsupported data size format")
177+
return nil, UnsupportedFormat
114178
}
115179

116180
// MustCompile parses a data size expression and returns, if successful,
@@ -154,5 +218,5 @@ func BytesToSize(size float64, precision uint) (string, error) {
154218
size /= 1 << 10
155219
}
156220

157-
return "", errors.New("unable convert")
221+
return "", UnexpectedError
158222
}

0 commit comments

Comments
 (0)