* sort the list of ACLs for MongoDBFlex * Fix and test other cases of ListAttribute ordering * fix linting * revert sorting changes, introduce new reconcilestrlist function * merge main * Fix rabbitmq * fix segmentation fault * Improve testing * pass context to mapfields, minor name fixes
62 lines
1.7 KiB
Go
62 lines
1.7 KiB
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
|
|
|
|
"github.com/hashicorp/terraform-plugin-framework/types"
|
|
)
|
|
|
|
// ReconcileStringSlices reconciles two string lists by removing elements from the
|
|
// first list that are not in the second list and appending elements from the
|
|
// second list that are not in the first list.
|
|
// This preserves the order of the elements in the first list that are also in
|
|
// the second list, which is useful when using ListAttributes in Terraform.
|
|
// The source of truth for the order is the first list and the source of truth for the content is the second list.
|
|
func ReconcileStringSlices(list1, list2 []string) []string {
|
|
// Create a copy of list1 to avoid modifying the original list
|
|
list1Copy := append([]string{}, list1...)
|
|
|
|
// Create a map to quickly check if an element is in list2
|
|
inList2 := make(map[string]bool)
|
|
for _, elem := range list2 {
|
|
inList2[elem] = true
|
|
}
|
|
|
|
// Remove elements from list1Copy that are not in list2
|
|
i := 0
|
|
for _, elem := range list1Copy {
|
|
if inList2[elem] {
|
|
list1Copy[i] = elem
|
|
i++
|
|
}
|
|
}
|
|
list1Copy = list1Copy[:i]
|
|
|
|
// Append elements to list1Copy that are in list2 but not in list1Copy
|
|
inList1 := make(map[string]bool)
|
|
for _, elem := range list1Copy {
|
|
inList1[elem] = true
|
|
}
|
|
for _, elem := range list2 {
|
|
if !inList1[elem] {
|
|
list1Copy = append(list1Copy, elem)
|
|
}
|
|
}
|
|
|
|
return list1Copy
|
|
}
|
|
|
|
func ListValuetoStringSlice(list basetypes.ListValue) ([]string, error) {
|
|
result := []string{}
|
|
for _, el := range list.Elements() {
|
|
elStr, ok := el.(types.String)
|
|
if !ok {
|
|
return result, fmt.Errorf("expected record to be of type %T, got %T", types.String{}, elStr)
|
|
}
|
|
result = append(result, elStr.ValueString())
|
|
}
|
|
|
|
return result, nil
|
|
}
|