Skip to content

Commit

Permalink
ci(lint): add prealloc
Browse files Browse the repository at this point in the history
Add https://github.com/alexkohler/prealloc to find slice declarations
that could potentially be pre-allocated and address linter errors.
  • Loading branch information
Roman Inflianskas committed Feb 3, 2024
1 parent 4f0d554 commit 73e7040
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 37 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ linters:
- unused
- stylecheck
- errorlint
- prealloc

issues:
exclude:
Expand Down
8 changes: 5 additions & 3 deletions internal/schemautil/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ func StringSliceToInterfaceSlice(s []string) []interface{} {
}

func SetTagsTerraformProperties(t map[string]string) []map[string]interface{} {
var tags []map[string]interface{}
tags := make([]map[string]interface{}, len(t))
var i int
for k, v := range t {
tags = append(tags, map[string]interface{}{
tags[i] = map[string]interface{}{
"key": k,
"value": v,
})
}
i++
}

return tags
Expand Down
6 changes: 3 additions & 3 deletions internal/schemautil/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -793,9 +793,9 @@ func copyServicePropertiesFromAPIResponseToTerraform(
}

func FlattenServiceComponents(r *aiven.Service) []map[string]interface{} {
var components []map[string]interface{}
components := make([]map[string]interface{}, len(r.Components))

for _, c := range r.Components {
for i, c := range r.Components {
component := map[string]interface{}{
"component": c.Component,
"host": c.Host,
Expand All @@ -808,7 +808,7 @@ func FlattenServiceComponents(r *aiven.Service) []map[string]interface{} {
"ssl": PointerValueOrDefault(c.Ssl, true),
"usage": c.Usage,
}
components = append(components, component)
components[i] = component
}

return components
Expand Down
2 changes: 1 addition & 1 deletion internal/schemautil/userconfig/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func representationToMap(schemaType SchemaType, representation []byte) (map[stri

// TerraformTypes converts schema representation types to Terraform types.
func TerraformTypes(types []string) ([]string, []string, error) {
var terraformTypes, aivenTypes []string
var terraformTypes, aivenTypes []string // nolint:prealloc

for _, typeValue := range types {
switch typeValue {
Expand Down
16 changes: 8 additions & 8 deletions internal/sdkprovider/service/flink/flink_application_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,13 @@ func resourceFlinkApplicationVersionCreate(ctx context.Context, d *schema.Resour

// expandFlinkApplicationVersionSourcesOrSinks expands the sources or sinks from the Terraform schema to the Aiven API.
func expandFlinkApplicationVersionSourcesOrSinks(sources []interface{}) []aiven.FlinkApplicationVersionRelation {
var result []aiven.FlinkApplicationVersionRelation
for _, source := range sources {
result := make([]aiven.FlinkApplicationVersionRelation, len(sources))
for i, source := range sources {
sourceMap := source.(map[string]interface{})
result = append(result, aiven.FlinkApplicationVersionRelation{
result[i] = aiven.FlinkApplicationVersionRelation{
CreateTable: sourceMap["create_table"].(string),
IntegrationID: sourceMap["integration_id"].(string),
})
}
}

return result
Expand Down Expand Up @@ -325,12 +325,12 @@ func resourceFlinkApplicationVersionRead(ctx context.Context, d *schema.Resource

// flattenFlinkApplicationVersionSourcesOrSinks is a helper function to flatten the sources and sinks fields.
func flattenFlinkApplicationVersionSourcesOrSinks(sources []aiven.FlinkApplicationVersionRelation) []map[string]interface{} {
var result []map[string]interface{}
for _, source := range sources {
result = append(result, map[string]interface{}{
result := make([]map[string]interface{}, len(sources))
for i, source := range sources {
result[i] = map[string]interface{}{
"create_table": source.CreateTable,
"integration_id": source.IntegrationID,
})
}
}

return result
Expand Down
6 changes: 3 additions & 3 deletions internal/sdkprovider/service/kafka/kafka_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,15 @@ func kafkaConnectorConfigNameShouldNotBeEmpty() func(ctx context.Context, oldVal
}

func flattenKafkaConnectorTasks(r *aiven.KafkaConnector) []map[string]interface{} {
var tasks []map[string]interface{}
tasks := make([]map[string]interface{}, len(r.Tasks))

for _, taskS := range r.Tasks {
for i, taskS := range r.Tasks {
task := map[string]interface{}{
"connector": taskS.Connector,
"task": taskS.Task,
}

tasks = append(tasks, task)
tasks[i] = task
}

return tasks
Expand Down
9 changes: 5 additions & 4 deletions internal/sdkprovider/service/kafkatopic/kafka_topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,15 +335,16 @@ func resourceKafkaTopicCreate(ctx context.Context, d *schema.ResourceData, m int
}

func getTags(d *schema.ResourceData) []aiven.KafkaTopicTag {
var tags []aiven.KafkaTopicTag
for _, tagD := range d.Get("tag").(*schema.Set).List() {
tagSet := d.Get("tag").(*schema.Set)
tags := make([]aiven.KafkaTopicTag, tagSet.Len())
for i, tagD := range tagSet.List() {
tagM := tagD.(map[string]interface{})
tag := aiven.KafkaTopicTag{
Key: tagM["key"].(string),
Value: tagM["value"].(string),
}

tags = append(tags, tag)
tags[i] = tag
}

return tags
Expand Down Expand Up @@ -463,7 +464,7 @@ func resourceKafkaTopicReadDatasource(ctx context.Context, d *schema.ResourceDat
}

func flattenKafkaTopicTags(list []aiven.KafkaTopicTag) []map[string]interface{} {
var tags []map[string]interface{}
tags := make([]map[string]interface{}, 0, len(list))
for _, tagS := range list {
tags = append(tags, map[string]interface{}{
"key": tagS.Key,
Expand Down
6 changes: 3 additions & 3 deletions internal/sdkprovider/service/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,9 +426,9 @@ func contactEmailListForTerraform(d *schema.ResourceData, field string, contactE
return nil
}

var results []string
for _, contactEmail := range contactEmails {
results = append(results, contactEmail.Email)
results := make([]string, len(contactEmails))
for i, contactEmail := range contactEmails {
results[i] = contactEmail.Email
}

return d.Set(field, results)
Expand Down
16 changes: 10 additions & 6 deletions internal/sdkprovider/service/vpc/aws_privatelink.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@ func ResourceAWSPrivatelink() *schema.Resource {
func resourceAWSPrivatelinkCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*aiven.Client)

var principals []string
var project = d.Get("project").(string)
var serviceName = d.Get("service_name").(string)

for _, p := range d.Get("principals").(*schema.Set).List() {
principals = append(principals, p.(string))
var principalsSet = d.Get("principals").(*schema.Set)
principals := make([]string, principalsSet.Len())

for i, p := range principalsSet.List() {
principals[i] = p.(string)
}

_, err := client.AWSPrivatelink.Create(
Expand Down Expand Up @@ -130,9 +132,11 @@ func resourceAWSPrivatelinkUpdate(ctx context.Context, d *schema.ResourceData, m
return diag.FromErr(err)
}

var principals []string
for _, p := range d.Get("principals").(*schema.Set).List() {
principals = append(principals, p.(string))
principalsSet := d.Get("principals").(*schema.Set)
principals := make([]string, principalsSet.Len())

for i, p := range principalsSet.List() {
principals[i] = p.(string)
}

_, err = client.AWSPrivatelink.Update(
Expand Down
16 changes: 10 additions & 6 deletions internal/sdkprovider/service/vpc/azure_privatelink.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,14 @@ func ResourceAzurePrivatelink() *schema.Resource {
func resourceAzurePrivatelinkCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*aiven.Client)

var subscriptionIDs []string
var project = d.Get("project").(string)
var serviceName = d.Get("service_name").(string)

for _, s := range d.Get("user_subscription_ids").(*schema.Set).List() {
subscriptionIDs = append(subscriptionIDs, s.(string))
var subscriptionIDsSet = d.Get("user_subscription_ids").(*schema.Set)
subscriptionIDs := make([]string, subscriptionIDsSet.Len())

for i, s := range subscriptionIDsSet.List() {
subscriptionIDs[i] = s.(string)
}

_, err := client.AzurePrivatelink.Create(
Expand Down Expand Up @@ -141,14 +143,16 @@ func resourceAzurePrivatelinkRead(ctx context.Context, d *schema.ResourceData, m
func resourceAzurePrivatelinkUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*aiven.Client)

var subscriptionIDs []string
project, serviceName, err := schemautil.SplitResourceID2(d.Id())
if err != nil {
return diag.FromErr(err)
}

for _, s := range d.Get("user_subscription_ids").(*schema.Set).List() {
subscriptionIDs = append(subscriptionIDs, s.(string))
var subscriptionIDsSet = d.Get("user_subscription_ids").(*schema.Set)
subscriptionIDs := make([]string, subscriptionIDsSet.Len())

for i, s := range subscriptionIDsSet.List() {
subscriptionIDs[i] = s.(string)
}

_, err = client.AzurePrivatelink.Update(
Expand Down

0 comments on commit 73e7040

Please sign in to comment.