Skip to content

Commit

Permalink
refactor: replace isStringInSlice with slices.Contains (#674)
Browse files Browse the repository at this point in the history
Remove isStringInSlice in favor of slices.Contains.

Extracted from #622 
Related to #665

Signed-off-by: Marques Johansson <[email protected]>
  • Loading branch information
displague authored May 20, 2024
1 parent e5bf731 commit 3cfe64e
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 15 deletions.
3 changes: 2 additions & 1 deletion equinix/data_source_network_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package equinix
import (
"context"
"fmt"
"slices"
"strings"

"github.com/equinix/terraform-provider-equinix/internal/config"
Expand Down Expand Up @@ -686,7 +687,7 @@ func dataSourceNetworkDeviceRead(ctx context.Context, d *schema.ResourceData, m
return diag.Errorf("cannot fetch primary network device due to '%v'", err)
}

if isStringInSlice(ne.StringValue(primary.Status), []string{ne.DeviceStateDeprovisioning, ne.DeviceStateDeprovisioned}) {
if slices.Contains([]string{ne.DeviceStateDeprovisioning, ne.DeviceStateDeprovisioned}, ne.StringValue(primary.Status)) {
d.SetId("")
return diags
}
Expand Down
14 changes: 3 additions & 11 deletions equinix/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package equinix
import (
"context"
"fmt"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -195,7 +196,7 @@ func configureProvider(ctx context.Context, d *schema.ResourceData, p *schema.Pr

func stringsFound(source []string, target []string) bool {
for i := range source {
if !isStringInSlice(source[i], target) {
if !slices.Contains(target, source[i]) {
return false
}
}
Expand All @@ -204,16 +205,7 @@ func stringsFound(source []string, target []string) bool {

func atLeastOneStringFound(source []string, target []string) bool {
for i := range source {
if isStringInSlice(source[i], target) {
return true
}
}
return false
}

func isStringInSlice(needle string, hay []string) bool {
for i := range hay {
if needle == hay[i] {
if slices.Contains(target, source[i]) {
return true
}
}
Expand Down
5 changes: 3 additions & 2 deletions equinix/resource_ecx_l2_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -714,12 +715,12 @@ func resourceECXL2ConnectionRead(ctx context.Context, d *schema.ResourceData, m
if err != nil {
return diag.Errorf("cannot fetch primary connection due to %v", err)
}
if isStringInSlice(ecx.StringValue(primary.Status), []string{
if slices.Contains([]string{
ecx.ConnectionStatusPendingDelete,
ecx.ConnectionStatusDeprovisioning,
ecx.ConnectionStatusDeprovisioned,
ecx.ConnectionStatusDeleted,
}) {
}, ecx.StringValue(primary.Status)) {
d.SetId("")
return diags
}
Expand Down
3 changes: 2 additions & 1 deletion equinix/resource_network_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"os"
"path/filepath"
"slices"
"time"

"github.com/equinix/terraform-provider-equinix/internal/config"
Expand Down Expand Up @@ -972,7 +973,7 @@ func resourceNetworkDeviceRead(ctx context.Context, d *schema.ResourceData, m in
if err != nil {
return diag.Errorf("cannot fetch primary network device due to %v", err)
}
if isStringInSlice(ne.StringValue(primary.Status), []string{ne.DeviceStateDeprovisioning, ne.DeviceStateDeprovisioned}) {
if slices.Contains([]string{ne.DeviceStateDeprovisioning, ne.DeviceStateDeprovisioned}, ne.StringValue(primary.Status)) {
d.SetId("")
return diags
}
Expand Down

0 comments on commit 3cfe64e

Please sign in to comment.