Skip to content

Commit c9c94be

Browse files
authored
fix(BREV-2112): Allow unmarshalling of empty Bytes (#60)
* fix(BREV-2112): Allow unmarshalling of empty bytes * fix shadeform disk size
1 parent 93cb879 commit c9c94be

3 files changed

Lines changed: 34 additions & 29 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)

v1/providers/shadeform/instance.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -370,17 +370,18 @@ func (c *ShadeformClient) convertShadeformInstanceToV1Instance(shadeformInstance
370370
diskSize := units.Base2Bytes(shadeformInstance.Configuration.StorageInGb) * units.GiB
371371

372372
instance := &v1.Instance{
373-
Name: shadeformInstance.Name,
374-
CreatedAt: shadeformInstance.CreatedAt,
375-
CloudID: v1.CloudProviderInstanceID(shadeformInstance.Id),
376-
PublicIP: shadeformInstance.Ip,
377-
PublicDNS: shadeformInstance.Ip,
378-
Hostname: hostname,
379-
ImageID: shadeformInstance.Configuration.Os,
380-
InstanceType: instanceType,
381-
DiskSize: diskSize,
382-
SSHUser: shadeformInstance.SshUser,
383-
SSHPort: int(shadeformInstance.SshPort),
373+
Name: shadeformInstance.Name,
374+
CreatedAt: shadeformInstance.CreatedAt,
375+
CloudID: v1.CloudProviderInstanceID(shadeformInstance.Id),
376+
PublicIP: shadeformInstance.Ip,
377+
PublicDNS: shadeformInstance.Ip,
378+
Hostname: hostname,
379+
ImageID: shadeformInstance.Configuration.Os,
380+
InstanceType: instanceType,
381+
DiskSize: diskSize,
382+
DiskSizeBytes: v1.NewBytes(v1.BytesValue(shadeformInstance.Configuration.StorageInGb), v1.Gigabyte),
383+
SSHUser: shadeformInstance.SshUser,
384+
SSHPort: int(shadeformInstance.SshPort),
384385
Status: v1.Status{
385386
LifecycleStatus: lifeCycleStatus,
386387
},

0 commit comments

Comments
 (0)