Skip to content

Commit 2d1d12c

Browse files
authored
fix(BREV-2112): Allow unmarshalling of empty bytes to an empty struct (#61)
1 parent c9c94be commit 2d1d12c

2 files changed

Lines changed: 5 additions & 5 deletions

File tree

v1/bytes.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
var (
13-
zeroBytes = Bytes{value: 0, unit: Byte}
13+
zero = Bytes{}
1414

1515
ErrBytesInvalidUnit = errors.New("invalid unit")
1616
ErrBytesNotAnInt64 = errors.New("byte count is not an int64")
@@ -128,7 +128,7 @@ func (b *Bytes) UnmarshalJSON(data []byte) error {
128128
}
129129

130130
if bytesJSON.Value == 0 && bytesJSON.Unit == "" {
131-
*b = zeroBytes
131+
*b = zero
132132
return nil
133133
}
134134

v1/bytes_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func TestBytesUnmarshalJSON(t *testing.T) {
8181
want Bytes
8282
wantErr error
8383
}{
84-
{name: "Empty bytes", json: `{"value":0,"unit":""}`, want: zeroBytes, wantErr: nil},
84+
{name: "Empty bytes", json: `{"value":0,"unit":""}`, want: zero, wantErr: nil},
8585
{name: "1000 B", json: `{"value":1000,"unit":"B"}`, want: NewBytes(1000, Byte), wantErr: nil},
8686
{name: "1000 KB", json: `{"value":1000,"unit":"KB"}`, want: NewBytes(1000, Kilobyte), wantErr: nil},
8787
{name: "1000 MB", json: `{"value":1000,"unit":"MB"}`, want: NewBytes(1000, Megabyte), wantErr: nil},
@@ -94,8 +94,8 @@ func TestBytesUnmarshalJSON(t *testing.T) {
9494
{name: "1000 TiB", json: `{"value":1000,"unit":"TiB"}`, want: NewBytes(1000, Tebibyte), wantErr: nil},
9595
{name: "1000 PiB", json: `{"value":1000,"unit":"PiB"}`, want: NewBytes(1000, Pebibyte), wantErr: nil},
9696

97-
{name: "Empty unit", json: `{"value":1000,"unit":""}`, want: zeroBytes, wantErr: ErrBytesInvalidUnit},
98-
{name: "Invalid unit", json: `{"value":1000,"unit":"invalid"}`, want: zeroBytes, wantErr: ErrBytesInvalidUnit},
97+
{name: "Empty unit", json: `{"value":1000,"unit":""}`, want: zero, wantErr: ErrBytesInvalidUnit},
98+
{name: "Invalid unit", json: `{"value":1000,"unit":"invalid"}`, want: zero, wantErr: ErrBytesInvalidUnit},
9999
}
100100

101101
for _, test := range tests {

0 commit comments

Comments
 (0)