-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: remove util/slice
and use standard slices
library
#13775
Conversation
util/slice
and use standard slices
library
18d80f1
to
4ff7fd6
Compare
Signed-off-by: Alan Clucas <[email protected]>
4ff7fd6
to
dc011a9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks for simplifying this. I left one question here around having a helper vs an anonymous in-line func for DeleteFunc
return append(ret, slice[i+1:]...) | ||
} | ||
} | ||
return slice |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume that you decided that it wasn't worth keeping this file for a simplified version of this helper that uses DeleteFunc
? i.e. return slices.DeleteFunc(slice, func (x string) bool { return x == element }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I considered it, but didn't do it as:
- It behaves differently from the original (modifying the input slice in place). This is sort of surprising, as it also returns the resulting slice. The
slices
version documents this for us. RemoveString()
only operated on arrays ofstring
s, not generic arrays, so overall encouraging it to stay around and potentially get duplicated to addRemoveMyOtherType()
was worse long term. Possibly genericing it would have been useful, but probably worse as a long term option.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea I considered making it a generic RemoveElement
as well.
Thanks for noting this; I don't have a strong opinion on this but wanted to make sure the rationale was documented since the decision seemed intentional
Motivation
/util/slices
can be replaced by the official slices library added in go 1.21. This library is also the official version ofgolang.org/x/exp/slices
.Replace all uses of these libraries with slices
Modifications
Replace
slice.ContainsString()
withslices.Contains()
. This is very simple.Replace
slice.RemoveString()
withslices.DeleteFunc()
, which involves a small inline boolean comparison function.RemoveString
returned a copy of the slice,DeleteFunc
modifies the slice in place. In the one case where we're using RemoveString to clone the slice (getPodCleanupPatch
),slices.Clone()
it before modifying.Also fix a test name with a spelling mistake.
Verification
Test suite to catch the problems