Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix small ARM conversion bug #1524

Merged
merged 1 commit into from
May 28, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
12 changes: 12 additions & 0 deletions hack/generator/pkg/astmodel/armconversion/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down