Add reflective struct-tag config loader

Implements conf.Load to populate tagged structs from a chain of Sources
(env, .env, YAML/JSON/TOML, custom). Supports default values, slice
separators, nested structs, pointer fields, and a Validator hook.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
juancwu 2026-04-27 20:35:51 +00:00
commit c4ebd80669
15 changed files with 941 additions and 0 deletions

35
dotenv_test.go Normal file
View file

@ -0,0 +1,35 @@
package conf
import (
"strings"
"testing"
)
func TestDotEnvReader(t *testing.T) {
in := `# comment
FOO=bar
export QUOTED="hello\nworld"
SINGLE='ab cd'
INLINE=plain # trailing
EMPTY=
`
src, err := DotEnvReader(strings.NewReader(in))
if err != nil {
t.Fatal(err)
}
cases := map[string]string{
"FOO": "bar",
"QUOTED": "hello\nworld",
"SINGLE": "ab cd",
"INLINE": "plain",
}
for k, want := range cases {
got, ok := src.Lookup(k)
if !ok || got != want {
t.Errorf("%s = %q ok=%v, want %q", k, got, ok, want)
}
}
if _, ok := src.Lookup("EMPTY"); ok {
t.Errorf("EMPTY should be absent")
}
}