From ba6f99984022672347c7a4d3dc6fed28d4882bf0 Mon Sep 17 00:00:00 2001 From: Matthew Christopher Date: Thu, 27 May 2021 17:05:21 -0700 Subject: [PATCH] Fix small ARM conversion bug --- .../convert_from_arm_function_builder.go | 21 ++++++++++++++----- .../pkg/astmodel/armconversion/shared.go | 12 +++++++++++ ..._has_embedded_resource_inside_azure.golden | 12 ++++------- ...est_embedded_resource_removed_azure.golden | 6 ++---- ...rm_test_id_resource_reference_azure.golden | 6 ++---- ..._optional_resource_references_azure.golden | 6 ++---- 6 files changed, 38 insertions(+), 25 deletions(-) diff --git a/hack/generator/pkg/astmodel/armconversion/convert_from_arm_function_builder.go b/hack/generator/pkg/astmodel/armconversion/convert_from_arm_function_builder.go index 0f503bbc21c..4847b1e4be4 100644 --- a/hack/generator/pkg/astmodel/armconversion/convert_from_arm_function_builder.go +++ b/hack/generator/pkg/astmodel/armconversion/convert_from_arm_function_builder.go @@ -94,16 +94,27 @@ func (builder *convertFromARMBuilder) functionDeclaration() *dst.FuncDecl { func (builder *convertFromARMBuilder) functionBodyStatements() []dst.Stmt { var result []dst.Stmt + assertStmts := builder.assertInputTypeIsARM() + + conversionStmts := generateTypeConversionAssignments( + builder.armType, + builder.kubeType, + builder.propertyConversionHandler) + + // No conversions to perform -- some properties might be ignored + if len(removeEmptyStatements(conversionStmts)) == 0 { + return []dst.Stmt{ + astbuilder.ReturnNoError(), + } + } + // perform a type assert and check its results - result = append(result, builder.assertInputTypeIsARM()...) + result = append(result, assertStmts...) // Do all of the assignments for each property result = append( result, - generateTypeConversionAssignments( - builder.armType, - builder.kubeType, - builder.propertyConversionHandler)...) + conversionStmts...) // Return nil error if we make it to the end result = append( diff --git a/hack/generator/pkg/astmodel/armconversion/shared.go b/hack/generator/pkg/astmodel/armconversion/shared.go index bf3a8fbaf94..2ffa73bf2d7 100644 --- a/hack/generator/pkg/astmodel/armconversion/shared.go +++ b/hack/generator/pkg/astmodel/armconversion/shared.go @@ -146,3 +146,15 @@ func NewARMTransformerImpl( populateFromARMFunc) } } + +func removeEmptyStatements(stmts []dst.Stmt) []dst.Stmt { + var result []dst.Stmt + for _, stmt := range stmts { + if _, ok := stmt.(*dst.EmptyStmt); ok { + continue + } + result = append(result, stmt) + } + + return result +} diff --git a/hack/generator/pkg/codegen/testdata/ArmResource/Arm_test_embedded_resource_has_embedded_resource_inside_azure.golden b/hack/generator/pkg/codegen/testdata/ArmResource/Arm_test_embedded_resource_has_embedded_resource_inside_azure.golden index d87be2964bc..92eb92c3b19 100644 --- a/hack/generator/pkg/codegen/testdata/ArmResource/Arm_test_embedded_resource_has_embedded_resource_inside_azure.golden +++ b/hack/generator/pkg/codegen/testdata/ArmResource/Arm_test_embedded_resource_has_embedded_resource_inside_azure.golden @@ -945,10 +945,8 @@ func (bResource *BResource) CreateEmptyARMValue() interface{} { // PopulateFromARM populates a Kubernetes CRD object from an Azure ARM object func (bResource *BResource) PopulateFromARM(owner genruntime.KnownResourceReference, armInput interface{}) error { - typedInput, ok := armInput.(BResourceARM) - if !ok { - return fmt.Errorf("unexpected type supplied for PopulateFromARM() function. Expected BResourceARM, got %T", armInput) - } + + // No error return nil } @@ -983,10 +981,8 @@ func (cResource *CResource) CreateEmptyARMValue() interface{} { // PopulateFromARM populates a Kubernetes CRD object from an Azure ARM object func (cResource *CResource) PopulateFromARM(owner genruntime.KnownResourceReference, armInput interface{}) error { - typedInput, ok := armInput.(CResourceARM) - if !ok { - return fmt.Errorf("unexpected type supplied for PopulateFromARM() function. Expected CResourceARM, got %T", armInput) - } + + // No error return nil } diff --git a/hack/generator/pkg/codegen/testdata/ArmResource/Arm_test_embedded_resource_removed_azure.golden b/hack/generator/pkg/codegen/testdata/ArmResource/Arm_test_embedded_resource_removed_azure.golden index 1629abaa89f..fe65bb7ba84 100644 --- a/hack/generator/pkg/codegen/testdata/ArmResource/Arm_test_embedded_resource_removed_azure.golden +++ b/hack/generator/pkg/codegen/testdata/ArmResource/Arm_test_embedded_resource_removed_azure.golden @@ -635,10 +635,8 @@ func (bResource *BResource) CreateEmptyARMValue() interface{} { // PopulateFromARM populates a Kubernetes CRD object from an Azure ARM object func (bResource *BResource) PopulateFromARM(owner genruntime.KnownResourceReference, armInput interface{}) error { - typedInput, ok := armInput.(BResourceARM) - if !ok { - return fmt.Errorf("unexpected type supplied for PopulateFromARM() function. Expected BResourceARM, got %T", armInput) - } + + // No error return nil } diff --git a/hack/generator/pkg/codegen/testdata/ArmResource/Arm_test_id_resource_reference_azure.golden b/hack/generator/pkg/codegen/testdata/ArmResource/Arm_test_id_resource_reference_azure.golden index 863f0fea1a8..c60be741c55 100644 --- a/hack/generator/pkg/codegen/testdata/ArmResource/Arm_test_id_resource_reference_azure.golden +++ b/hack/generator/pkg/codegen/testdata/ArmResource/Arm_test_id_resource_reference_azure.golden @@ -299,10 +299,8 @@ func (fakeResourceProperties *FakeResourceProperties) CreateEmptyARMValue() inte // PopulateFromARM populates a Kubernetes CRD object from an Azure ARM object func (fakeResourceProperties *FakeResourceProperties) PopulateFromARM(owner genruntime.KnownResourceReference, armInput interface{}) error { - typedInput, ok := armInput.(FakeResourcePropertiesARM) - if !ok { - return fmt.Errorf("unexpected type supplied for PopulateFromARM() function. Expected FakeResourcePropertiesARM, got %T", armInput) - } + + // No error return nil } diff --git a/hack/generator/pkg/codegen/testdata/ArmResource/Arm_test_required_and_optional_resource_references_azure.golden b/hack/generator/pkg/codegen/testdata/ArmResource/Arm_test_required_and_optional_resource_references_azure.golden index 788b90f632b..60d939f39d2 100644 --- a/hack/generator/pkg/codegen/testdata/ArmResource/Arm_test_required_and_optional_resource_references_azure.golden +++ b/hack/generator/pkg/codegen/testdata/ArmResource/Arm_test_required_and_optional_resource_references_azure.golden @@ -297,10 +297,8 @@ func (fakeResourceProperties *FakeResourceProperties) CreateEmptyARMValue() inte // PopulateFromARM populates a Kubernetes CRD object from an Azure ARM object func (fakeResourceProperties *FakeResourceProperties) PopulateFromARM(owner genruntime.KnownResourceReference, armInput interface{}) error { - typedInput, ok := armInput.(FakeResourcePropertiesARM) - if !ok { - return fmt.Errorf("unexpected type supplied for PopulateFromARM() function. Expected FakeResourcePropertiesARM, got %T", armInput) - } + + // No error return nil }