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

Replace strings.SplitN with strings.Cut #21185

Merged
merged 1 commit into from
Jan 11, 2024
Merged

Replace strings.SplitN with strings.Cut #21185

merged 1 commit into from
Jan 11, 2024

Conversation

serprex
Copy link
Contributor

@serprex serprex commented Jan 6, 2024

Cut is a cleaner & more performant api relative to SplitN(_, _, 2) added in go 1.18

Previously applied this refactoring to buildah: containers/buildah#5239

Does this PR introduce a user-facing change?

None

@github-actions github-actions bot added machine kind/api-change Change to remote API; merits scrutiny labels Jan 6, 2024
@serprex
Copy link
Contributor Author

serprex commented Jan 6, 2024

@rhatdan did something similar

if len(split) != 2 {
return toReturn, nil, nil, errors.New("must provide a path to a namespace when specifying \"ns:\"")
}
_, value, _ := strings.Cut(ns, ":")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed this code to preserve existing behavior, the split always works because of HasPrefix, but maybe error message was intended for len(value) == 0

default:
return false, fmt.Errorf("'U' or 'chown' must be set to true or false, instead received %q: %w", kv[1], errOptionArg)
}
func validChownFlag(value string) (bool, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleaned up this function quite a bit, going as far as not bothering to reparse k=v by changing function signature

Copy link

Ephemeral COPR build failed. @containers/packit-build please check.

@rhatdan
Copy link
Member

rhatdan commented Jan 8, 2024

@Luap99 @giuseppe PTAL

Copy link
Member

@edsantiago edsantiago left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work, but a little on the impossible side to review, so I may be submitting drive-by comments over the course of today. Neither of these are blockers.

split := strings.SplitN(label, "=", 2)
if split[0] == "" {
key, value, _ := strings.Cut(label, "=")
if key == "" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question, but not for addressing in your PR: why is this null-key check not being done everywhere?

@mheon
Copy link
Member

mheon commented Jan 8, 2024

Test failures look unrelated

Copy link
Member

@edsantiago edsantiago left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still not finished. The validChownFlag() cleanup is lovely, but it took me a long time to understand how it ever worked, because I scrolled linearly and did not see the changed function signature until much later. This is a small block of comments all relating to its unit tests.

Copy link
Member

@edsantiago edsantiago left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WHEW! First review pass complete. (Breaking with my custom, there will be no second pass).

LGTM modulo my prior feedback. Most are suggestions/questions; the chown test ones are blockers.

Again: really, really nice work. You took time to evaluate each instance, give meaningful variable names, and perform extra cleanup when necessary. Thank you.

Copy link
Member

@edsantiago edsantiago left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM other than ParseNamespace() and ParseUserNamespace() which I will defer to someone more go-savvy than me.

One note: you will need to squash your commits before merging, but for the benefit of anyone re-reviewing please do not squash just yet.

Cut is a cleaner & more performant api relative to SplitN(_, _, 2) added in go 1.18

Previously applied this refactoring to buildah:
containers/buildah#5239

Signed-off-by: Philip Dubé <philip@peerdb.io>
Copy link
Contributor

openshift-ci bot commented Jan 11, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: edsantiago, serprex

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci openshift-ci bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jan 11, 2024
@edsantiago
Copy link
Member

/lgtm

Thank you, @serprex !

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. label Jan 11, 2024
@openshift-merge-bot openshift-merge-bot bot merged commit 238d08f into containers:main Jan 11, 2024
@github-actions github-actions bot added the locked - please file new issue/PR Assist humans wanting to comment on an old issue or PR with locked comments. label Apr 15, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. kind/api-change Change to remote API; merits scrutiny lgtm Indicates that a PR is ready to be merged. locked - please file new issue/PR Assist humans wanting to comment on an old issue or PR with locked comments. machine release-note-none
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants