Apply values
Use ApplyValues when another part of the application has already parsed values into Go types, such as a CLI flag parser or configuration loader.
type mapSource map[string]any
func (s mapSource) Get(path string) (any, bool, error) {
value, found := s[path]
return value, found, nil
}
type Database struct {
Driver string
MaxOpenConns int
}
type CLIConfig struct {
Database *Database
Labels []string
}
binding, err := model.NewBinding[CLIConfig]()
if err != nil {
return err
}
cfg := CLIConfig{}
err = binding.ApplyValues(&cfg, mapSource{
"Database.Driver": "postgres",
"Database.MaxOpenConns": int32(8),
"Labels[]": []string{"ci", "nightly"},
})
Paths use exact exported Go field names. The source is queried for fields even below a nil pointer; Database is allocated when a descendant value is supplied. Collection values are replaced as a whole with a path ending in []. ApplyValues does not assign individual collection element paths such as Endpoints[].URL.
If Get reports a value as missing, the field stays unchanged. A found nil resets the field to its zero value. Values must be assignable or convertible to the field type; scalar values can initialize pointer-to-scalar fields. A failed assignment can leave earlier assignments in place.