Skip to content

Commit a4859df

Browse files
Merge pull request #6243 from tongjicoder/dev
refactor: use slices.Contains to simplify code
2 parents 85c709e + 3be29ab commit a4859df

File tree

5 files changed

+11
-30
lines changed

5 files changed

+11
-30
lines changed

pkg/catalog/aws/catalog.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"io"
99
"path"
10+
"slices"
1011
"strings"
1112

1213
"github.com/aws/aws-sdk-go-v2/aws"
@@ -140,10 +141,8 @@ func (c Catalog) ResolvePath(templateName, second string) (string, error) {
140141
}
141142

142143
// check if templateName is already an absolute path to c key
143-
for _, key := range keys {
144-
if key == templateName {
145-
return templateName, nil
146-
}
144+
if slices.Contains(keys, templateName) {
145+
return templateName, nil
147146
}
148147

149148
return "", fmt.Errorf("no such path found: %s%s for keys: %v", second, templateName, keys)

pkg/catalog/aws/catalog_test.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package aws
33
import (
44
"io"
55
"reflect"
6+
"slices"
67
"strings"
78
"testing"
89

@@ -250,13 +251,7 @@ func (m mocks3svc) getAllKeys() ([]string, error) {
250251
}
251252

252253
func (m mocks3svc) downloadKey(name string) (io.ReadCloser, error) {
253-
found := false
254-
for _, key := range m.keys {
255-
if key == name {
256-
found = true
257-
break
258-
}
259-
}
254+
found := slices.Contains(m.keys, name)
260255
if !found {
261256
return nil, errors.New("key not found")
262257
}

pkg/catalog/config/nucleiconfig.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"log"
88
"os"
99
"path/filepath"
10+
"slices"
1011
"strings"
1112

1213
"github.com/projectdiscovery/goflags"
@@ -334,12 +335,7 @@ func (c *Config) copyIgnoreFile() {
334335
// this could be a feature specific to debugging like PPROF or printing stats
335336
// of max host error etc
336337
func (c *Config) IsDebugArgEnabled(arg string) bool {
337-
for _, v := range c.debugArgs {
338-
if v == arg {
339-
return true
340-
}
341-
}
342-
return false
338+
return slices.Contains(c.debugArgs, arg)
343339
}
344340

345341
// parseDebugArgs from string

pkg/input/formats/openapi/examples.go

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

33
import (
44
"fmt"
5+
"slices"
56

67
"github.com/getkin/kin-openapi/openapi3"
78
"github.com/pkg/errors"
@@ -84,13 +85,7 @@ func excludeFromMode(schema *openapi3.Schema) bool {
8485

8586
// isRequired checks whether a key is actually required.
8687
func isRequired(schema *openapi3.Schema, key string) bool {
87-
for _, req := range schema.Required {
88-
if req == key {
89-
return true
90-
}
91-
}
92-
93-
return false
88+
return slices.Contains(schema.Required, key)
9489
}
9590

9691
type cachedSchema struct {

pkg/protocols/http/request_condition.go

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

33
import (
44
"regexp"
5+
"slices"
56
)
67

78
var (
@@ -32,10 +33,5 @@ func (request *Request) NeedsRequestCondition() bool {
3233
}
3334

3435
func checkRequestConditionExpressions(expressions ...string) bool {
35-
for _, expression := range expressions {
36-
if reRequestCondition.MatchString(expression) {
37-
return true
38-
}
39-
}
40-
return false
36+
return slices.ContainsFunc(expressions, reRequestCondition.MatchString)
4137
}

0 commit comments

Comments
 (0)