Files
config/README.md
T
end f2b6c07ff7 fix: validate config after PostLoad hooks
Re-run validator after PostLoad, return errors from RootDir, and add Config
alias for C(). Hooks receive *T instead of *Manager.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-29 18:00:31 -07:00

1.7 KiB

config

Typed configuration loading with viper and validator.

Install

go get gitea.auvem.com/go-toolkit/config

Quick start

type Schema struct {
    AppName string `mapstructure:"appName" validate:"required"`
}

dir, err := config.RootDir(".env.yml", 1)
if err != nil {
    log.Fatal(err)
}

m := config.NewManager[Schema](".env", "yaml", dir).WithOpts(&config.ManagerOpts[Schema]{
    EnvPrefix: "MYAPP",
    PostLoad: func(cfg *Schema) error {
        // derived fields — validated again after PostLoad
        return nil
    },
})

if err := m.Load(); err != nil {
    log.Fatal(err)
}

cfg := m.C() // or m.Config()
_ = cfg.AppName

Load vs C

  • Load() returns errors explicitly — use in main during startup
  • C() and Config() lazy-load and panic on failure — convenient after startup

PostLoad hooks

PostLoad runs after the first validation pass. The schema is re-validated after PostLoad so derived fields cannot bypass constraints.

RootDir

RootDir(name, depth) returns (string, error). MustRootDir panics on failure (for package-level var initialization).

Breaking changes (V1)

Before After
PostLoad func(*Manager[T]) PostLoad func(*T)
PreLoad func(*Manager[T]) PreLoad func(*T)
Manager.R field use C() / hook *T parameter
RootDir(...) string (panic) RootDir(...) (string, error); use MustRootDir for panic

Companion types

Configuration structs in dbx and courier include mapstructure/validate tags compatible with this package.