confunc

package module
v0.0.0-...-ece7dc4 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2016 License: MIT Imports: 5 Imported by: 0

README

confunc

Build Status Coverage Status

confunc provides a functional approach to configure an application. With confunc, each configuraion value can be defined as a function, providing a dynamic value rather than a static one that is assigned once the application starts up.

Basic Usage

Example:

// declare your configuration value
maxNumberOfOpenConnections := confunc.From(confunc.Env()).Int("MAX_OPEN_CONNECTIONS")
...
// when you need to access the value
if connectionCount < maxNumberOfOpenConnections() {
    // do your thing
}

Source

Source is where the configuration is retrieved. It is defined by the Source interface where a string value is returned for a specified key. Once it is passed to confunc.From() function; a struct that can return configuration functions is created. Available sources are:

  • Env() : retrieves Environment Variables
  • Map(map[string]string) : retrieves configuration from a map
  • cfconsul.Source(*api.Config) : retrieves configuration from consul. You need to import confunc/cfconsul package

Interceptor

confunc provides a middleware mechanism via Interceptor type. It enables handling of various cases like default value

maxNumberOfOpenConnections := confunc.From(confunc.Env()).Int("MAX_OPEN_CONNECTIONS", confunc.Default("10"))
...
// when you need to access the value
if connectionCount < maxNumberOfOpenConnections() {
    // do your thing
}

Interceptor is a simple function that accepts a Confunc function and also returns Confunc function.

type Confunc func() (string, error)

type Interceptor func(Confunc) Confunc

Interceptors process the configuration value before it is accessed, every time. Custom interceptors can be easily introduces by providing a function matching the above signature. Available interceptors are:

  • Default(defaultValue string) : returns a default value if there is no configuration made
  • CacheOnce() : caches the value retrieved once and prevents accessing the configuration source each time
  • CacheFor(cacheDuration time.Duration) : caches the value for a duration provided.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Boolean

type Boolean func() bool

type Confunc

type Confunc func() (string, error)

type Float64

type Float64 func() float64

type Integer

type Integer func() int

type Interceptor

type Interceptor func(Confunc) Confunc

func CacheFor

func CacheFor(cacheDuration time.Duration) Interceptor

func CacheOnce

func CacheOnce() Interceptor

func Default

func Default(defaultVal string) Interceptor

type Parameterizer

type Parameterizer struct {
	// contains filtered or unexported fields
}

func From

func From(s Source) *Parameterizer

func (*Parameterizer) Bool

func (p *Parameterizer) Bool(sourceKey string, interceptors ...Interceptor) Boolean

func (*Parameterizer) Conf

func (p *Parameterizer) Conf(sourceKey string, interceptors ...Interceptor) Confunc

func (*Parameterizer) Float64

func (p *Parameterizer) Float64(sourceKey string, interceptors ...Interceptor) Float64

func (*Parameterizer) Int

func (p *Parameterizer) Int(sourceKey string, interceptors ...Interceptor) Integer

func (*Parameterizer) String

func (p *Parameterizer) String(sourceKey string, interceptors ...Interceptor) String

type Source

type Source interface {
	Value(v string) (string, error)
}

func Env

func Env() Source

func Map

func Map(c map[string]string) Source

type String

type String func() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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