Skip to content

Commit 7513043

Browse files
committed
fix: string helper cleanup
1 parent a7084d4 commit 7513043

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

pkg/controller/controllerutil/controllerutil.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ type MutateFn func() error
173173
// AddFinalizer accepts a metav1 object and adds the provided finalizer if not present.
174174
func AddFinalizer(o metav1.Object, finalizer string) {
175175
newFinalizers := AddString(o.GetFinalizers(), finalizer)
176-
o.SetFinalizers(append(newFinalizers, finalizer))
176+
o.SetFinalizers(newFinalizers)
177177
}
178178

179179
// AddFinalizerIfPossible tries to convert a runtime object to a metav1 object and add the provided finalizer.
@@ -206,24 +206,22 @@ func RemoveFinalizerIfPossible(o runtime.Object, finalizer string) error {
206206

207207
// AddString returns a []string with s appended if it is not already found in the provided slice.
208208
func AddString(slice []string, s string) []string {
209-
newSlice := make([]string, 0)
210-
for i, item := range slice {
209+
for _, item := range slice {
211210
if item == s {
212-
return append(newSlice, slice[i+1:]...)
211+
return slice
213212
}
214-
newSlice = append(newSlice, item)
215213
}
216214
return append(slice, s)
217215
}
218216

219217
// RemoveString returns a newly created []string that contains all items from slice that are not equal to s.
220218
func RemoveString(slice []string, s string) []string {
221-
newSlice := make([]string, 0)
219+
new := make([]string, 0)
222220
for _, item := range slice {
223221
if item == s {
224222
continue
225223
}
226-
newSlice = append(newSlice, item)
224+
new = append(new, item)
227225
}
228-
return newSlice
226+
return new
229227
}

pkg/controller/controllerutil/controllerutil_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,20 +308,19 @@ var _ = Describe("Controllerutil", func() {
308308
})
309309

310310
Describe("String Helpers", func() {
311-
var testStrings []string
312311
testString := "foobar"
313312
It("should add the string when not present", func() {
314-
testStrings = controllerutil.AddString(testStrings, testString)
313+
testStrings := controllerutil.AddString(nil, testString)
315314
Expect(testStrings).To(Equal([]string{testString}))
316315
})
317316

318317
It("should not add the string when already present", func() {
319-
testStrings = controllerutil.AddString(testStrings, testString)
318+
testStrings := controllerutil.AddString([]string{testString}, testString)
320319
Expect(testStrings).To(Equal([]string{testString}))
321320
})
322321

323322
It("should remove string if present", func() {
324-
testStrings = controllerutil.RemoveString(testStrings, testString)
323+
testStrings := controllerutil.RemoveString([]string{testString}, testString)
325324
Expect(testStrings).To(Equal([]string{}))
326325
})
327326
})

0 commit comments

Comments
 (0)