conf/source.go
juancwu c4ebd80669 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>
2026-04-27 20:35:51 +00:00

34 lines
776 B
Go

package conf
import "os"
// Source returns a string value for a key, or false if the key is not present.
type Source interface {
Lookup(key string) (string, bool)
}
type envSource struct{}
func (envSource) Lookup(key string) (string, bool) {
v, ok := os.LookupEnv(key)
if !ok || v == "" {
return "", false
}
return v, true
}
// EnvSource reads from the process environment. Empty values are treated as absent.
func EnvSource() Source { return envSource{} }
type mapSource map[string]string
func (m mapSource) Lookup(key string) (string, bool) {
v, ok := m[key]
if !ok || v == "" {
return "", false
}
return v, true
}
// MapSource wraps a key/value map as a Source. The map is not copied.
func MapSource(m map[string]string) Source { return mapSource(m) }