Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions v1/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

var (
zeroBytes = Bytes{value: 0, unit: Byte}
zero = Bytes{}

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

if bytesJSON.Value == 0 && bytesJSON.Unit == "" {
*b = zeroBytes
*b = zero
return nil
}

Expand Down
6 changes: 3 additions & 3 deletions v1/bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestBytesUnmarshalJSON(t *testing.T) {
want Bytes
wantErr error
}{
{name: "Empty bytes", json: `{"value":0,"unit":""}`, want: zeroBytes, wantErr: nil},
{name: "Empty bytes", json: `{"value":0,"unit":""}`, want: zero, wantErr: nil},
{name: "1000 B", json: `{"value":1000,"unit":"B"}`, want: NewBytes(1000, Byte), wantErr: nil},
{name: "1000 KB", json: `{"value":1000,"unit":"KB"}`, want: NewBytes(1000, Kilobyte), wantErr: nil},
{name: "1000 MB", json: `{"value":1000,"unit":"MB"}`, want: NewBytes(1000, Megabyte), wantErr: nil},
Expand All @@ -94,8 +94,8 @@ func TestBytesUnmarshalJSON(t *testing.T) {
{name: "1000 TiB", json: `{"value":1000,"unit":"TiB"}`, want: NewBytes(1000, Tebibyte), wantErr: nil},
{name: "1000 PiB", json: `{"value":1000,"unit":"PiB"}`, want: NewBytes(1000, Pebibyte), wantErr: nil},

{name: "Empty unit", json: `{"value":1000,"unit":""}`, want: zeroBytes, wantErr: ErrBytesInvalidUnit},
{name: "Invalid unit", json: `{"value":1000,"unit":"invalid"}`, want: zeroBytes, wantErr: ErrBytesInvalidUnit},
{name: "Empty unit", json: `{"value":1000,"unit":""}`, want: zero, wantErr: ErrBytesInvalidUnit},
{name: "Invalid unit", json: `{"value":1000,"unit":"invalid"}`, want: zero, wantErr: ErrBytesInvalidUnit},
}

for _, test := range tests {
Expand Down
Loading