Skip to content

refactor: single package, no secrets in errors, focused examples - #229

Merged
obalunenko merged 9 commits into
masterfrom
refactor/single-package
Nov 15, 2026
Merged

obalunenko merged 9 commits into
masterfrom
refactor/single-package

Conversation

@obalunenko

Copy link
Copy Markdown
Owner

Description

Follow-up to a review of the library. Four commits, each one stands on its own:

  1. fix: keep url.URL values out of parse errors. url.Parse returns a *url.Error that quotes the whole input, so an invalid DATABASE_URL=postgres://user:password@… put the password into the error, and log.Fatal(err) put it into the logs. The underlying cause is returned instead. A test checks that the password is not in err.Error().
  2. refactor: merge internal and option into the root package. option could not import getenv, so Parameters had to live in internal, which leaked into the public API: Option.Apply(*internal.Parameters) and Parsable = internal.EnvParsable, whose types godoc did not show. Now:
    • getenv.Option, getenv.WithSeparator and getenv.WithTimeLayout; Option is sealed with an unexported method;
    • Parsable lists its types itself, so the hand-written list in the package doc is gone;
    • option is a deprecated forwarder: option.Option = getenv.Option, so existing calls keep compiling.
  3. docs: state how slice values are split. Elements are not trimmed ("1, 2" is not a valid []int), and a trailing separator adds an empty element. Both are documented and pinned with tests.
  4. style: tidy error messages and split the examples.
    • Errors look like environment variable "PORT": invalid value: ….
    • decimalBase is replaced with the literal 10.
    • One short example per use case instead of two that printed every type through any.
    • The README examples are regenerated: the old url.URL example printed struct fields, not the URL shown in its output.

Github Issue

—

Possible failures/side-effects

  • apidiff/gorelease will report the removal of option.Option.Apply. It could not be called outside the module, since internal.Parameters cannot be constructed there. Callers that use option.WithSeparator or option.WithTimeLayout with Env or EnvOrDefault keep compiling; option/option_test.go checks this.
  • getenv.Parsable is now its own interface, not an alias. Its type set is the same, so generic code like func f[T getenv.Parsable]() is unaffected.
  • Error messages change again after fix: put "invalid value" before the cause in parse errors #228. Code that matches error text instead of using errors.Is will break. The sentinels and errors.Is(err, strconv.ErrSyntax) work as before.
  • Checks run:
    • go vet and go test -race ./...;
    • golangci-lint with both .golangci.yml and .golangci.pipe.yml: 0 issues;
    • every README snippet compiled and run, output matches.

Other notes

The base is fix/error-message-order (#228), because this PR changes the same error messages and option docs. Merge #228 first; GitHub will then retarget this PR to master.

The next release should list the behavior changes since v1.9.1 in its notes: the default separator and time layout, the error text, and the deprecated option package.

🤖 Generated with Claude Code

obalunenko and others added 6 commits November 16, 2026 00:26
Parse wrapped the parser error first and ErrInvalidValue last, so the
message read backwards:

  strconv.ParseInt: parsing "x": invalid syntax: invalid value

Swap the operands so the message goes from the general error to its
cause, as Go error chains usually do:

  invalid value: strconv.ParseInt: parsing "x": invalid syntax

Only the text changes: errors.Is still matches both ErrInvalidValue and
the cause.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The EnvParser interface was removed with the parser hierarchy in #223,
but the Option comment still called it "a contract for EnvParser
parameters options". Describe what an option does instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
url.Parse returns a *url.Error that quotes the whole input, so an invalid
DSN such as postgres://user:password@host put the password into the error
and from there into logs. Return the underlying cause instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The option package could not import getenv, so the parameters it sets had
to live in a third package, internal. That leaked into the public API:
Option.Apply took an *internal.Parameters that callers cannot name, and
Parsable was an alias of internal.EnvParsable, so godoc did not list the
supported types and the package doc had to repeat them by hand.

Everything now lives in package getenv:

- Option, WithSeparator and WithTimeLayout. Option is sealed with an
  unexported method instead of by an unimportable parameter type.
- Parsable spells out its types, so godoc shows them and the package doc
  points there instead of repeating them.

Package option stays as a deprecated forwarder: option.Option is an alias
of getenv.Option, so existing calls keep compiling. Option.Apply is gone;
it could not be called outside the module, as internal.Parameters cannot
be constructed there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Elements are not trimmed and a trailing separator adds an empty element.
Both are easy to trip over, so say it in the package doc and pin it with
tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Errors read `environment variable "KEY": not set` instead of
  `failed to get environment variable[KEY]: not set`: %q is the usual way
  to quote a name, and "failed to" only adds length to a wrapped chain.
- Use the literal base 10 for strconv: a named constant for it is noise.
- Replace the two examples that printed every type through an any with one
  short example per use case, so godoc shows the typed results and how to
  tell ErrNotSet from ErrInvalidValue.
- Regenerate the README examples. The old url.URL example printed the
  struct fields, not the URL its output claimed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sonarqubecloud

Copy link
Copy Markdown

@codecov

codecov Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (8fe5733) to head (3d1cc19).

Additional details and impacted files
@@            Coverage Diff            @@
##            master      #229   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            3         4    +1     
  Lines           99       103    +4     
=========================================
+ Hits            99       103    +4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

url.Parse wraps every error in *url.Error, so the fallback return never
ran and was the only statement left without coverage.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Base automatically changed from fix/error-message-order to master November 15, 2026 20:55
obalunenko and others added 2 commits November 16, 2026 00:57
master has #228 squashed; its tree equals 9744de6, which this branch
already contains, so the branch tree stays as it is.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@obalunenko
obalunenko merged commit 9e1b153 into master Nov 15, 2026
11 checks passed
@obalunenko
obalunenko deleted the refactor/single-package branch November 15, 2026 21:09
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.

1 participant