Skip to content

Commit b0d7055

Browse files
authored
fix: make schemas consistent with API (#348)
In some places the schema definitions are not consistent with the API documentation, for example properties may not have pointer receivers even though they are marked as optional in the documentation. This results in inconsistent JSON when converting `JSON -> Schema -> JSON` (as seen for example in hetznercloud/cli#461, where one output is the exact API response and one is serialized and then converted back to JSON). This PR aims to fix this issue by making the schema definitions consistent.
1 parent b276eeb commit b0d7055

File tree

5 files changed

+64
-64
lines changed

5 files changed

+64
-64
lines changed

hcloud/schema/image.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ type Image struct {
1111
Description string `json:"description"`
1212
ImageSize *float32 `json:"image_size"`
1313
DiskSize float32 `json:"disk_size"`
14-
Created time.Time `json:"created"`
14+
Created *time.Time `json:"created"`
1515
CreatedFrom *ImageCreatedFrom `json:"created_from"`
1616
BoundTo *int64 `json:"bound_to"`
1717
OSFlavor string `json:"os_flavor"`
1818
OSVersion *string `json:"os_version"`
1919
Architecture string `json:"architecture"`
2020
RapidDeploy bool `json:"rapid_deploy"`
2121
Protection ImageProtection `json:"protection"`
22-
Deprecated time.Time `json:"deprecated"`
23-
Deleted time.Time `json:"deleted"`
22+
Deprecated *time.Time `json:"deprecated"`
23+
Deleted *time.Time `json:"deleted"`
2424
Labels map[string]string `json:"labels"`
2525
}
2626

hcloud/schema/iso.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import "time"
44

55
// ISO defines the schema of an ISO image.
66
type ISO struct {
7-
ID int64 `json:"id"`
8-
Name string `json:"name"`
9-
Description string `json:"description"`
10-
Type string `json:"type"`
11-
Architecture *string `json:"architecture"`
12-
Deprecated time.Time `json:"deprecated"`
7+
ID int64 `json:"id"`
8+
Name string `json:"name"`
9+
Description string `json:"description"`
10+
Type string `json:"type"`
11+
Architecture *string `json:"architecture"`
12+
Deprecated *time.Time `json:"deprecated"`
1313
DeprecatableResource
1414
}
1515

hcloud/schema/server_type.go

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type ServerType struct {
1313
Architecture string `json:"architecture"`
1414
IncludedTraffic int64 `json:"included_traffic"`
1515
Prices []PricingServerTypePrice `json:"prices"`
16+
Deprecated bool `json:"deprecated"`
1617
DeprecatableResource
1718
}
1819

hcloud/schema_gen.go

+17
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,17 @@ type converter interface {
149149
ServerTypeFromSchema(schema.ServerType) *ServerType
150150

151151
// goverter:map Pricings Prices
152+
// goverter:map DeprecatableResource.Deprecation Deprecated | isDeprecationNotNil
152153
SchemaFromServerType(*ServerType) schema.ServerType
153154

154155
ImageFromSchema(schema.Image) *Image
155156

156157
SchemaFromImage(*Image) schema.Image
157158

159+
// Needed because of how goverter works internally, see https://github.com/jmattheis/goverter/issues/114
160+
// goverter:map ImageSize | mapZeroFloat32ToNil
161+
intSchemaFromImage(Image) schema.Image
162+
158163
// goverter:ignore Currency
159164
// goverter:ignore VATRate
160165
PriceFromSchema(schema.Price) Price
@@ -906,3 +911,15 @@ func rawSchemaFromErrorDetails(v interface{}) json.RawMessage {
906911
msg, _ := json.Marshal(d)
907912
return msg
908913
}
914+
915+
func mapZeroFloat32ToNil(f float32) *float32 {
916+
fmt.Println(f)
917+
if f == 0 {
918+
return nil
919+
}
920+
return &f
921+
}
922+
923+
func isDeprecationNotNil(d *DeprecationInfo) bool {
924+
return d != nil
925+
}

hcloud/zz_schema.go

+37-55
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)