Skip to content

opts: parseKeyValueFile: cleanup and remove redundant trimming - #5496

Merged
thaJeztah merged 1 commit into
docker:masterfrom
thaJeztah:parseKeyValueFile_refactor
Oct 4, 2024
Merged

opts: parseKeyValueFile: cleanup and remove redundant trimming#5496
thaJeztah merged 1 commit into
docker:masterfrom
thaJeztah:parseKeyValueFile_refactor

Conversation

@thaJeztah

Copy link
Copy Markdown
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

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

Copy link
Copy Markdown
MemberAuthor

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

@codecov-commenter

codecov-commenter commented Oct 3, 2024

Copy link
Copy Markdown

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 62 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 teamOctober 3, 2024 20:24

@robmryrobmry left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM (plus bikeshedding!).

Comment threadopts/file.go
// 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
Copy Markdown
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
Copy Markdown
MemberAuthor

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.

Comment threadopts/file.go Outdated
Comment threadopts/file.go Outdated
@thaJeztah
thaJeztahforce-pushed the parseKeyValueFile_refactor branch from bd9ff60 to 355b18fCompareOctober 4, 2024 09:54
- 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 <github@gone.nl>
@thaJeztah
thaJeztahforce-pushed the parseKeyValueFile_refactor branch from 355b18f to 76196dbCompareOctober 4, 2024 09:56
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/refactorPR's that refactor, or clean-up codeprocess/cherry-pickedstatus/2-code-review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@thaJeztah@codecov-commenter@laurazard@robmry