Documentation
¶
Overview ¶
Example ¶
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/zpatrick/cfg"
"github.com/zpatrick/cfg/ini"
"github.com/zpatrick/cfg/internal"
)
type Config struct {
Timeout time.Duration
ServerPort int64
ServerAddr string
}
func main() {
const data = `
timeout = "5s"
[server]
port = 8080
addr = "localhost"
`
path, err := internal.WriteTempFile("", data)
if err != nil {
panic(err)
}
defer os.Remove(path)
iniFile, err := ini.Load(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: iniFile.Duration("", "timeout"),
},
"server.port": cfg.Schema[int64]{
Dest: &c.ServerPort,
Provider: iniFile.Int64("server", "port"),
},
"server.addr": cfg.Schema[string]{
Dest: &c.ServerAddr,
Provider: iniFile.String("server", "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, section string, key string, convert func(k *ini.Key) (T, error)) cfg.Provider[T]
- type Provider
- func (p *Provider) Bool(section, key string) cfg.Provider[bool]
- func (p *Provider) Duration(section, key string) cfg.Provider[time.Duration]
- func (p *Provider) Float64(section, key string) cfg.Provider[float64]
- func (p *Provider) Int(section, key string) cfg.Provider[int]
- func (p *Provider) Int64(section, key string) cfg.Provider[int64]
- func (p *Provider) String(section, key string) cfg.Provider[string]
- func (p *Provider) Uint64(section, key string) cfg.Provider[uint64]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Provide ¶
func Provide[T any](p *Provider, section string, key string, convert func(k *ini.Key) (T, error)) cfg.Provider[T]
Provide loads a *ini.Key from the given section and key from p. The convert paramter is then called to convert the *ini.Key into a T. If the key does not exist, a cfg.NoValueProvidedError error will be returned.
Types ¶
type Provider ¶
type Provider struct {
// contains filtered or unexported fields
}
A Provider loads configuration values from a file in ini format.
func (*Provider) Duration ¶
Duration returns a duration configuration value at the given section and key.
func (*Provider) Float64 ¶
Float64 returns a float64 configuration value at the given section and key.
Click to show internal directories.
Click to hide internal directories.