Skip to content

feat: add generic Builder.Load[T] method - #20

Open
JeremyLoy wants to merge 2 commits into
feat/sourcesfrom
feat/generic-load
Open

JeremyLoy wants to merge 2 commits into
feat/sourcesfrom
feat/generic-load

Conversation

@JeremyLoy

@JeremyLoy JeremyLoy commented Oct 12, 2026 •

Copy link
Copy Markdown
Owner

Stack 7/7. Base: #19.

cfg, err := config.FromEnv().Load[MyConfig]()

Load is a generic method on Builder (generic methods arrived in Go 1.27, which the module requires since #16). It returns a populated T instead of filling a pointer you pass in. When T is not a struct type, it returns ErrInvalidTarget instead of panicking. To and Sub keep their existing panic so current callers see no change.

Verification

  • Test_Load checks that a load succeeds, and that on a field error the other fields are still filled in.
  • Test_LoadInvalidTarget checks int and *struct.
  • Added ExampleBuilder_Load.
  • The full suite plus gofmt, vet and staticcheck pass on go1.27.0 and go1.27.2.

🤖 Generated with Claude Code

https://claude.ai/code/session_01NUxivLjodSESTkzRtdQ4CP

@JeremyLoy JeremyLoy changed the title feat: add generic Load[T] feat: add generic Builder.Load[T] method Oct 12, 2026
@JeremyLoy
JeremyLoy added this pull request to stack #21 October 12, 2026 05:15
  cfg, err := config.Load[MyConfig](config.FromEnv())

Load returns a populated T instead of filling a pointer, and returns
ErrInvalidTarget rather than panicking when T is not a struct type. To and
Sub keep their existing panic for compatibility.

Load is a package-level function rather than a Builder method so that the
module can keep supporting Go 1.26. Generic methods need Go 1.27; Load can
become b.Load[T]() once 1.26 leaves support.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NUxivLjodSESTkzRtdQ4CP
Now that the module requires Go 1.27, Load can be a generic method:

  cfg, err := config.FromEnv().Load[MyConfig]()

This replaces the package-level config.Load[T](b), which existed only to
keep supporting Go 1.26.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NUxivLjodSESTkzRtdQ4CP
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.

2 participants