Skip to content
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

Merged
merged 1 commit into from
Oct 24, 2024

Conversation

Joibel
Copy link
Member

@Joibel Joibel commented Oct 17, 2024

Motivation

/util/slices can be replaced by the official slices library added in go 1.21. This library is also the official version of golang.org/x/exp/slices.

Replace all uses of these libraries with slices

Modifications

Replace slice.ContainsString() with slices.Contains(). This is very simple.

Replace slice.RemoveString() with slices.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

@Joibel Joibel changed the title refactor: remove util/slice and use standard slices library refactor: remove util/slice and use standard slices library Oct 17, 2024
@Joibel Joibel marked this pull request as ready for review October 17, 2024 15:58
@Joibel
Copy link
Member Author

Joibel commented Oct 17, 2024

Draft as it collides with #13776/#12862

@Joibel Joibel marked this pull request as draft October 17, 2024 16:56
@Joibel Joibel force-pushed the util-slice-removal branch from 18d80f1 to 4ff7fd6 Compare October 22, 2024 07:55
@Joibel Joibel force-pushed the util-slice-removal branch from 4ff7fd6 to dc011a9 Compare October 22, 2024 08:37
@Joibel
Copy link
Member Author

Joibel commented Oct 22, 2024

Draft as it collides with #13776/#12862

This is now merged and this rebased on top.

@Joibel Joibel marked this pull request as ready for review October 22, 2024 08:41
Copy link

@agilgur5 agilgur5 left a 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

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 }

Copy link
Member Author

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 of strings, not generic arrays, so overall encouraging it to stay around and potentially get duplicated to add RemoveMyOtherType() was worse long term. Possibly genericing it would have been useful, but probably worse as a long term option.

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

@Joibel Joibel merged commit b49e88e into argoproj:main Oct 24, 2024
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants