Repository navigation
Conversation
All strconv parsing in setValue/setSlice/setPtrValue discarded errors and silently set the field to its zero value (or left pointer fields nil), reporting success. A typo'd value like an env var of "not-a-number" for an int field would silently become 0. Now parse errors are propagated and wrapped with the field's kind, so invalid configuration surfaces as an error instead of corrupting state. Also switch setPtrValue to match on Kind() instead of Name(), so pointer fields of named types (e.g. type MyInt int) can be set, matching the non-pointer path.
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.
Problem
All
strconvparse calls insetValue,setSlice, andsetPtrValuediscard their errors (i, _ := strconv.ParseInt(...)). When a value fails to parse, the field is silently set to its zero value (or a pointer field is leftnil), andSetFieldreports success.Example:
Port int \env:"PORT"`withPORT=not-a-numberreturns no error andPort == 0`. A typo'd configuration value silently corrupts state instead of surfacing an error.Related: #30 ("Return errors") was about a broader case; this closes the remaining silent-parse gap in the setter path.
Changes
setValue/setSlice/setPtrValue: parse errors are now returned and wrapped with the target kind (cannot parse "not-a-number" as int: ...).setInt64: now returns an error (was avoid); invalidtime.Durationstrings also error.setPtrValue: matches onKind()instead ofName()as a bonus fix, so pointer fields of named types (type MyInt int) can be set — consistent with the non-pointer path.Tests
Added unit tests covering:
int,uint,float,bool,time.Durationvalues return errors viasetValuesetSlicesetPtrValueNewwith a bad env value now returns an errorExisting tests were updated where they called the now-error-returning
setInt64.Verification
go test ./...— all pass (coverage 94%)golangci-lint run ./...— 0 issues