Skip to main content

Write values

Use WriteValues when another component needs to inspect the resolved configuration after defaults, file values, and environment overrides have been applied. For example, an E2Engine-style config inspect command can collect field paths and values without writing a separate walker.

type Database struct {
Driver string
}
type CLIConfig struct {
DB Database
}

type entry struct {
Path string
Value any
}
type entries []entry

func (s *entries) Set(path string, value any) error {
*s = append(*s, entry{Path: path, Value: value})
return nil
}

binding, err := model.NewBinding[CLIConfig]()
if err != nil {
return err
}
cfg := CLIConfig{DB: Database{Driver: "postgres"}}
var output entries
if err := binding.WriteValues(&cfg, &output); err != nil {
return err
}
for _, item := range output {
if item.Path == "DB.Driver" {
fmt.Printf("%s=%v\n", item.Path, item.Value) // DB.Driver=postgres
}
}

The sink receives exported Go field paths such as DB.Driver. Scalar collections are written once; struct collections are written under a [] path, followed by child paths such as Endpoints[].URL. That child path can occur more than once, so use an append-oriented sink if every element matters. A map sink keeps only the last value for a repeated path.

Nil struct pointers are written but not traversed. Reference values such as maps, slices, and pointers are passed through without copying. A sink error stops traversal, and earlier writes remain in the sink.