config

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2018 License: MIT Imports: 7 Imported by: 3

README

config

Golang package for app configuration.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("not found")

ErrNotFound returned when an operation is attempted on a resource that doesn't exist, such as fetching a non-existing property name.

Functions

This section is empty.

Types

type ChangedListener

type ChangedListener interface {

	// Changed is called when one or more properties in a `SourceMonitored` has a
	// changed value.
	Changed(cfg *Config, src SourceMonitored)
}

ChangedListener interface is for receiving notifications when one or more properties within monitored config sources (SourceMonitored) have changed values.

type Config

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

Config provides methods for retrieving property values from one or more configuration sources.

func (*Config) AddChangedListener

func (config *Config) AddChangedListener(l ChangedListener)

AddChangedListener adds a listener that will receive notifications whenever one or more property values change within the config.

func (*Config) AppendSource

func (config *Config) AppendSource(srcs ...Source)

AppendSource appends one or more `Sources` at the end of the list of sources such that the last source will be the source checked last when resolving a property value.

func (*Config) Bool

func (config *Config) Bool(name string, def bool) (val bool, err error)

Bool returns the value of the named prop as a `bool`. If the property is not found then the supplied default `def` and `ErrNotFound` are returned.

Supports (t, true, 1, y, yes) for true, and (f, false, 0, n, no) for false, all case-insensitive.

See config.String

func (*Config) Duration

func (config *Config) Duration(name string, def time.Duration) (val time.Duration, err error)

Duration returns the value of the named prop as a `time.Duration`, representing a span of time.

Units of measure are supported: ms, sec, min, hour, day, week, year. See config.UnitsToMillis for a complete list of units supported.

If the property is not found then the supplied default `def` and `ErrNotFound` are returned.

See config.String

func (*Config) Float64

func (config *Config) Float64(name string, def float64) (val float64, err error)

Float64 returns the value of the named prop as a `float64`. If the property is not found then the supplied default `def` and `ErrNotFound` are returned.

See config.String

func (*Config) Int

func (config *Config) Int(name string, def int) (val int, err error)

Int returns the value of the named prop as an `int`. If the property is not found then the supplied default `def` and `ErrNotFound` are returned.

See config.String

func (*Config) Int64

func (config *Config) Int64(name string, def int64) (val int64, err error)

Int64 returns the value of the named prop as an `int64`. If the property is not found then the supplied default `def` and `ErrNotFound` are returned.

See config.String

func (*Config) PrependSource

func (config *Config) PrependSource(srcs ...Source)

PrependSource inserts one or more `Sources` at the beginning of the list of sources such that the first source will be the source checked first when resolving a property value.

func (*Config) RemoveChangedListener

func (config *Config) RemoveChangedListener(l ChangedListener) error

RemoveChangedListener removes all instances of a ChangedListener. Returns `ErrNotFound` if the listener was not present.

func (*Config) Shutdown

func (config *Config) Shutdown()

Shutdown can be called to stop monitoring of all config sources.

func (*Config) String

func (config *Config) String(name string, def string) (val string, err error)

String returns the value of the named prop as a string. If the property is not found then the supplied default `def` and `ErrNotFound` are returned.

Each `Source` is checked, in the order they are added via `ApppendSource` and `PrependSource`, until a value for the property is found.

type Source

type Source interface {

	// GetProp fetches the value of a named property. The value is
	// returned as a string unless the name is not found, in which
	// case `ok=false` is returned.
	GetProp(name string) (val string, ok bool)
}

Source is the interface required for any source of name/value pairs.

type SourceMonitored

type SourceMonitored interface {

	// GetLastModified returns the time of the latest modification to any
	// property value within the source. If a source does not support
	// modifying properties at runtime then the zero value for `Time`
	// should be returned to ensure reload events are not generated.
	GetLastModified() time.Time

	// GetMonitorFreq returns the frequency as a `time.Duration` between
	// checks for changes to this config source.
	//
	// Returning zero (or less) will temporarily suspend calls to `GetLastModified`
	// and `GetMonitorFreq` will be called every 10 seconds until resumed, after which
	// `GetMontitorFreq` will be called at a frequency roughly equal to the `time.Duration`
	// returned.
	GetMonitorFreq() time.Duration
}

SourceMonitored is the interface required for any config source that is monitored for changes.

type SrcMap

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

SrcMap is a configuration `Source` backed by a simple map.

func NewSrcMap

func NewSrcMap() *SrcMap

NewSrcMap creates an empty `SrcMap`.

func NewSrcMapFromMap

func NewSrcMapFromMap(mapIn map[string]string) *SrcMap

NewSrcMapFromMap creates a `SrcMap` containing a copy of the specified map.

func (*SrcMap) GetLastModified

func (sm *SrcMap) GetLastModified() (last time.Time)

GetLastModified returns the time of the latest modification to any property value within the source. If a source does not support modifying properties at runtime then the zero value for `Time` should be returned to ensure reload events are not generated.

func (*SrcMap) GetMonitorFreq

func (sm *SrcMap) GetMonitorFreq() (freq time.Duration)

GetMonitorFreq returns the frequency as a `time.Duration` between checks for changes to this config source. Defaults to 1 minute unless changed with `GetMonitorFreq`.

func (*SrcMap) GetProp

func (sm *SrcMap) GetProp(name string) (val string, ok bool)

GetProp fetches the value of a named property. The value is returned as a string unless the name is not found, in which case `ok=false` is returned.

func (*SrcMap) Put

func (sm *SrcMap) Put(key string, val string)

Put inserts or updates a value in the `SrcMap`.

func (*SrcMap) PutAll

func (sm *SrcMap) PutAll(mapIn map[string]string)

PutAll inserts a copy of `mapIn` into the `SrcMap`

func (*SrcMap) SetMonitorFreq

func (sm *SrcMap) SetMonitorFreq(freq time.Duration)

SetMonitorFreq sets the frequency between checks for changes to this config source.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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