Skip to content

Commit ad7f47c

Browse files
committed
chore: use slices.Contains method
Signed-off-by: shota3506 <[email protected]>
1 parent 90cfa7e commit ad7f47c

File tree

8 files changed

+20
-55
lines changed

8 files changed

+20
-55
lines changed

config/config.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"math"
66
"net/url"
7+
"slices"
78
"time"
89

910
metricsdk "go.opentelemetry.io/otel/sdk/metric"
@@ -146,10 +147,8 @@ func (c Config) GetPodGCDeleteDelayDuration() time.Duration {
146147
}
147148

148149
func (c Config) ValidateProtocol(inputProtocol string, allowedProtocol []string) error {
149-
for _, protocol := range allowedProtocol {
150-
if inputProtocol == protocol {
151-
return nil
152-
}
150+
if slices.Contains(allowedProtocol, inputProtocol) {
151+
return nil
153152
}
154153
return fmt.Errorf("protocol %s is not allowed", inputProtocol)
155154
}

examples/validator.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,13 @@ import (
44
"fmt"
55
"os"
66
"path/filepath"
7+
"slices"
78
"strings"
89

910
"github.com/xeipuuv/gojsonschema"
1011
"sigs.k8s.io/yaml"
1112
)
1213

13-
// https://stackoverflow.com/questions/10485743/contains-method-for-a-slice
14-
func contains(s []string, e string) bool {
15-
for _, a := range s {
16-
if a == e {
17-
return true
18-
}
19-
}
20-
return false
21-
}
22-
2314
func ValidateArgoYamlRecursively(fromPath string, skipFileNames []string) (map[string][]string, error) {
2415
schemaBytes, err := os.ReadFile("../api/jsonschema/schema.json")
2516
if err != nil {
@@ -34,8 +25,7 @@ func ValidateArgoYamlRecursively(fromPath string, skipFileNames []string) (map[s
3425
if err != nil {
3526
return err
3627
}
37-
if contains(skipFileNames, info.Name()) {
38-
// fmt.Printf("skipping %+v \n", info.Name())
28+
if slices.Contains(skipFileNames, info.Name()) {
3929
return filepath.SkipDir
4030
}
4131
if info.IsDir() {

hack/featuregen/components.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"slices"
45
"strings"
56
)
67

@@ -17,12 +18,7 @@ var validComponents = []string{
1718
}
1819

1920
func isValidComponent(component string) bool {
20-
for _, c := range validComponents {
21-
if c == component {
22-
return true
23-
}
24-
}
25-
return false
21+
return slices.Contains(validComponents, component)
2622
}
2723

2824
func listValidComponents() string {

pkg/apis/workflow/v1alpha1/workflow_types.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2607,12 +2607,7 @@ func (n NodeStatus) GetDuration() time.Duration {
26072607
}
26082608

26092609
func (n NodeStatus) HasChild(childID string) bool {
2610-
for _, nodeID := range n.Children {
2611-
if childID == nodeID {
2612-
return true
2613-
}
2614-
}
2615-
return false
2610+
return slices.Contains(n.Children, childID)
26162611
}
26172612

26182613
// S3Bucket contains the access information required for interfacing with an S3 bucket
@@ -3215,12 +3210,7 @@ func (tmpl *Template) IsLeaf() bool {
32153210
}
32163211

32173212
func (tmpl *Template) IsMainContainerName(containerName string) bool {
3218-
for _, c := range tmpl.GetMainContainerNames() {
3219-
if c == containerName {
3220-
return true
3221-
}
3222-
}
3223-
return false
3213+
return slices.Contains(tmpl.GetMainContainerNames(), containerName)
32243214
}
32253215

32263216
func (tmpl *Template) GetMainContainerNames() []string {

workflow/artifacts/oss/oss.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"math"
99
"os"
1010
"path/filepath"
11+
"slices"
1112
"strings"
1213
"time"
1314

@@ -404,10 +405,8 @@ func isTransientOSSErr(ctx context.Context, err error) bool {
404405
return true
405406
}
406407
if ossErr, ok := err.(oss.ServiceError); ok {
407-
for _, transientErrCode := range ossTransientErrorCodes {
408-
if ossErr.Code == transientErrCode {
409-
return true
410-
}
408+
if slices.Contains(ossTransientErrorCodes, ossErr.Code) {
409+
return true
411410
}
412411
}
413412
return false

workflow/controller/operator.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3583,11 +3583,9 @@ func (woc *wfOperationCtx) addChildNode(ctx context.Context, parent string, chil
35833583
if err != nil {
35843584
woc.log.WithPanic().WithField("nodeID", parentID).Error(ctx, "was unable to obtain node for nodeID")
35853585
}
3586-
for _, nodeID := range node.Children {
3587-
if childID == nodeID {
3588-
// already exists
3589-
return
3590-
}
3586+
if slices.Contains(node.Children, childID) {
3587+
// already exists
3588+
return
35913589
}
35923590
node.Children = append(node.Children, childID)
35933591
woc.wf.Status.Nodes.Set(ctx, parentID, *node)

workflow/controller/workflowpod_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"path"
88
"path/filepath"
9+
"slices"
910
"strconv"
1011
"testing"
1112
"time"
@@ -812,10 +813,8 @@ func TestOutOfCluster(t *testing.T) {
812813
verifyKubeConfigVolume := func(ctr apiv1.Container, volName, mountPath string) {
813814
for _, vol := range ctr.VolumeMounts {
814815
if vol.Name == volName && vol.MountPath == mountPath {
815-
for _, arg := range ctr.Args {
816-
if arg == fmt.Sprintf("--kubeconfig=%s", mountPath) {
817-
return
818-
}
816+
if slices.Contains(ctr.Args, fmt.Sprintf("--kubeconfig=%s", mountPath)) {
817+
return
819818
}
820819
}
821820
}

workflow/validate/validate.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"reflect"
88
"regexp"
9+
"slices"
910
"strconv"
1011
"strings"
1112
"time"
@@ -914,14 +915,7 @@ func validateArgumentsValues(prefix string, arguments wfv1.Arguments, allowEmpty
914915
}
915916
return errors.Errorf(errors.CodeBadRequest, "%s%s.value is required", prefix, param.Name)
916917
}
917-
valueSpecifiedInEnumList := false
918-
for _, enum := range param.Enum {
919-
if enum == *param.Value {
920-
valueSpecifiedInEnumList = true
921-
break
922-
}
923-
}
924-
if !valueSpecifiedInEnumList {
918+
if !slices.Contains(param.Enum, *param.Value) {
925919
return errors.Errorf(errors.CodeBadRequest, "%s%s.value should be present in %s%s.enum list", prefix, param.Name, prefix, param.Name)
926920
}
927921
}

0 commit comments

Comments
 (0)