fix: return defaultValue on conversion failure in ConvertStringToTypeOrDefault#492
Open
immanuwell wants to merge 1 commit into
Open
fix: return defaultValue on conversion failure in ConvertStringToTypeOrDefault#492immanuwell wants to merge 1 commit into
immanuwell wants to merge 1 commit into
Conversation
…OrDefault Signed-off-by: Immanuel Tikhonov <pchpr.00@list.ru> Signed-off-by: immanuwell <pchpr.00@list.ru>
There was a problem hiding this comment.
Pull request overview
This PR aligns ConvertStringToTypeOrDefault behavior with its documentation by returning the provided default value when parsing fails, preventing invalid env var values from silently becoming type zero-values (e.g., huge page size becoming 0). It also includes a small logging field typo fix and improves an error message for non-ENOENT stat failures.
Changes:
- Return
defaultValueonAtoi/ParseBoolconversion errors inConvertStringToTypeOrDefault. - Add unit tests covering empty/valid/invalid conversions for
intandbool. - Fix
label-selectorslog field key typo and improve a misleadingstaterror message.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| pkg/utils/cmd.go | Fix conversion-failure behavior to return defaultValue (per function doc). |
| pkg/utils/common_test.go | Add tests validating correct defaults and parsing outcomes for int/bool. |
| pkg/utils/kubernetes/runtime.go | Correct a log field key typo (label-selectors). |
| pkg/utils/longhorn/longhorn.go | Improve error message when os.Stat fails for reasons other than non-existence. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+21
to
+27
| var got any | ||
| switch d := testCase.defaultValue.(type) { | ||
| case int: | ||
| got = ConvertStringToTypeOrDefault(testCase.input, d) | ||
| case bool: | ||
| got = ConvertStringToTypeOrDefault(testCase.input, d) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue(s) this PR fixes:
Issue longhorn/longhorn#10000
What this PR does / why we need it:
ConvertStringToTypeOrDefaultdoc says "if conversion fails, return the default value" but the code returns the zero value (0for int,falsefor bool) instead. SoLONGHORN_SPDK_HUGE_PAGE_SIZE=garbagesilently sets huge page size to0rather than the default2048.Also: typo
lable-selectors->label-selectorsin a log field, and a misleading"does not exist"error message in a non-ENOENT stat error branch.Special notes for your reviewer:
Reproduce:
Additional documentation or context