Documentation ¶
Overview ¶
Example ¶
package main import ( "context" "fmt" "os" "time" "github.com/zpatrick/cfg" "github.com/zpatrick/cfg/internal" "github.com/zpatrick/cfg/toml" ) type Config struct { Timeout time.Duration ServerPort int64 ServerAddr string } func main() { const data = ` timeout = "5s" [servers] [servers.alpha] port = 8080 addr = "localhost" ` path, err := internal.WriteTempFile("", data) if err != nil { panic(err) } defer os.Remove(path) tomlFile, err := toml.New(path) if err != nil { panic(err) } var c Config if err := cfg.Load(context.Background(), cfg.Schemas{ "timeout": cfg.Schema[time.Duration]{ Dest: &c.Timeout, Provider: tomlFile.Duration("timeout"), }, "server port": cfg.Schema[int64]{ Dest: &c.ServerPort, Provider: tomlFile.Int64("servers", "alpha", "port"), }, "server addr": cfg.Schema[string]{ Dest: &c.ServerAddr, Provider: tomlFile.String("servers", "alpha", "addr"), }, }); err != nil { panic(err) } fmt.Printf("Timeout: %s ServerPort: %d ServerAddr: %s", c.Timeout, c.ServerPort, c.ServerAddr, ) }
Output: Timeout: 5s ServerPort: 8080 ServerAddr: localhost
Index ¶
- func Provide[T any](p *Provider, keys ...string) cfg.Provider[T]
- type Provider
- func (p *Provider) Bool(keys ...string) cfg.Provider[bool]
- func (p *Provider) Duration(keys ...string) cfg.Provider[time.Duration]
- func (p *Provider) Float64(keys ...string) cfg.Provider[float64]
- func (p *Provider) Int(keys ...string) cfg.Provider[int]
- func (p *Provider) Int64(keys ...string) cfg.Provider[int64]
- func (p *Provider) String(keys ...string) cfg.Provider[string]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
Click to show internal directories.
Click to hide internal directories.