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

Use Go 1.23 slices.Chunk as a replacement for internal/slices.Chunks, redux #39475

Merged
merged 11 commits into from
Sep 25, 2024
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
2 changes: 1 addition & 1 deletion .go-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.22.7
1.23.1
16 changes: 8 additions & 8 deletions internal/service/autoscaling/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import ( // nosemgrep:ci.semgrep.aws.multiple-service-imports
"errors"
"fmt"
"log"
"slices"
"strconv"
"strings"
"time"
Expand All @@ -34,7 +35,6 @@ import ( // nosemgrep:ci.semgrep.aws.multiple-service-imports
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
"github.com/hashicorp/terraform-provider-aws/internal/sdkv2/types/nullable"
tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
"github.com/hashicorp/terraform-provider-aws/names"
Expand Down Expand Up @@ -1504,7 +1504,7 @@ func resourceGroupUpdate(ctx context.Context, d *schema.ResourceData, meta inter

// API only supports adding or removing 10 at a time.
batchSize := 10
for _, chunk := range tfslices.Chunks(expandTrafficSourceIdentifiers(os.Difference(ns).List()), batchSize) {
for chunk := range slices.Chunk(expandTrafficSourceIdentifiers(os.Difference(ns).List()), batchSize) {
input := &autoscaling.DetachTrafficSourcesInput{
AutoScalingGroupName: aws.String(d.Id()),
TrafficSources: chunk,
Expand All @@ -1521,7 +1521,7 @@ func resourceGroupUpdate(ctx context.Context, d *schema.ResourceData, meta inter
}
}

for _, chunk := range tfslices.Chunks(expandTrafficSourceIdentifiers(ns.Difference(os).List()), batchSize) {
for chunk := range slices.Chunk(expandTrafficSourceIdentifiers(ns.Difference(os).List()), batchSize) {
input := &autoscaling.AttachTrafficSourcesInput{
AutoScalingGroupName: aws.String(d.Id()),
TrafficSources: chunk,
Expand All @@ -1545,7 +1545,7 @@ func resourceGroupUpdate(ctx context.Context, d *schema.ResourceData, meta inter

// API only supports adding or removing 10 at a time.
batchSize := 10
for _, chunk := range tfslices.Chunks(flex.ExpandStringValueSet(os.Difference(ns)), batchSize) {
for chunk := range slices.Chunk(flex.ExpandStringValueSet(os.Difference(ns)), batchSize) {
input := &autoscaling.DetachLoadBalancersInput{
AutoScalingGroupName: aws.String(d.Id()),
LoadBalancerNames: chunk,
Expand All @@ -1562,7 +1562,7 @@ func resourceGroupUpdate(ctx context.Context, d *schema.ResourceData, meta inter
}
}

for _, chunk := range tfslices.Chunks(flex.ExpandStringValueSet(ns.Difference(os)), batchSize) {
for chunk := range slices.Chunk(flex.ExpandStringValueSet(ns.Difference(os)), batchSize) {
input := &autoscaling.AttachLoadBalancersInput{
AutoScalingGroupName: aws.String(d.Id()),
LoadBalancerNames: chunk,
Expand All @@ -1586,7 +1586,7 @@ func resourceGroupUpdate(ctx context.Context, d *schema.ResourceData, meta inter

// API only supports adding or removing 10 at a time.
batchSize := 10
for _, chunk := range tfslices.Chunks(flex.ExpandStringValueSet(os.Difference(ns)), batchSize) {
for chunk := range slices.Chunk(flex.ExpandStringValueSet(os.Difference(ns)), batchSize) {
input := &autoscaling.DetachLoadBalancerTargetGroupsInput{
AutoScalingGroupName: aws.String(d.Id()),
TargetGroupARNs: chunk,
Expand All @@ -1603,7 +1603,7 @@ func resourceGroupUpdate(ctx context.Context, d *schema.ResourceData, meta inter
}
}

for _, chunk := range tfslices.Chunks(flex.ExpandStringValueSet(ns.Difference(os)), batchSize) {
for chunk := range slices.Chunk(flex.ExpandStringValueSet(ns.Difference(os)), batchSize) {
input := &autoscaling.AttachLoadBalancerTargetGroupsInput{
AutoScalingGroupName: aws.String(d.Id()),
TargetGroupARNs: chunk,
Expand Down Expand Up @@ -1864,7 +1864,7 @@ func drainGroup(ctx context.Context, conn *autoscaling.Client, name string, inst
}
}
const batchSize = 50 // API limit.
for _, chunk := range tfslices.Chunks(instanceIDs, batchSize) {
for chunk := range slices.Chunk(instanceIDs, batchSize) {
input := &autoscaling.SetInstanceProtectionInput{
AutoScalingGroupName: aws.String(name),
InstanceIds: chunk,
Expand Down
8 changes: 3 additions & 5 deletions internal/service/connect/routing_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"fmt"
"log"
"slices"
"strings"

"github.com/aws/aws-sdk-go-v2/aws"
Expand All @@ -20,7 +21,6 @@ import (
"github.com/hashicorp/terraform-provider-aws/internal/enum"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
Expand Down Expand Up @@ -361,8 +361,7 @@ func updateRoutingProfileQueueAssociations(ctx context.Context, conn *connect.Cl
// the respective queues based on the diff detected

// disassociate first since Queue and channel type combination cannot be duplicated
chunks := tfslices.Chunks(del, routingProfileQueueAssociationChunkSize)
for _, chunk := range chunks {
for chunk := range slices.Chunk(del, routingProfileQueueAssociationChunkSize) {
var queueReferences []awstypes.RoutingProfileQueueReference
for _, v := range chunk {
if v := v.QueueReference; v != nil {
Expand All @@ -385,8 +384,7 @@ func updateRoutingProfileQueueAssociations(ctx context.Context, conn *connect.Cl
}
}

chunks = tfslices.Chunks(add, routingProfileQueueAssociationChunkSize)
for _, chunk := range chunks {
for chunk := range slices.Chunk(add, routingProfileQueueAssociationChunkSize) {
input := &connect.AssociateRoutingProfileQueuesInput{
InstanceId: aws.String(instanceID),
QueueConfigs: chunk,
Expand Down
4 changes: 2 additions & 2 deletions internal/service/docdb/cluster_parameter_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"log"
"reflect"
"slices"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
Expand All @@ -21,7 +22,6 @@ import (
"github.com/hashicorp/terraform-provider-aws/internal/enum"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
Expand Down Expand Up @@ -226,7 +226,7 @@ func modifyClusterParameterGroupParameters(ctx context.Context, conn *docdb.Clie
clusterParameterGroupMaxParamsBulkEdit = 20
)
// We can only modify 20 parameters at a time, so chunk them until we've got them all.
for _, chunk := range tfslices.Chunks(parameters, clusterParameterGroupMaxParamsBulkEdit) {
for chunk := range slices.Chunk(parameters, clusterParameterGroupMaxParamsBulkEdit) {
input := &docdb.ModifyDBClusterParameterGroupInput{
DBClusterParameterGroupName: aws.String(name),
Parameters: chunk,
Expand Down
6 changes: 3 additions & 3 deletions internal/service/kafka/scram_secret_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"log"
"slices"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/kafka"
Expand All @@ -19,7 +20,6 @@ import (
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
)
Expand Down Expand Up @@ -163,7 +163,7 @@ func findSCRAMSecretsByClusterARN(ctx context.Context, conn *kafka.Client, clust
}

func associateSRAMSecrets(ctx context.Context, conn *kafka.Client, clusterARN string, secretARNs []string) error {
for _, chunk := range tfslices.Chunks(secretARNs, scramSecretBatchSize) {
for chunk := range slices.Chunk(secretARNs, scramSecretBatchSize) {
input := &kafka.BatchAssociateScramSecretInput{
ClusterArn: aws.String(clusterARN),
SecretArnList: chunk,
Expand All @@ -184,7 +184,7 @@ func associateSRAMSecrets(ctx context.Context, conn *kafka.Client, clusterARN st
}

func disassociateSRAMSecrets(ctx context.Context, conn *kafka.Client, clusterARN string, secretARNs []string) error {
for _, chunk := range tfslices.Chunks(secretARNs, scramSecretBatchSize) {
for chunk := range slices.Chunk(secretARNs, scramSecretBatchSize) {
input := &kafka.BatchDisassociateScramSecretInput{
ClusterArn: aws.String(clusterARN),
SecretArnList: chunk,
Expand Down
8 changes: 4 additions & 4 deletions internal/service/lakeformation/lf_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"fmt"
"log"
"slices"
"strings"

"github.com/YakDriver/regexache"
Expand All @@ -20,7 +21,6 @@ import (
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices"
"github.com/hashicorp/terraform-provider-aws/names"
)

Expand Down Expand Up @@ -83,7 +83,7 @@ func resourceLFTagCreate(ctx context.Context, d *schema.ResourceData, meta inter
id := lfTagCreateResourceID(catalogID, tagKey)

i := 0
for _, chunk := range tfslices.Chunks(tagValues.List(), lfTagsValuesMaxBatchSize) {
for chunk := range slices.Chunk(tagValues.List(), lfTagsValuesMaxBatchSize) {
if i == 0 {
input := &lakeformation.CreateLFTagInput{
CatalogId: aws.String(catalogID),
Expand Down Expand Up @@ -169,11 +169,11 @@ func resourceLFTagUpdate(ctx context.Context, d *schema.ResourceData, meta inter

var toAddChunks, toDeleteChunks [][]interface{}
if len(toAdd.List()) > 0 {
toAddChunks = tfslices.Chunks(toAdd.List(), lfTagsValuesMaxBatchSize)
toAddChunks = slices.Collect(slices.Chunk(toAdd.List(), lfTagsValuesMaxBatchSize))
}

if len(toDelete.List()) > 0 {
toDeleteChunks = tfslices.Chunks(toDelete.List(), lfTagsValuesMaxBatchSize)
toDeleteChunks = slices.Collect(slices.Chunk(toDelete.List(), lfTagsValuesMaxBatchSize))
}

for {
Expand Down
2 changes: 1 addition & 1 deletion internal/service/neptune/cluster_parameter_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func modifyClusterParameterGroupParameters(ctx context.Context, conn *neptune.Cl
clusterParameterGroupMaxParamsBulkEdit = 20
)
// We can only modify 20 parameters at a time, so chunk them until we've got them all.
for _, chunk := range tfslices.Chunks(parameters, clusterParameterGroupMaxParamsBulkEdit) {
for chunk := range slices.Chunk(parameters, clusterParameterGroupMaxParamsBulkEdit) {
input := &neptune.ModifyDBClusterParameterGroupInput{
DBClusterParameterGroupName: aws.String(name),
Parameters: chunk,
Expand Down
6 changes: 3 additions & 3 deletions internal/service/neptune/parameter_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"fmt"
"log"
"slices"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
Expand All @@ -20,7 +21,6 @@ import (
"github.com/hashicorp/terraform-provider-aws/internal/enum"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
Expand Down Expand Up @@ -226,7 +226,7 @@ func resourceParameterGroupDelete(ctx context.Context, d *schema.ResourceData, m
}

func addDBParameterGroupParameters(ctx context.Context, conn *neptune.Client, name string, parameters []awstypes.Parameter) error { // We can only modify 20 parameters at a time, so chunk them until we've got them all.
for _, chunk := range tfslices.Chunks(parameters, dbParameterGroupMaxParamsBulkEdit) {
for chunk := range slices.Chunk(parameters, dbParameterGroupMaxParamsBulkEdit) {
input := &neptune.ModifyDBParameterGroupInput{
DBParameterGroupName: aws.String(name),
Parameters: chunk,
Expand All @@ -243,7 +243,7 @@ func addDBParameterGroupParameters(ctx context.Context, conn *neptune.Client, na
}

func delDBParameterGroupParameters(ctx context.Context, conn *neptune.Client, name string, parameters []awstypes.Parameter) error { // We can only modify 20 parameters at a time, so chunk them until we've got them all.
for _, chunk := range tfslices.Chunks(parameters, dbParameterGroupMaxParamsBulkEdit) {
for chunk := range slices.Chunk(parameters, dbParameterGroupMaxParamsBulkEdit) {
input := &neptune.ResetDBParameterGroupInput{
DBParameterGroupName: aws.String(name),
Parameters: chunk,
Expand Down
4 changes: 2 additions & 2 deletions internal/service/rds/cluster_parameter_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func resourceClusterParameterGroupUpdate(ctx context.Context, d *schema.Resource
o, n := d.GetChange(names.AttrParameter)
os, ns := o.(*schema.Set), n.(*schema.Set)

for _, chunk := range tfslices.Chunks(expandParameters(ns.Difference(os).List()), maxParamModifyChunk) {
for chunk := range slices.Chunk(expandParameters(ns.Difference(os).List()), maxParamModifyChunk) {
input := &rds.ModifyDBClusterParameterGroupInput{
DBClusterParameterGroupName: aws.String(d.Id()),
Parameters: chunk,
Expand Down Expand Up @@ -236,7 +236,7 @@ func resourceClusterParameterGroupUpdate(ctx context.Context, d *schema.Resource
}

// Reset parameters that have been removed.
for _, chunk := range tfslices.Chunks(maps.Values(toRemove), maxParamModifyChunk) {
for chunk := range slices.Chunk(maps.Values(toRemove), maxParamModifyChunk) {
input := &rds.ResetDBClusterParameterGroupInput{
DBClusterParameterGroupName: aws.String(d.Id()),
Parameters: chunk,
Expand Down
3 changes: 1 addition & 2 deletions internal/service/route53/zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,7 @@ func deleteAllResourceRecordsFromHostedZone(ctx context.Context, conn *route53.C
const (
chunkSize = 100
)
chunks := tfslices.Chunks(resourceRecordSets, chunkSize)
for _, chunk := range chunks {
for chunk := range slices.Chunk(resourceRecordSets, chunkSize) {
changes := tfslices.ApplyToAll(chunk, func(v awstypes.ResourceRecordSet) awstypes.Change {
return awstypes.Change{
Action: awstypes.ChangeActionDelete,
Expand Down
14 changes: 5 additions & 9 deletions internal/service/ssm/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"log"
"regexp"
"slices"
"strings"
"time"

Expand All @@ -27,7 +28,6 @@ import (
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
itypes "github.com/hashicorp/terraform-provider-aws/internal/types"
Expand Down Expand Up @@ -290,9 +290,7 @@ func resourceDocumentCreate(ctx context.Context, d *schema.ResourceData, meta in
tfMap := flex.ExpandStringValueMap(v.(map[string]interface{}))

if v, ok := tfMap["account_ids"]; ok && v != "" {
chunks := tfslices.Chunks(strings.Split(v, ","), documentPermissionsBatchLimit)

for _, chunk := range chunks {
for chunk := range slices.Chunk(strings.Split(v, ","), documentPermissionsBatchLimit) {
input := &ssm.ModifyDocumentPermissionInput{
AccountIdsToAdd: chunk,
Name: aws.String(d.Id()),
Expand Down Expand Up @@ -426,7 +424,7 @@ func resourceDocumentUpdate(ctx context.Context, d *schema.ResourceData, meta in
}
}

for _, chunk := range tfslices.Chunks(newAccountIDs.Difference(oldAccountIDs), documentPermissionsBatchLimit) {
for chunk := range slices.Chunk(newAccountIDs.Difference(oldAccountIDs), documentPermissionsBatchLimit) {
input := &ssm.ModifyDocumentPermissionInput{
AccountIdsToAdd: chunk,
Name: aws.String(d.Id()),
Expand All @@ -440,7 +438,7 @@ func resourceDocumentUpdate(ctx context.Context, d *schema.ResourceData, meta in
}
}

for _, chunk := range tfslices.Chunks(oldAccountIDs.Difference(newAccountIDs), documentPermissionsBatchLimit) {
for chunk := range slices.Chunk(oldAccountIDs.Difference(newAccountIDs), documentPermissionsBatchLimit) {
input := &ssm.ModifyDocumentPermissionInput{
AccountIdsToRemove: chunk,
Name: aws.String(d.Id()),
Expand Down Expand Up @@ -517,9 +515,7 @@ func resourceDocumentDelete(ctx context.Context, d *schema.ResourceData, meta in
tfMap := flex.ExpandStringValueMap(v.(map[string]interface{}))

if v, ok := tfMap["account_ids"]; ok && v != "" {
chunks := tfslices.Chunks(strings.Split(v, ","), documentPermissionsBatchLimit)

for _, chunk := range chunks {
for chunk := range slices.Chunk(strings.Split(v, ","), documentPermissionsBatchLimit) {
input := &ssm.ModifyDocumentPermissionInput{
AccountIdsToRemove: chunk,
Name: aws.String(d.Id()),
Expand Down
17 changes: 0 additions & 17 deletions internal/slices/slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,23 +100,6 @@ func Any[S ~[]E, E any](s S, f Predicate[E]) bool {
return false
}

// Chunks returns a slice of S, each of the specified size (or less).
func Chunks[S ~[]E, E any](s S, size int) []S {
chunks := make([]S, 0)

for i := 0; i < len(s); i += size {
end := i + size

if end > len(s) {
end = len(s)
}

chunks = append(chunks, s[i:end])
}

return chunks
}

// AppendUnique appends unique (not already in the slice) values to a slice.
func AppendUnique[S ~[]E, E comparable](s S, vs ...E) S {
for _, v := range vs {
Expand Down
Loading
Loading