Skip to content

fix: return errors on invalid values instead of silently zeroing fields - #51

Open
Xuepoo wants to merge 1 commit into
BoRuDar:devfrom
Xuepoo:fix/return-parse-errors
Open

Xuepoo wants to merge 1 commit into
BoRuDar:devfrom
Xuepoo:fix/return-parse-errors

Conversation

@Xuepoo

@Xuepoo Xuepoo commented Aug 12, 2026

Copy link
Copy Markdown

Problem

All strconv parse calls in setValue, setSlice, and setPtrValue discard 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 left nil), and SetField reports 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 a void); invalid time.Duration strings also error.
  • setPtrValue: matches on Kind() instead of Name() 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:

  • invalid int, uint, float, bool, time.Duration values return errors via setValue
  • invalid slice items return errors via setSlice
  • invalid pointer values return errors via setPtrValue
  • integration: New with a bad env value now returns an error

Existing tests were updated where they called the now-error-returning setInt64.

Verification

  • go test ./... — all pass (coverage 94%)
  • golangci-lint run ./... — 0 issues

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant