Skip to content

Commit cc138c9

Browse files
committed
fix(BREV-2112): Allow unmarshalling of empty bytes
1 parent 93cb879 commit cc138c9

2 files changed

Lines changed: 22 additions & 18 deletions

File tree

v1/bytes.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ func (b *Bytes) UnmarshalJSON(data []byte) error {
127127
return errors.WrapAndTrace(err)
128128
}
129129

130+
if bytesJSON.Value == 0 && bytesJSON.Unit == "" {
131+
*b = zeroBytes
132+
return nil
133+
}
134+
130135
unit, err := stringToBytesUnit(bytesJSON.Unit)
131136
if err != nil {
132137
return errors.WrapAndTrace(err)

v1/bytes_test.go

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ func TestBytesMarshalJSON(t *testing.T) {
6060
for _, test := range tests {
6161
t.Run(test.name, func(t *testing.T) {
6262
json, err := json.Marshal(test.bytes)
63-
if test.wantErr != nil {
64-
if err == nil {
65-
t.Fatalf("json.Marshal() error = nil, want %v", test.wantErr)
63+
if err != nil {
64+
if test.wantErr == nil {
65+
t.Fatalf("json.Marshal() error = %v, want nil", err)
6666
}
6767
if !errors.Is(err, test.wantErr) {
6868
t.Fatalf("json.Marshal() error = %v, want %v", err, test.wantErr)
@@ -81,6 +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},
8485
{name: "1000 B", json: `{"value":1000,"unit":"B"}`, want: NewBytes(1000, Byte), wantErr: nil},
8586
{name: "1000 KB", json: `{"value":1000,"unit":"KB"}`, want: NewBytes(1000, Kilobyte), wantErr: nil},
8687
{name: "1000 MB", json: `{"value":1000,"unit":"MB"}`, want: NewBytes(1000, Megabyte), wantErr: nil},
@@ -101,9 +102,9 @@ func TestBytesUnmarshalJSON(t *testing.T) {
101102
t.Run(test.name, func(t *testing.T) {
102103
var bytes Bytes
103104
err := json.Unmarshal([]byte(test.json), &bytes)
104-
if test.wantErr != nil {
105-
if err == nil {
106-
t.Fatalf("json.Unmarshal() error = nil, want %v", test.wantErr)
105+
if err != nil {
106+
if test.wantErr == nil {
107+
t.Fatalf("json.Unmarshal() error = %v, want nil", err)
107108
}
108109
if !errors.Is(err, test.wantErr) {
109110
t.Fatalf("json.Unmarshal() error = %v, want %v", err, test.wantErr)
@@ -404,12 +405,11 @@ func TestBytesByteCountInUnitInt64(t *testing.T) {
404405
t.Run(test.name, func(t *testing.T) {
405406
got, err := test.bytes.ByteCountInUnitInt64(test.unit)
406407
if err != nil {
407-
if test.wantErr != nil {
408-
if !errors.Is(err, test.wantErr) {
409-
t.Errorf("Bytes.ByteCountInUnitInt64() = %v, want %v", err, test.wantErr)
410-
}
411-
} else {
412-
t.Errorf("Bytes.ByteCountInUnitInt64() = %v, want %v", err, test.wantErr)
408+
if test.wantErr == nil {
409+
t.Fatalf("Bytes.ByteCountInUnitInt64() error = %v, want nil", err)
410+
}
411+
if !errors.Is(err, test.wantErr) {
412+
t.Fatalf("Bytes.ByteCountInUnitInt64() error = %v, want %v", err, test.wantErr)
413413
}
414414
} else if got != test.want {
415415
t.Errorf("Bytes.ByteCountInUnitInt64() = %v, want %v", got, test.want)
@@ -434,12 +434,11 @@ func TestBytesByteCountInUnitInt32(t *testing.T) {
434434
t.Run(test.name, func(t *testing.T) {
435435
got, err := test.bytes.ByteCountInUnitInt32(test.unit)
436436
if err != nil {
437-
if test.wantErr != nil {
438-
if !errors.Is(err, test.wantErr) {
439-
t.Errorf("Bytes.ByteCountInUnitInt32() = %v, want %v", err, test.wantErr)
440-
}
441-
} else {
442-
t.Errorf("Bytes.ByteCountInUnitInt32() = %v, want %v", err, test.wantErr)
437+
if test.wantErr == nil {
438+
t.Fatalf("Bytes.ByteCountInUnitInt32() error = %v, want nil", err)
439+
}
440+
if !errors.Is(err, test.wantErr) {
441+
t.Fatalf("Bytes.ByteCountInUnitInt32() error = %v, want %v", err, test.wantErr)
443442
}
444443
} else if got != test.want {
445444
t.Errorf("Bytes.ByteCountInUnitInt32() = %v, want %v", got, test.want)

0 commit comments

Comments
 (0)