Documentation
¶
Overview ¶
Package appconf provides a flexible configuration loader for applications that wish to load configuration values from different sources, such as files or the environment. It supports nested data structures, type-safe conversions and binding using reflection.
Example ¶
package main import ( "fmt" "time" "github.com/halimath/appconf" ) func main() { c, err := appconf.New( appconf.Static(map[string]interface{}{ "web.address": "http://example.com", "DB": map[string]interface{}{ "Timeout": 2 * time.Second, }, }), appconf.JSONFile("./testdata/config.json", true), ) if err != nil { panic(err) } fmt.Println(c.HasKey("Web")) fmt.Println(c.HasKey("Foo")) fmt.Println(c.GetString("Web.Address")) fmt.Println(c.GetDuration("db.timeout")) }
Output: true false localhost:8080 2s
Index ¶
- Constants
- Variables
- type AppConfig
- func (c *AppConfig) Bind(v interface{}) error
- func (c *AppConfig) GetBool(key string) bool
- func (c *AppConfig) GetBoolE(key string) (bool, error)
- func (c *AppConfig) GetComplex128(key string) complex128
- func (c *AppConfig) GetComplex128E(key string) (complex128, error)
- func (c *AppConfig) GetDuration(key string) time.Duration
- func (c *AppConfig) GetDurationE(key string) (time.Duration, error)
- func (c *AppConfig) GetFloat32(key string) float32
- func (c *AppConfig) GetFloat32E(key string) (float32, error)
- func (c *AppConfig) GetFloat64(key string) float64
- func (c *AppConfig) GetFloat64E(key string) (float64, error)
- func (c *AppConfig) GetInt(key string) int
- func (c *AppConfig) GetInt64(key string) int64
- func (c *AppConfig) GetInt64E(key string) (int64, error)
- func (c *AppConfig) GetIntE(key string) (int, error)
- func (c *AppConfig) GetString(key string) string
- func (c *AppConfig) GetStringE(key string) (string, error)
- func (c *AppConfig) GetUint(key string) uint
- func (c *AppConfig) GetUint64(key string) uint64
- func (c *AppConfig) GetUint64E(key string) (uint64, error)
- func (c *AppConfig) GetUintE(key string) (uint, error)
- func (c *AppConfig) HasKey(key string) bool
- func (c *AppConfig) Sub(key string) *AppConfig
- func (c *AppConfig) SubE(key string) (*AppConfig, error)
- type Key
- type KeyPath
- type Loader
- type LoaderFunc
- type Node
- func (n *Node) Dump(indent int)
- func (n *Node) GetBool() bool
- func (n *Node) GetBoolE() (bool, error)
- func (n *Node) GetComplex128() complex128
- func (n *Node) GetComplex128E() (complex128, error)
- func (n *Node) GetDuration() time.Duration
- func (n *Node) GetDurationE() (time.Duration, error)
- func (n *Node) GetFloat32() float32
- func (n *Node) GetFloat32E() (float32, error)
- func (n *Node) GetFloat64() float64
- func (n *Node) GetFloat64E() (float64, error)
- func (n *Node) GetInt() int
- func (n *Node) GetInt64() int64
- func (n *Node) GetInt64E() (int64, error)
- func (n *Node) GetIntE() (int, error)
- func (n *Node) GetString() string
- func (n *Node) GetStringE() (string, error)
- func (n *Node) GetUint() uint
- func (n *Node) GetUint64() uint64
- func (n *Node) GetUint64E() (uint64, error)
- func (n *Node) GetUintE() (uint, error)
- func (n *Node) OverwriteWith(o *Node)
- type ReaderLoaderFunc
Examples ¶
Constants ¶
const ( FieldTagKey = "appconf" FieldTagValueSeparator = "," FieldTagIgnore = "ignore" )
const (
KeySeparator = "."
)
Variables ¶
var ( ErrUnsupportedValue = errors.New("unsupported value") ErrNoSuchKey = errors.New("no such key") ErrNotAScalar = errors.New("not a scalar value") )
var ( // ErrInvalidBindingType is returned from binding when a value's type is not supported for binding. ErrInvalidBindingType = errors.New("invalid binding type") )
Functions ¶
This section is empty.
Types ¶
type AppConfig ¶
type AppConfig struct {
// contains filtered or unexported fields
}
AppConf is the main data type used to interact with configuration values.
func New ¶
New creates a new AppConfig using the given loaders. The loaders are executed in given order with values from later loaders overwriting values from earlier ones (put most significant loaders last).
func (*AppConfig) Bind ¶
Bind binds the configuration to the data structure v and returns any error that occured during binding. v must be a pointer to either a struct value or a map[string]interface{}. Other values are not supported and are rejected by an error. See the README for an explanation of how to use and customize the binding.
Example (Map) ¶
package main import ( "fmt" "github.com/halimath/appconf" ) func main() { c, err := appconf.New(appconf.JSONFile("./testdata/config.json", true)) if err != nil { panic(err) } var config map[string]interface{} if err := c.Bind(&config); err != nil { panic(err) } fmt.Printf("%v\n", config) }
Output: map[backends:map[0:map[host:alpha port:8080 tags:map[0:a 1:1]] 1:map[host:beta port:8081 tags:map[0:b 1:2]]] db:map[host:localhost password:secret port:3306 type:mysql user:test] web:map[address:localhost:8080 authorize:true timeout:2s]]
Example (Struct) ¶
package main import ( "fmt" "time" "github.com/halimath/appconf" ) func main() { type Config struct { DB struct { Engine string `appconf:"type"` Host string Port int User string `appconf:",ignore"` } Web struct { Address string Timeout time.Duration Authorize bool } Backends []struct { Host string Port int Tags []string } } c, err := appconf.New(appconf.JSONFile("./testdata/config.json", true)) if err != nil { panic(err) } var config Config if err := c.Bind(&config); err != nil { panic(err) } fmt.Printf("%v\n", config) }
Output: {{mysql localhost 3306 } {localhost:8080 2s true} [{alpha 8080 [a 1]} {beta 8081 [b 2]}]}
func (*AppConfig) GetBoolE ¶
GetBoolE returns the bool value stored under key or an error if the key is not defined or the value cannot be converted to an bool.
func (*AppConfig) GetComplex128 ¶
func (c *AppConfig) GetComplex128(key string) complex128
GetComplex128 returns the complex128 value stored under key.
func (*AppConfig) GetComplex128E ¶
func (c *AppConfig) GetComplex128E(key string) (complex128, error)
GetComplex128E returns the complex128 value stored under key or an error if the key is not defined or the value cannot be converted to an complex128.
func (*AppConfig) GetDuration ¶
GetDuration returns the duration value stored under key.
func (*AppConfig) GetDurationE ¶
GetDurationE returns the duration value stored under key or an error if the key is not found or the string value cannot be parsed into a Duration.
func (*AppConfig) GetFloat32 ¶
GetFloat32 returns the float32 value stored under key.
func (*AppConfig) GetFloat32E ¶
GetFloat32E returns the float32 value stored under key or an error if the key is not defined or the value cannot be converted to an float32.
func (*AppConfig) GetFloat64 ¶
GetFloat64 returns the float64 value stored under key.
func (*AppConfig) GetFloat64E ¶
GetFloat64E returns the float64 value stored under key or an error if the key is not defined or the value cannot be converted to an float64.
func (*AppConfig) GetInt64E ¶
GetInt64E returns the int64 value stored under key or an error if the key is not defined or the value cannot be converted to an int64.
func (*AppConfig) GetIntE ¶
GetIntE returns the int value stored under key or an error if the key is not defined or the value cannot be converted to an int.
func (*AppConfig) GetStringE ¶
GetStringE returns the string value stored under key or an error if the key is not defined.
func (*AppConfig) GetUint64E ¶
GetUint64E returns the uint64 value stored under key or an error if the key is not defined or the value cannot be converted to an uint64.
func (*AppConfig) GetUintE ¶
GetUintE returns the uint value stored under key or an error if the key is not defined or the value cannot be converted to an uint.
type Loader ¶
type Loader interface { // Load loads the configuration values and returns them as a tree of Nodes. If the loading was not // successful an error should be returned. Load() (*Node, error) }
Loader defines the interface implemented by all loaders.
func Env ¶
Env creates a Loader which reads configuration values from the environment. Only env variables with a name starting with prefix are considered. Use the empty string to select all variables.
func File ¶
func File(filename string, mandatory bool, l ReaderLoaderFunc) Loader
File creates a Loader that reads the file named filename and forwards the content to l. If mandatory is set to false, an empty configuration will be returned when filename does not exist. Otherwise this is is reported as an error.
func Static ¶
Static creates a Loader that returns static configuration values from the given map structure. The map's values are limited to strings, map[string]interface{} (with the same value constraints applied) or slices of either strings or maps.
type LoaderFunc ¶
LoaderFunc is a convenience type to convert a function to a Loader.
func (LoaderFunc) Load ¶
func (l LoaderFunc) Load() (*Node, error)
type Node ¶
func ConvertToNode ¶
func (*Node) GetComplex128 ¶
func (n *Node) GetComplex128() complex128
func (*Node) GetComplex128E ¶
func (n *Node) GetComplex128E() (complex128, error)