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

[TechDebt]: Use flex.Compare in the skaff Update method #41512

Open
jar-b opened this issue Feb 21, 2025 · 1 comment
Open

[TechDebt]: Use flex.Compare in the skaff Update method #41512

jar-b opened this issue Feb 21, 2025 · 1 comment
Labels
skaff Issues and pull requested related to the skaff tool technical-debt Addresses areas of the codebase that need refactoring or redesign.

Comments

@jar-b
Copy link
Member

jar-b commented Feb 21, 2025

Description

The internal/framework/flex package contains a Compare function for calculating whether state and plan have differences. The skaff template for Terraform Plugin Framework based resources should utilize this method for comparing state and plan in the update method rather than the current logic which explicitly compares each argument individually.

if !plan.Name.Equal(state.Name) ||
!plan.Description.Equal(state.Description) ||
!plan.ComplexArgument.Equal(state.ComplexArgument) ||
!plan.Type.Equal(state.Type) {

// Calculate compares the plan and state values and returns whether there are changes
func Calculate(ctx context.Context, plan, state any, options ...ChangeOption) (*Results, diag.Diagnostics) {
var diags diag.Diagnostics
opts := NewChangeOptions(options...)
planValue, stateValue := dereferencePointer(reflect.ValueOf(plan)), dereferencePointer(reflect.ValueOf(state))
planType, stateType := planValue.Type(), stateValue.Type()
var ignoredFields []string
result := Results{}
if planType != stateType {
diags.AddError(
"Type mismatch between plan and state",
fmt.Sprintf("plan type: %s, state type: %s", planType.String(), stateType.String()),
)
return &result, diags
}
var hasChanges bool
for i := 0; i < planValue.NumField(); i++ {
fieldName := planType.Field(i).Name
if shouldSkipField(fieldName, opts.IgnoredFields) {
ignoredFields = append(ignoredFields, fieldName)
continue
}
if !fieldExistsInState(stateType, fieldName) {
continue
}
if !implementsAttrValue(planValue.FieldByName(fieldName)) || !implementsAttrValue(stateValue.FieldByName(fieldName)) {
continue
}
planFieldValue := planValue.FieldByName(fieldName).Interface().(attr.Value)
stateFieldValue := stateValue.FieldByName(fieldName).Interface().(attr.Value)
if !planFieldValue.Type(ctx).Equal(stateFieldValue.Type(ctx)) {
continue
}
if !planFieldValue.Equal(stateFieldValue) {
hasChanges = true
} else {
ignoredFields = append(ignoredFields, fieldName)
}
}
result.hasChanges = hasChanges
result.ignoredFieldNames = ignoredFields
return &result, diags
}

References

No response

Would you like to implement a fix?

None

@jar-b jar-b added the skaff Issues and pull requested related to the skaff tool label Feb 21, 2025
Copy link

Community Note

Voting for Prioritization

  • Please vote on this issue by adding a 👍 reaction to the original post to help the community and maintainers prioritize this request.
  • Please see our prioritization guide for information on how we prioritize.
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request.

Volunteering to Work on This Issue

  • If you are interested in working on this issue, please leave a comment.
  • If this would be your first contribution, please review the contribution guide.

@jar-b jar-b added the technical-debt Addresses areas of the codebase that need refactoring or redesign. label Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
skaff Issues and pull requested related to the skaff tool technical-debt Addresses areas of the codebase that need refactoring or redesign.
Projects
None yet
Development

No branches or pull requests

1 participant