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
5 changes: 5 additions & 0 deletions v1/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ func (b *Bytes) UnmarshalJSON(data []byte) error {
return errors.WrapAndTrace(err)
}

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

unit, err := stringToBytesUnit(bytesJSON.Unit)
if err != nil {
return errors.WrapAndTrace(err)
Expand Down
35 changes: 17 additions & 18 deletions v1/bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ func TestBytesMarshalJSON(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
json, err := json.Marshal(test.bytes)
if test.wantErr != nil {
if err == nil {
t.Fatalf("json.Marshal() error = nil, want %v", test.wantErr)
if err != nil {
if test.wantErr == nil {
t.Fatalf("json.Marshal() error = %v, want nil", err)
}
if !errors.Is(err, test.wantErr) {
t.Fatalf("json.Marshal() error = %v, want %v", err, test.wantErr)
Expand All @@ -81,6 +81,7 @@ func TestBytesUnmarshalJSON(t *testing.T) {
want Bytes
wantErr error
}{
{name: "Empty bytes", json: `{"value":0,"unit":""}`, want: zeroBytes, 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 @@ -101,9 +102,9 @@ func TestBytesUnmarshalJSON(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
var bytes Bytes
err := json.Unmarshal([]byte(test.json), &bytes)
if test.wantErr != nil {
if err == nil {
t.Fatalf("json.Unmarshal() error = nil, want %v", test.wantErr)
if err != nil {
if test.wantErr == nil {
t.Fatalf("json.Unmarshal() error = %v, want nil", err)
}
if !errors.Is(err, test.wantErr) {
t.Fatalf("json.Unmarshal() error = %v, want %v", err, test.wantErr)
Expand Down Expand Up @@ -404,12 +405,11 @@ func TestBytesByteCountInUnitInt64(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
got, err := test.bytes.ByteCountInUnitInt64(test.unit)
if err != nil {
if test.wantErr != nil {
if !errors.Is(err, test.wantErr) {
t.Errorf("Bytes.ByteCountInUnitInt64() = %v, want %v", err, test.wantErr)
}
} else {
t.Errorf("Bytes.ByteCountInUnitInt64() = %v, want %v", err, test.wantErr)
if test.wantErr == nil {
t.Fatalf("Bytes.ByteCountInUnitInt64() error = %v, want nil", err)
}
if !errors.Is(err, test.wantErr) {
t.Fatalf("Bytes.ByteCountInUnitInt64() error = %v, want %v", err, test.wantErr)
}
} else if got != test.want {
t.Errorf("Bytes.ByteCountInUnitInt64() = %v, want %v", got, test.want)
Expand All @@ -434,12 +434,11 @@ func TestBytesByteCountInUnitInt32(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
got, err := test.bytes.ByteCountInUnitInt32(test.unit)
if err != nil {
if test.wantErr != nil {
if !errors.Is(err, test.wantErr) {
t.Errorf("Bytes.ByteCountInUnitInt32() = %v, want %v", err, test.wantErr)
}
} else {
t.Errorf("Bytes.ByteCountInUnitInt32() = %v, want %v", err, test.wantErr)
if test.wantErr == nil {
t.Fatalf("Bytes.ByteCountInUnitInt32() error = %v, want nil", err)
}
if !errors.Is(err, test.wantErr) {
t.Fatalf("Bytes.ByteCountInUnitInt32() error = %v, want %v", err, test.wantErr)
}
} else if got != test.want {
t.Errorf("Bytes.ByteCountInUnitInt32() = %v, want %v", got, test.want)
Expand Down
23 changes: 12 additions & 11 deletions v1/providers/shadeform/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,17 +370,18 @@ func (c *ShadeformClient) convertShadeformInstanceToV1Instance(shadeformInstance
diskSize := units.Base2Bytes(shadeformInstance.Configuration.StorageInGb) * units.GiB

instance := &v1.Instance{
Name: shadeformInstance.Name,
CreatedAt: shadeformInstance.CreatedAt,
CloudID: v1.CloudProviderInstanceID(shadeformInstance.Id),
PublicIP: shadeformInstance.Ip,
PublicDNS: shadeformInstance.Ip,
Hostname: hostname,
ImageID: shadeformInstance.Configuration.Os,
InstanceType: instanceType,
DiskSize: diskSize,
SSHUser: shadeformInstance.SshUser,
SSHPort: int(shadeformInstance.SshPort),
Name: shadeformInstance.Name,
CreatedAt: shadeformInstance.CreatedAt,
CloudID: v1.CloudProviderInstanceID(shadeformInstance.Id),
PublicIP: shadeformInstance.Ip,
PublicDNS: shadeformInstance.Ip,
Hostname: hostname,
ImageID: shadeformInstance.Configuration.Os,
InstanceType: instanceType,
DiskSize: diskSize,
DiskSizeBytes: v1.NewBytes(v1.BytesValue(shadeformInstance.Configuration.StorageInGb), v1.Gigabyte),
SSHUser: shadeformInstance.SshUser,
SSHPort: int(shadeformInstance.SshPort),
Status: v1.Status{
LifecycleStatus: lifeCycleStatus,
},
Expand Down
Loading