config

package
v0.0.0-...-53d8ad0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 15, 2024 License: MIT Imports: 7 Imported by: 0

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

View Source
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

View Source
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 ParseInt

func ParseInt(value interface{}) error

ParseInt ensures that a value is an int.

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 New

func New() *Config

New creates a new config with an initialized in memory store.

func (*Config) Bool

func (c *Config) Bool(key string) bool

Bool parses a config value as a boolean, or returns false if it cannot converted.

func (*Config) Duration

func (c *Config) Duration(key string) time.Duration

Duration attempts to parse a config value as a Duration, or returns a zero value.

func (*Config) Float64

func (c *Config) Float64(key string) float64

Float64 attempts to retrieve a config value as a float64, or returns a zero value.

func (*Config) Get

func (c *Config) Get(key string) interface{}

Get finds a config value by key.

func (*Config) Int

func (c *Config) Int(key string) int

Int attempts to retrieve a config value as an int, or returns a zero value.

func (*Config) Load

func (c *Config) Load(key string, value interface{}, fns ...ParseFunc) error

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) MustLoad

func (c *Config) MustLoad(key string, value interface{}, fns ...ParseFunc)

MustLoad is functionally equivalent to Load, but panics on error.

func (*Config) Set

func (c *Config) Set(key string, value interface{})

Set sets a config value by key.

func (*Config) String

func (c *Config) String(key string) string

String attempts to retrieve a config value as a string, or returns a zero value.

func (*Config) StringSlice

func (c *Config) StringSlice(key string) []string

StringSlice attempts to retrieve a config value as a slice of strings, or returns an empty slice.

func (*Config) Uint

func (c *Config) Uint(key string) uint

Uint attempts to retrieve a config value as a uint, or returns a zero value.

type ParseFunc

type ParseFunc func(value interface{}) error

ParseFunc can be used to validate a given configuration value. It returns an error if the given value is invalid.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL