Skip to content

Commit f7c929b

Browse files
committed
refactor: simplify some implementations by modern go features
Signed-off-by: chlins <[email protected]>
1 parent b647032 commit f7c929b

File tree

75 files changed

+161
-289
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+161
-289
lines changed

src/common/dao/pgsql_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestMaxOpenConns(t *testing.T) {
2727

2828
queryNum := 200
2929
results := make([]bool, queryNum)
30-
for i := 0; i < queryNum; i++ {
30+
for i := range queryNum {
3131
wg.Add(1)
3232
go func(i int) {
3333
defer wg.Done()

src/common/dao/testutils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func ArrayEqual(arrayA, arrayB []int) bool {
142142
return false
143143
}
144144
size := len(arrayA)
145-
for i := 0; i < size; i++ {
145+
for i := range size {
146146
if arrayA[i] != arrayB[i] {
147147
return false
148148
}

src/common/rbac/project/evaluator_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func BenchmarkProjectEvaluator(b *testing.B) {
119119
resource := NewNamespace(public.ProjectID).Resource(rbac.ResourceRepository)
120120

121121
b.ResetTimer()
122-
for i := 0; i < b.N; i++ {
122+
for b.Loop() {
123123
evaluator.HasPermission(context.TODO(), resource, rbac.ActionPull)
124124
}
125125
}

src/common/utils/utils.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func GenerateRandomStringWithLen(length int) string {
7575
if err != nil {
7676
log.Warningf("Error reading random bytes: %v", err)
7777
}
78-
for i := 0; i < length; i++ {
78+
for i := range length {
7979
result[i] = chars[int(result[i])%l]
8080
}
8181
return string(result)
@@ -337,13 +337,3 @@ func MostMatchSorter(a, b string, matchWord string) bool {
337337
func IsLocalPath(path string) bool {
338338
return len(path) == 0 || (strings.HasPrefix(path, "/") && !strings.HasPrefix(path, "//"))
339339
}
340-
341-
// StringInSlice check if the string is in the slice
342-
func StringInSlice(str string, slice []string) bool {
343-
for _, s := range slice {
344-
if s == str {
345-
return true
346-
}
347-
}
348-
return false
349-
}

src/controller/artifact/annotation/v1alpha1.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ func parseV1alpha1SkipList(artifact *artifact.Artifact, manifest *v1.Manifest) {
6666
skipListAnnotationKey := fmt.Sprintf("%s.%s.%s", AnnotationPrefix, V1alpha1, SkipList)
6767
skipList, ok := manifest.Config.Annotations[skipListAnnotationKey]
6868
if ok {
69-
skipKeyList := strings.Split(skipList, ",")
70-
for _, skipKey := range skipKeyList {
69+
for skipKey := range strings.SplitSeq(skipList, ",") {
7170
delete(metadata, skipKey)
7271
}
7372
artifact.ExtraAttrs = metadata

src/controller/artifact/processor/cnai/parser/util_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func TestAddNode(t *testing.T) {
156156
// Verify the path exists.
157157
current := root
158158
parts := filepath.Clean(tt.path)
159-
for _, part := range strings.Split(parts, string(filepath.Separator)) {
159+
for part := range strings.SplitSeq(parts, string(filepath.Separator)) {
160160
if part == "" {
161161
continue
162162
}

src/controller/blob/controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ func (suite *ControllerTestSuite) TestGet() {
232232

233233
func (suite *ControllerTestSuite) TestSync() {
234234
var references []distribution.Descriptor
235-
for i := 0; i < 5; i++ {
235+
for i := range 5 {
236236
references = append(references, distribution.Descriptor{
237237
MediaType: fmt.Sprintf("media type %d", i),
238238
Digest: suite.Digest(),

src/controller/config/controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func maxValueLimitedByLength(length int) int64 {
206206
var value int64
207207
// the times for multiple, should *10 for every time
208208
times := 1
209-
for i := 0; i < length; i++ {
209+
for range length {
210210
value = value + int64(9*times)
211211
times = times * 10
212212
}

src/controller/event/handler/webhook/scan/scan.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func constructScanImagePayload(ctx context.Context, event *event.ScanImageEvent,
129129
// Wait for reasonable time to make sure the report is ready
130130
// Interval=500ms and total time = 5s
131131
// If the report is still not ready in the total time, then failed at then
132-
for i := 0; i < 10; i++ {
132+
for range 10 {
133133
// First check in case it is ready
134134
if re, err := scan.DefaultController.GetReport(ctx, art, []string{v1.MimeTypeNativeReport, v1.MimeTypeGenericVulnerabilityReport}); err == nil {
135135
if len(re) > 0 && len(re[0].Report) > 0 {

src/controller/health/controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (c *controller) GetHealth(_ context.Context) *OverallHealthStatus {
4848
for name, checker := range registry {
4949
go check(name, checker, timeout, ch)
5050
}
51-
for i := 0; i < len(registry); i++ {
51+
for range len(registry) {
5252
componentStatus := <-ch
5353
if len(componentStatus.Error) != 0 {
5454
isHealthy = false

0 commit comments

Comments
 (0)