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

opts: parseKeyValueFile: cleanup and remove redundant trimming #5496

Merged
merged 1 commit into from
Oct 4, 2024

Conversation

thaJeztah
Copy link
Member


  • the function already trimmed leading whitespace from each line before parsing. keys with trailing whitespace would be invalidated, and values have whitespace preserved, so there's no need to trim whitespace for the key.
  • if a line is validated (key is valid), we don't need to reconstruct the key=value by concatenating, and we can add the line as-is.
  • check if the key is empty before checking if it contains whitespace
  • touch-up comments
  • rename some variables for readability
  • slight cleanup to use early returns / early continues to reduce nesting

@thaJeztah thaJeztah added status/2-code-review kind/refactor PR's that refactor, or clean-up code labels Oct 3, 2024
@thaJeztah thaJeztah added this to the 28.0.0 milestone Oct 3, 2024
@thaJeztah thaJeztah self-assigned this Oct 3, 2024
@thaJeztah
Copy link
Member Author

I'll rebase #5491 after this; still looking at separating this to a separate package, so there will be some follow-ups.

@codecov-commenter
Copy link

codecov-commenter commented Oct 3, 2024

Codecov Report

Attention: Patch coverage is 94.11765% with 1 line in your changes missing coverage. Please review.

Project coverage is 60.09%. Comparing base (dac7319) to head (76196db).
Report is 12 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5496      +/-   ##
==========================================
- Coverage   60.09%   60.09%   -0.01%     
==========================================
  Files         345      345              
  Lines       23445    23441       -4     
==========================================
- Hits        14090    14086       -4     
  Misses       8381     8381              
  Partials      974      974              

@thaJeztah thaJeztah requested a review from a team October 3, 2024 20:24
Copy link
Contributor

@robmry robmry left a comment

Choose a reason for hiding this comment

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

LGTM (plus bikeshedding!).

// leading whitespace was already removed from the line, but
// variables are not allowed to contain whitespace or have
// trailing whitespace.
if strings.ContainsAny(key, whiteSpaces) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems odd to search for whitespace like this, but trim leading whitespace with TrimLeftFunc(_, unicode.IsSpace) above?

strings.ContainsFunc(key, unicode.IsSpace) would catch things that were missed before (like non-breaking space), so I guess it'd be a breaking change. Maybe worthwhile though?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I was looking at that as well; indeed also considering whether we should change. I'll keep this one for a follow-up in the meantime, but good suggestion.

opts/file.go Outdated Show resolved Hide resolved
opts/file.go Outdated Show resolved Hide resolved
- the function already trimmed leading whitespace from each line before
  parsing. keys with trailing whitespace would be invalidated, and values
  have whitespace preserved, so there's no need to trim whitespace for the
  key.
- if a line is validated (key is valid), we don't need to reconstruct the
  key=value by concatenating, and we can add the line as-is.
- check if the key is empty before checking if it contains whitespace
- touch-up comments
- rename some variables for readability
- slight cleanup to use early returns / early continues to reduce nesting

Signed-off-by: Sebastiaan van Stijn <[email protected]>
@thaJeztah thaJeztah merged commit 9dee12f into docker:master Oct 4, 2024
89 checks passed
@thaJeztah thaJeztah deleted the parseKeyValueFile_refactor branch October 4, 2024 10:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants