Documentation ¶
Overview ¶
Package config provides functions to load config from os env vars with the ability to perform optional validation on the returned values.
Index ¶
- Constants
- Variables
- func ParseBool(value interface{}) error
- func ParseDuration(value interface{}) error
- func ParseFloat64(value interface{}) error
- func ParseInt(value interface{}) error
- func ParseLogLevel(value interface{}) error
- func ParseString(value interface{}) error
- func ParseStringSlice(value interface{}) error
- type Config
- func (c *Config) Bool(key string) bool
- func (c *Config) Duration(key string) time.Duration
- func (c *Config) Float64(key string) float64
- func (c *Config) Get(key string) interface{}
- func (c *Config) Int(key string) int
- func (c *Config) Load(key string, value interface{}, fns ...ParseFunc) error
- func (c *Config) MustLoad(key string, value interface{}, fns ...ParseFunc)
- func (c *Config) Set(key string, value interface{})
- func (c *Config) String(key string) string
- func (c *Config) StringSlice(key string) []string
- func (c *Config) Uint(key string) uint
- type ParseFunc
Constants ¶
const ( // Service config. KeyServiceLogLevel = "service.log.level" KeyServiceShutdownTimeout = "service.shutdown.timeout" KeyServiceDiscoveryEnabled = "service.discovery.enabled" KeyServiceDiscoveryAddr = "service.discovery.addr" KeyServiceRegisterInt = "service.register.interval" // HTTPS/S server config. KeyHttpServerPort = "http.server.port" KeyHttpReadTimeout = "http.read.timeout" KeyHttpWriteTimeout = "http.write.timeout" KeyHttpIdleTimeout = "http.idle.timeout" // gRPC TLS config. KeyGrpcTLSCA = "grpc.tls.ca" // gRPC server config. KeyGrpcServerPort = "grpc.server.port" KeyGrpcServerCert = "grpc.server.cert" KeyGrpcServerKey = "grpc.server.key" KeyGrpcServerConnTimeout = "grpc.server.conn.timeout" // gRPC client config. KeyGrpcClientCert = "grpc.client.cert" KeyGrpcClientKey = "grpc.client.key" KeyGrpcClientTimeout = "grpc.client.timeout" )
Variables ¶
var ( ErrInvalidString = errors.New("value must be a string") ErrInvalidStringSlice = errors.New("value must be a slice of strings") ErrInvalidInt = errors.New("value must be an integer") ErrInvalidFloat64 = errors.New("value must be a float64") ErrInvalidBool = errors.New("value must be a boolean") ErrInvalidDuration = errors.New("value must be a duration with a time unit") ErrInvalidLogLevel = errors.New("value must be a log level") )
Errors representing parse failures.
Functions ¶
func ParseBool ¶
func ParseBool(value interface{}) error
ParseBool ensures that a value is a boolean. It accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False. Any other value returns an error.
func ParseDuration ¶
func ParseDuration(value interface{}) error
ParseDuration ensures that a value is a valid duration.
func ParseFloat64 ¶
func ParseFloat64(value interface{}) error
ParseFloat64 ensures that a value is a float64.
func ParseLogLevel ¶
func ParseLogLevel(value interface{}) error
ParseLogLevel validates that the value is a valid log level. Valid log levels are one of: trace, debug, info, warn, error, fatal
func ParseString ¶
func ParseString(value interface{}) error
ParseString ensures that a value is a string.
func ParseStringSlice ¶
func ParseStringSlice(value interface{}) error
ParseStringSlice ensures that a value is comma delimited slice of strings. Note: each segment is trimmed.
Types ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config stores config key/values and provides methods for concurrent read/write access.
func (*Config) Bool ¶
Bool parses a config value as a boolean, or returns false if it cannot converted.
func (*Config) Duration ¶
Duration attempts to parse a config value as a Duration, or returns a zero value.
func (*Config) Float64 ¶
Float64 attempts to retrieve a config value as a float64, or returns a zero value.
func (*Config) Load ¶
Load attempts to read config values from env vars, setting a default value if not found. If supplied, all parse funcs will be ran against the value and panic on failure.
func (*Config) String ¶
String attempts to retrieve a config value as a string, or returns a zero value.
func (*Config) StringSlice ¶
StringSlice attempts to retrieve a config value as a slice of strings, or returns an empty slice.