feat(go-config): multi-format codecs, envc CLI, fixtures, ASRs

- Module github.com/eslider/go-config with env/yaml/json/ini packages
- internal: source, keymap, merge, structconv, bytesutil
- cmd/envc: convert, get, merge
- docs/asr ASR-0001..0008, README/CHANGELOG/AGENTS refresh
- golangci-lint v2 config; tests + fixtures

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-05-02 14:04:01 +01:00
co-authored by Cursor
parent 288a5b600c
commit c575341178
75 changed files with 2810 additions and 528 deletions
+172
View File
@@ -0,0 +1,172 @@
package env
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"sort"
"strings"
"github.com/eslider/go-config/internal/bytesutil"
"github.com/eslider/go-config/internal/keymap"
"github.com/eslider/go-config/internal/merge"
"github.com/eslider/go-config/internal/source"
"github.com/eslider/go-config/internal/structconv"
"github.com/joho/godotenv"
)
// Codec loads environment-style key/value data from multiple sources.
type Codec struct {
layers []func(context.Context) (map[string]string, error)
prefix string
normalizer keymap.Normalizer
sliceStrat merge.SliceStrategy
structOpts structconv.Options
mergeOpts []merge.Option
httpClient *http.Client
urlHeader http.Header
}
// New builds a Codec from options. Sources are merged left-to-right; later
// sources override earlier scalar leaves (see package merge).
func New(opts ...Option) *Codec {
c := &Codec{
normalizer: keymap.LowerAlnum,
sliceStrat: merge.Replace,
structOpts: structconv.Options{
TagName: "mapstructure",
WeaklyTyped: true,
Trim: true,
},
}
for _, o := range opts {
o(c)
}
c.mergeOpts = []merge.Option{merge.WithSliceStrategy(c.sliceStrat)}
return c
}
// Map returns the merged nested map after all sources are loaded and keys normalized.
func (c *Codec) Map(ctx context.Context) (map[string]any, error) {
if len(c.layers) == 0 {
return nil, fmt.Errorf("env: no sources configured")
}
acc := make(map[string]any)
for _, layer := range c.layers {
flat, err := layer(ctx)
if err != nil {
return nil, err
}
nested := nestedFromFlat(flat, c.prefix)
merge.DeepMerge(acc, nested, c.mergeOpts...)
}
if c.normalizer != nil {
keymap.Walk(acc, c.normalizer)
}
return acc, nil
}
// Unmarshal decodes using context.Background.
func (c *Codec) Unmarshal(dst any) error {
return c.UnmarshalContext(context.Background(), dst)
}
// UnmarshalContext decodes the merged map into dst using mapstructure.
func (c *Codec) UnmarshalContext(ctx context.Context, dst any) error {
m, err := c.Map(ctx)
if err != nil {
return err
}
return structconv.Decode(m, dst, c.structOpts)
}
// Marshal encodes src into dotenv-style bytes (KEY=value lines, sorted keys).
// src may be a struct or map[string]any.
func (c *Codec) Marshal(src any) ([]byte, error) {
m, err := encodeToMap(src)
if err != nil {
return nil, fmt.Errorf("env: marshal encode: %w", err)
}
flat := flattenMap(nil, m)
var buf bytes.Buffer
keys := make([]string, 0, len(flat))
for k := range flat {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
fmt.Fprintf(&buf, "%s=%s\n", k, flat[k])
}
return buf.Bytes(), nil
}
// WriteTo writes dotenv-style output to w.
func (c *Codec) WriteTo(w io.Writer, src any) (int64, error) {
b, err := c.Marshal(src)
if err != nil {
return 0, err
}
n, err := w.Write(b)
return int64(n), err
}
func flattenMap(prefix []string, m map[string]any) map[string]string {
out := make(map[string]string)
for k, v := range m {
path := append(prefix, k)
switch t := v.(type) {
case map[string]any:
for kk, vv := range flattenMap(path, t) {
out[kk] = vv
}
case []any:
parts := make([]string, 0, len(t))
for _, e := range t {
parts = append(parts, fmt.Sprint(e))
}
key := strings.Join(path, "_")
out[strings.ToUpper(key)] = strings.Join(parts, ",")
default:
key := strings.Join(path, "_")
out[strings.ToUpper(key)] = fmt.Sprint(t)
}
}
return out
}
func encodeToMap(src any) (map[string]any, error) {
if m, ok := src.(map[string]any); ok {
return m, nil
}
return structconv.Encode(src)
}
func flatFromEnviron(environ []string) map[string]string {
out := make(map[string]string)
for _, entry := range environ {
eq := strings.IndexByte(entry, '=')
if eq < 0 {
continue
}
out[entry[:eq]] = entry[eq+1:]
}
return out
}
func withSource(s source.Source, label string) func(*Codec) {
return func(c *Codec) {
c.layers = append(c.layers, func(ctx context.Context) (map[string]string, error) {
b, err := bytesutil.ReadAll(ctx, s)
if err != nil {
return nil, fmt.Errorf("env: read %s: %w", label, err)
}
m, err := godotenv.UnmarshalBytes(b)
if err != nil {
return nil, fmt.Errorf("env: parse %s: %w", label, err)
}
return m, nil
})
}
}
+125
View File
@@ -0,0 +1,125 @@
package env
import (
"context"
"testing"
"github.com/eslider/go-config/testfixtures"
)
type identityRoot struct {
Service svc `mapstructure:"service"`
}
type svc struct {
Name string
Subservice sub `mapstructure:"subservice"`
Database db `mapstructure:"database"`
}
type sub struct {
Name string
Key string
Timeout int
Enabled bool
}
type db struct {
URL string
PoolSize int `mapstructure:"poolsize"`
}
func TestCodec_IdentityEnvFixture(t *testing.T) {
c := New(WithBytes(testfixtures.Load(t, "identity", "service.env")))
var got identityRoot
if err := c.Unmarshal(&got); err != nil {
t.Fatal(err)
}
assertIdentity(t, got)
}
func assertIdentity(t *testing.T, got identityRoot) {
t.Helper()
if got.Service.Name != "my-service" {
t.Fatalf("Name=%q", got.Service.Name)
}
if got.Service.Subservice.Name != "abc" || got.Service.Subservice.Key != "abs" {
t.Fatalf("sub %+v", got.Service.Subservice)
}
if got.Service.Subservice.Timeout != 30 || !got.Service.Subservice.Enabled {
t.Fatalf("sub scalars %+v", got.Service.Subservice)
}
if got.Service.Database.URL != "postgres://localhost:5432/db" || got.Service.Database.PoolSize != 10 {
t.Fatalf("db %+v", got.Service.Database)
}
}
func TestCodec_MultiSourceLastWins(t *testing.T) {
c := New(
WithBytes(testfixtures.Load(t, "merge", "defaults.env")),
WithBytes(testfixtures.Load(t, "merge", "overlay.env")),
)
m, err := c.Map(context.Background())
if err != nil {
t.Fatal(err)
}
if got := m["service"].(map[string]any)["subservice"].(map[string]any)["name"]; got != "overlay-name" {
t.Fatalf("subservice.name = %v", got)
}
}
func TestCodec_WithCurrentEnvironment(t *testing.T) {
t.Setenv("SERVICE_KEY", "abc")
t.Setenv("SERVICE_PORT", "8080")
var cfg struct {
Service struct {
Key string `mapstructure:"key"`
Port int `mapstructure:"port"`
} `mapstructure:"service"`
}
c := New(WithCurrentEnvironment())
if err := c.Unmarshal(&cfg); err != nil {
t.Fatal(err)
}
if cfg.Service.Key != "abc" || cfg.Service.Port != 8080 {
t.Fatalf("%+v", cfg)
}
}
func TestCodec_WithTrimWeaklyTyped(t *testing.T) {
t.Setenv("NAME", " x ")
var cfg struct{ Name string }
c := New(WithCurrentEnvironment(), WithTrim(true))
if err := c.Unmarshal(&cfg); err != nil {
t.Fatal(err)
}
if cfg.Name != "x" {
t.Fatalf("%q", cfg.Name)
}
}
func TestCodec_WithWeaklyTypedOff(t *testing.T) {
t.Setenv("PORT", "8080")
var cfg struct{ Port int }
c := New(WithCurrentEnvironment(), WithWeaklyTyped(false))
if err := c.Unmarshal(&cfg); err == nil {
t.Fatal("expected error")
}
}
func TestCodec_WithTagName(t *testing.T) {
t.Setenv("DATABASE_URL", "postgres://x")
var cfg struct {
DB struct {
URL string `mapstructure:"url"`
} `mapstructure:"database" env:"database"`
}
c := New(WithCurrentEnvironment(), WithTagName("env"))
if err := c.Unmarshal(&cfg); err != nil {
t.Fatal(err)
}
if cfg.DB.URL != "postgres://x" {
t.Fatalf("%q", cfg.DB.URL)
}
}
Vendored
+14
View File
@@ -0,0 +1,14 @@
// Package env loads dotenv files and process environment variables into nested
// maps and Go structs. Multiple sources merge with later scalars overriding
// earlier ones; maps merge recursively.
//
// Example:
//
// c := env.New(
// env.WithFile(".default.env"),
// env.WithFile(".env"),
// env.WithCurrentEnvironment(),
// )
// var cfg MyConfig
// if err := c.Unmarshal(&cfg); err != nil { ... }
package env
+49
View File
@@ -0,0 +1,49 @@
package env
import (
"strings"
)
// insertPath walks path inside root, creating intermediate nested maps as
// needed, and stores value at the leaf. First write wins for a given leaf key
// within a single source map.
func insertPath(root map[string]any, path []string, value string) {
current := root
last := len(path) - 1
for i, raw := range path {
k := strings.ToLower(raw)
if i == last {
if _, exists := current[k]; exists {
return
}
current[k] = value
return
}
next, ok := current[k].(map[string]any)
if !ok {
next = make(map[string]any)
current[k] = next
}
current = next
}
}
func nestedFromFlat(flat map[string]string, prefix string) map[string]any {
out := make(map[string]any)
for k, v := range flat {
name := k
if prefix != "" {
if !strings.HasPrefix(name, prefix) {
continue
}
name = name[len(prefix):]
if name == "" {
continue
}
}
insertPath(out, strings.Split(name, "_"), v)
}
return out
}
+35
View File
@@ -0,0 +1,35 @@
package env
import (
"reflect"
"testing"
)
func TestNestedFromFlat(t *testing.T) {
got := nestedFromFlat(map[string]string{
"FOO": "bar",
"SERVICE_HTTP_PORT": "8080",
"SERVICE_KEY": "abc",
}, "")
want := map[string]any{
"foo": "bar",
"service": map[string]any{
"http": map[string]any{"port": "8080"},
"key": "abc",
},
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("got %#v want %#v", got, want)
}
}
func TestNestedFromFlat_FirstWriteWins(t *testing.T) {
flat := map[string]string{}
flat["A"] = "1"
got := nestedFromFlat(flat, "")
// Second insert for same key is ignored by insertPath within one build.
insertPath(got, []string{"a"}, "2")
if got["a"] != "1" {
t.Fatalf("got %v", got["a"])
}
}
+105
View File
@@ -0,0 +1,105 @@
package env
import (
"context"
"io"
"net/http"
"os"
"github.com/eslider/go-config/internal/keymap"
"github.com/eslider/go-config/internal/merge"
"github.com/eslider/go-config/internal/source"
"github.com/go-viper/mapstructure/v2"
)
// Option configures a Codec.
type Option func(*Codec)
// WithCurrentEnvironment appends the process environment as a source (read at Map time).
func WithCurrentEnvironment() Option {
return func(c *Codec) {
c.layers = append(c.layers, func(_ context.Context) (map[string]string, error) {
return flatFromEnviron(os.Environ()), nil
})
}
}
// WithFile appends a dotenv file path as a source.
func WithFile(path string) Option {
return withSource(source.File{Path: path}, path)
}
// WithBytes appends raw dotenv bytes as a source.
func WithBytes(b []byte) Option {
return withSource(source.Bytes{Data: b, Name: "bytes"}, "bytes")
}
// WithReader appends dotenv content from r (read fully on each Map call).
func WithReader(r io.Reader) Option {
return withSource(source.Reader{R: r, Name: "reader"}, "reader")
}
// WithURL appends an HTTP(S) URL returning dotenv content. Use WithHTTPHeader /
// WithHTTPClient before WithURL so they apply to this request.
func WithURL(raw string) Option {
return func(c *Codec) {
var hdr http.Header
if c.urlHeader != nil {
hdr = c.urlHeader.Clone()
}
s := source.URL{Raw: raw, Header: hdr, HTTPClient: c.httpClient}
withSource(s, raw)(c)
}
}
// WithHTTPClient sets the HTTP client used by subsequent WithURL sources.
func WithHTTPClient(client *http.Client) Option {
return func(c *Codec) { c.httpClient = client }
}
// WithHTTPHeader adds a header for subsequent WithURL sources.
func WithHTTPHeader(k, v string) Option {
return func(c *Codec) {
if c.urlHeader == nil {
c.urlHeader = make(http.Header)
}
c.urlHeader.Add(k, v)
}
}
// WithPrefix strips prefix from variable names before path splitting.
func WithPrefix(prefix string) Option {
return func(c *Codec) { c.prefix = prefix }
}
// WithKeyNormalizer sets key normalizer after merge (nil disables).
func WithKeyNormalizer(n keymap.Normalizer) Option {
return func(c *Codec) { c.normalizer = n }
}
// WithSliceMerge sets slice merge strategy when merging sources.
func WithSliceMerge(s merge.SliceStrategy) Option {
return func(c *Codec) { c.sliceStrat = s }
}
// WithTrim toggles struct string trim via mapstructure hook.
func WithTrim(enable bool) Option {
return func(c *Codec) { c.structOpts.Trim = enable }
}
// WithWeaklyTyped toggles weak typing for struct decode.
func WithWeaklyTyped(enable bool) Option {
return func(c *Codec) { c.structOpts.WeaklyTyped = enable }
}
// WithTagName sets the struct tag name for mapstructure.
func WithTagName(name string) Option {
return func(c *Codec) { c.structOpts.TagName = name }
}
// WithDecodeHook appends a decode hook.
func WithDecodeHook(h mapstructure.DecodeHookFunc) Option {
return func(c *Codec) {
c.structOpts.ExtraHooks = append(c.structOpts.ExtraHooks, h)
}
}