diff --git a/hcloud/schema/iso.go b/hcloud/schema/iso.go index 1160cff8..4cf48cc1 100644 --- a/hcloud/schema/iso.go +++ b/hcloud/schema/iso.go @@ -1,15 +1,12 @@ package schema -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"` DeprecatableResource } diff --git a/hcloud/schema_gen.go b/hcloud/schema_gen.go index 37deeee3..1008bddd 100644 --- a/hcloud/schema_gen.go +++ b/hcloud/schema_gen.go @@ -107,6 +107,12 @@ type converter interface { ISOFromSchema(schema.ISO) *ISO + // We cannot use goverter settings when mapping a struct to a struct pointer + // See [converter.ISOFromSchema] + // See https://github.com/jmattheis/goverter/issues/114 + // goverter:map DeprecatableResource.Deprecation.UnavailableAfter Deprecated + intISOFromSchema(schema.ISO) ISO + SchemaFromISO(*ISO) schema.ISO LocationFromSchema(schema.Location) *Location diff --git a/hcloud/schema_test.go b/hcloud/schema_test.go index 80ff9a58..0ff082b9 100644 --- a/hcloud/schema_test.go +++ b/hcloud/schema_test.go @@ -249,22 +249,38 @@ func TestPrimaryIPSchema(t *testing.T) { } func TestISOSchema(t *testing.T) { - data := []byte(`{ - "id": 4711, - "name": "FreeBSD-11.0-RELEASE-amd64-dvd1", - "description": "FreeBSD 11.0 x64", - "type": "public", - "architecture": "x86", - "deprecated": "2018-02-28T00:00:00+00:00" - }`) - - var s schema.ISO - assert.NoError(t, json.Unmarshal(data, &s)) + for name, tc := range map[string]string{ + "without deprecation": `{ + "id": 4711, + "name": "FreeBSD-11.0-RELEASE-amd64-dvd1", + "description": "FreeBSD 11.0 x64", + "type": "public", + "architecture": "x86" + }`, + "with deprecation": `{ + "id": 4711, + "name": "FreeBSD-11.0-RELEASE-amd64-dvd1", + "description": "FreeBSD 11.0 x64", + "type": "public", + "architecture": "x86", + "deprecation": { + "announced": "2018-01-28T00:00:00+00:00", + "unavailable_after": "2018-04-28T00:00:00+00:00" + }, + "deprecated": "2018-04-28T00:00:00+00:00" + }`, + } { + t.Run(name, func(t *testing.T) { + data := []byte(tc) + var s schema.ISO + assert.NoError(t, json.Unmarshal(data, &s)) - assert.Equal(t, s, SchemaFromISO(ISOFromSchema(s))) + assert.Equal(t, s, SchemaFromISO(ISOFromSchema(s))) - iso := ISOFromSchema(s) - assert.Equal(t, iso, ISOFromSchema(SchemaFromISO(iso))) + iso := ISOFromSchema(s) + assert.Equal(t, iso, ISOFromSchema(SchemaFromISO(iso))) + }) + } } func TestDatacenterSchema(t *testing.T) { diff --git a/hcloud/zz_schema.go b/hcloud/zz_schema.go index 82aa3cef..a908ab36 100755 --- a/hcloud/zz_schema.go +++ b/hcloud/zz_schema.go @@ -157,19 +157,7 @@ func (c *converterImpl) FloatingIPFromSchema(source schema.FloatingIP) *Floating return &hcloudFloatingIP } func (c *converterImpl) ISOFromSchema(source schema.ISO) *ISO { - var hcloudISO ISO - hcloudISO.ID = source.ID - hcloudISO.Name = source.Name - hcloudISO.Description = source.Description - hcloudISO.Type = ISOType(source.Type) - var pHcloudArchitecture *Architecture - if source.Architecture != nil { - hcloudArchitecture := Architecture(*source.Architecture) - pHcloudArchitecture = &hcloudArchitecture - } - hcloudISO.Architecture = pHcloudArchitecture - hcloudISO.Deprecated = c.pTimeTimeToTimeTime(source.Deprecated) - hcloudISO.DeprecatableResource = c.schemaDeprecatableResourceToHcloudDeprecatableResource(source.DeprecatableResource) + hcloudISO := c.intISOFromSchema(source) return &hcloudISO } func (c *converterImpl) ImageFromSchema(source schema.Image) *Image { @@ -696,7 +684,6 @@ func (c *converterImpl) SchemaFromISO(source *ISO) schema.ISO { pString = &xstring } schemaISO2.Architecture = pString - schemaISO2.Deprecated = timeToTimePtr((*source).Deprecated) schemaISO2.DeprecatableResource = c.hcloudDeprecatableResourceToSchemaDeprecatableResource((*source).DeprecatableResource) schemaISO = schemaISO2 } @@ -1590,6 +1577,26 @@ func (c *converterImpl) hcloudVolumeProtectionToSchemaVolumeProtection(source Vo schemaVolumeProtection.Delete = source.Delete return schemaVolumeProtection } +func (c *converterImpl) intISOFromSchema(source schema.ISO) ISO { + var hcloudISO ISO + hcloudISO.ID = source.ID + hcloudISO.Name = source.Name + hcloudISO.Description = source.Description + hcloudISO.Type = ISOType(source.Type) + var pHcloudArchitecture *Architecture + if source.Architecture != nil { + hcloudArchitecture := Architecture(*source.Architecture) + pHcloudArchitecture = &hcloudArchitecture + } + hcloudISO.Architecture = pHcloudArchitecture + var pTimeTime *time.Time + if source.DeprecatableResource.Deprecation != nil { + pTimeTime = &source.DeprecatableResource.Deprecation.UnavailableAfter + } + hcloudISO.Deprecated = c.pTimeTimeToTimeTime(pTimeTime) + hcloudISO.DeprecatableResource = c.schemaDeprecatableResourceToHcloudDeprecatableResource(source.DeprecatableResource) + return hcloudISO +} func (c *converterImpl) intSchemaFromImage(source Image) schema.Image { var schemaImage schema.Image schemaImage.ID = source.ID @@ -1675,7 +1682,6 @@ func (c *converterImpl) pHcloudISOToPSchemaISO(source *ISO) *schema.ISO { pString = &xstring } schemaISO.Architecture = pString - schemaISO.Deprecated = timeToTimePtr((*source).Deprecated) schemaISO.DeprecatableResource = c.hcloudDeprecatableResourceToSchemaDeprecatableResource((*source).DeprecatableResource) pSchemaISO = &schemaISO } @@ -2046,19 +2052,7 @@ func (c *converterImpl) pSchemaFirewallResourceServerToPHcloudFirewallResourceSe func (c *converterImpl) pSchemaISOToPHcloudISO(source *schema.ISO) *ISO { var pHcloudISO *ISO if source != nil { - var hcloudISO ISO - hcloudISO.ID = (*source).ID - hcloudISO.Name = (*source).Name - hcloudISO.Description = (*source).Description - hcloudISO.Type = ISOType((*source).Type) - var pHcloudArchitecture *Architecture - if (*source).Architecture != nil { - hcloudArchitecture := Architecture(*(*source).Architecture) - pHcloudArchitecture = &hcloudArchitecture - } - hcloudISO.Architecture = pHcloudArchitecture - hcloudISO.Deprecated = c.pTimeTimeToTimeTime((*source).Deprecated) - hcloudISO.DeprecatableResource = c.schemaDeprecatableResourceToHcloudDeprecatableResource((*source).DeprecatableResource) + hcloudISO := c.intISOFromSchema((*source)) pHcloudISO = &hcloudISO } return pHcloudISO