Skip to content

Commit

Permalink
fix: make schemas consistent with API (#348)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
phm07 committed Dec 12, 2023
1 parent b276eeb commit b0d7055
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 64 deletions.
6 changes: 3 additions & 3 deletions hcloud/schema/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ type Image struct {
Description string `json:"description"`
ImageSize *float32 `json:"image_size"`
DiskSize float32 `json:"disk_size"`
Created time.Time `json:"created"`
Created *time.Time `json:"created"`
CreatedFrom *ImageCreatedFrom `json:"created_from"`
BoundTo *int64 `json:"bound_to"`
OSFlavor string `json:"os_flavor"`
OSVersion *string `json:"os_version"`
Architecture string `json:"architecture"`
RapidDeploy bool `json:"rapid_deploy"`
Protection ImageProtection `json:"protection"`
Deprecated time.Time `json:"deprecated"`
Deleted time.Time `json:"deleted"`
Deprecated *time.Time `json:"deprecated"`
Deleted *time.Time `json:"deleted"`
Labels map[string]string `json:"labels"`
}

Expand Down
12 changes: 6 additions & 6 deletions hcloud/schema/iso.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import "time"

// ISO defines the schema of an ISO image.
type ISO struct {
ID int64 `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Type string `json:"type"`
Architecture *string `json:"architecture"`
Deprecated time.Time `json:"deprecated"`
ID int64 `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Type string `json:"type"`
Architecture *string `json:"architecture"`
Deprecated *time.Time `json:"deprecated"`
DeprecatableResource
}

Expand Down
1 change: 1 addition & 0 deletions hcloud/schema/server_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type ServerType struct {
Architecture string `json:"architecture"`
IncludedTraffic int64 `json:"included_traffic"`
Prices []PricingServerTypePrice `json:"prices"`
Deprecated bool `json:"deprecated"`
DeprecatableResource
}

Expand Down
17 changes: 17 additions & 0 deletions hcloud/schema_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,17 @@ type converter interface {
ServerTypeFromSchema(schema.ServerType) *ServerType

// goverter:map Pricings Prices
// goverter:map DeprecatableResource.Deprecation Deprecated | isDeprecationNotNil
SchemaFromServerType(*ServerType) schema.ServerType

ImageFromSchema(schema.Image) *Image

SchemaFromImage(*Image) schema.Image

// Needed because of how goverter works internally, see https://github.com/jmattheis/goverter/issues/114
// goverter:map ImageSize | mapZeroFloat32ToNil
intSchemaFromImage(Image) schema.Image

// goverter:ignore Currency
// goverter:ignore VATRate
PriceFromSchema(schema.Price) Price
Expand Down Expand Up @@ -906,3 +911,15 @@ func rawSchemaFromErrorDetails(v interface{}) json.RawMessage {
msg, _ := json.Marshal(d)
return msg
}

func mapZeroFloat32ToNil(f float32) *float32 {
fmt.Println(f)
if f == 0 {
return nil
}
return &f
}

func isDeprecationNotNil(d *DeprecationInfo) bool {
return d != nil
}
92 changes: 37 additions & 55 deletions hcloud/zz_schema.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b0d7055

Please sign in to comment.