config

package
v1.15.1 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2021 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// FileSourceType defines the value to be used to declare a
	// simple file config source type.
	FileSourceType = "file"

	// ObservableFileSourceType defines the value to be used to
	// declare an observable file config source type.
	ObservableFileSourceType = "observable_file"

	// EnvSourceType defines the value to be used to declare an
	// environment config source type.
	EnvSourceType = "env"

	// YamlDecoderFormat defines the value to be used to declare a YAML
	// config source format.
	YamlDecoderFormat = "yaml"
)
View Source
const (
	// ContainerID defines the id to be used as the container
	// registration id of a config instance, as a base id of all other config
	// package instances registered in the application container.
	ContainerID = gapp.ContainerID + ".config"

	// ContainerDecoderStrategyTag defines the tag to be assigned to all
	// container decoders strategies.
	ContainerDecoderStrategyTag = ContainerID + ".decoder.strategy"

	// ContainerYamlDecoderStrategyID defines the id to be used as the
	// container registration id of a yaml config decoder factory strategy
	// instance.
	ContainerYamlDecoderStrategyID = ContainerID + ".decoder.strategy.yaml"

	// ContainerDecoderFactoryID defines the id to be used as the
	// container registration id of a config decoder factory instance.
	ContainerDecoderFactoryID = ContainerID + ".decoder.factory"

	// ContainerSourceStrategyTag defines the tag to be assigned to all
	// container source strategies.
	ContainerSourceStrategyTag = ContainerID + ".source.strategy"

	// ContainerFileSourceStrategyID defines the id to be used as the
	// container registration id of a config file source factory strategy
	// instance.
	ContainerFileSourceStrategyID = ContainerID + ".source.strategy.file"

	// ContainerObservableFileSourceStrategyID defines the id to be used
	// as the container registration id of a config observable file source
	// factory strategy instance.
	ContainerObservableFileSourceStrategyID = ContainerID + ".source.strategy.observable_file"

	// ContainerEnvSourceStrategyID defines the id to be used as
	// the container registration id of a config environment source
	// factory strategy instance.
	ContainerEnvSourceStrategyID = ContainerID + ".source.strategy.environment"

	// ContainerSourceFactoryID defines the id to be used as the
	// container registration id config source factory instance.
	ContainerSourceFactoryID = ContainerID + ".source.factory"

	// ContainerLoaderID defines the id to be used as the container
	// registration id of a config loader instance.
	ContainerLoaderID = ContainerID + ".loader"
)
View Source
const (
	// Env defines the gapp/config package environment entry id base
	// string.
	Env = gapp.Env + "_CONFIG"

	// EnvObserveFrequency defines the name of the environment variable
	// to be checked for an overriding value for the config observe frequency
	// in seconds.
	EnvObserveFrequency = Env + "_OBSERVE_FREQUENCY"

	// EnvLoaderActive defines the name of the environment variable
	// to be checked for an overriding value for the config loader
	// activation.
	EnvLoaderActive = Env + "_LOADER_ACTIVE"

	// EnvLoaderSourceID defines the name of the environment variable
	// to be checked for an overriding value for the config loader entry
	// source id.
	EnvLoaderSourceID = Env + "_LOADER_SOURCE_ID"

	// EnvLoaderSourcePath defines the name of the environment variable
	// to be checked for an overriding value for the config loader entry
	// source path.
	EnvLoaderSourcePath = Env + "_LOADER_SOURCE_PATH"

	// EnvLoaderSourceFormat defines the name of the environment variable
	// to be checked for an overriding value for the config loader entry
	// source format.
	EnvLoaderSourceFormat = Env + "_LOADER_SOURCE_FORMAT"
)

Variables

View Source
var (
	// ObserveFrequency defines the id to be used as the default of a
	// config observable source frequency time in seconds.
	ObserveFrequency = 0

	// LoaderActive defines if the config loader should be executed
	// while the provider boot
	LoaderActive = true

	// LoaderSourceID defines the id to be used as the default of the
	// entry config source id to be used as the loader entry.
	LoaderSourceID = "main"

	// LoaderSourcePath defines the entry config source path
	// to be used as the loader entry.
	LoaderSourcePath = "config/config.yaml"

	// LoaderSourceFormat defines the entry config source format
	// to be used as the loader entry.
	LoaderSourceFormat = YamlDecoderFormat
)

Functions

func NewProvider

func NewProvider() gapp.Provider

NewProvider will create a new configuration provider instance used to register the basic configuration objects in the application container.

Types

type Config

type Config interface {
	io.Closer
	Has(path string) bool
	Get(path string, def ...interface{}) interface{}
	Bool(path string, def ...bool) bool
	Int(path string, def ...int) int
	Int8(path string, def ...int8) int8
	Int16(path string, def ...int16) int16
	Int32(path string, def ...int32) int32
	Int64(path string, def ...int64) int64
	UInt(path string, def ...uint) uint
	UInt8(path string, def ...uint8) uint8
	UInt16(path string, def ...uint16) uint16
	UInt32(path string, def ...uint32) uint32
	UInt64(path string, def ...uint64) uint64
	Float32(path string, def ...float32) float32
	Float64(path string, def ...float64) float64
	Complex64(path string, def ...complex64) complex64
	Complex128(path string, def ...complex128) complex128
	Rune(path string, def ...rune) rune
	String(path string, def ...string) string
	List(path string, def ...[]interface{}) []interface{}
	Partial(path string, def ...Partial) Partial
	HasSource(id string) bool
	AddSource(id string, priority int, src Source) error
	RemoveSource(id string) error
	Source(id string) (Source, error)
	SourcePriority(id string, priority int) error
	HasObserver(path string) bool
	AddObserver(path string, callback Observer) error
	RemoveObserver(path string)
}

Config defines the instance of a configuration managing structure.

func NewConfig

func NewConfig(period time.Duration) (Config, error)

NewConfig instantiate a new configuration object. This object will manage a series of sources, alongside of the ability of registration of configuration path/values observer callbacks that will be called whenever the value has changed.

type Decoder

type Decoder interface {
	io.Closer
	Decode() (Partial, error)
}

Decoder interface defines the interaction methods to a config content decoder used to parse the source content into an application usable configuration partial instance.

func NewYamlDecoder added in v1.11.0

func NewYamlDecoder(reader io.Reader) (Decoder, error)

NewYamlDecoder instantiate a new yaml configuration decoder object used to parse a yaml configuration source into a config partial.

type DecoderFactory

type DecoderFactory interface {
	Register(strategy DecoderStrategy) error
	Create(format string, args ...interface{}) (Decoder, error)
}

DecoderFactory defined the instance used to instantiate a new config logStream decoder for a specific encoding format.

func NewDecoderFactory

func NewDecoderFactory() DecoderFactory

NewDecoderFactory instantiate a new decoder factory.

type DecoderStrategy

type DecoderStrategy interface {
	Accept(format string, args ...interface{}) bool
	Create(args ...interface{}) (Decoder, error)
}

DecoderStrategy interface defines the methods of the decoder factory strategy that can validate creation requests and instantiation of a particular decoder.

func NewYamlDecoderStrategy added in v1.11.0

func NewYamlDecoderStrategy() DecoderStrategy

NewYamlDecoderStrategy instantiate a new yaml decoder factory strategy that will enable the decoder factory to instantiate a new yaml decoder.

type Loader

type Loader interface {
	Load(id string, path string, format string) error
}

Loader defines the config instantiation and initialization of a new config managing structure.

func NewLoader

func NewLoader(cfg Config, factory SourceFactory) (Loader, error)

NewLoader instantiate a new configuration loader.

type ObservableSource

type ObservableSource interface {
	Source
	Reload() (bool, error)
}

ObservableSource interface extends the Source interface with methods specific to sources that will be checked for updates in a regular periodicity defined in the config object where the source will be registered.

func NewObservableFileSource

func NewObservableFileSource(path string, format string, fs afero.Fs, factory DecoderFactory) (ObservableSource, error)

NewObservableFileSource instantiate a new source that treats a file as the origin of the configuration content. This file source will be periodically checked for changes and loaded if so.

type Observer

type Observer func(interface{}, interface{})

Observer callback function used to be called when an observed configuration path has changed.

type Partial

type Partial map[interface{}]interface{}

Partial defined a type used to store configuration information.

func (Partial) Get

func (p Partial) Get(path string, def ...interface{}) interface{}

Get will retrieve the value stored in the requested path. If the path does not exist, then the value nil will be returned. Or, if a default value was given as the optional extra argument, then it will be returned instead of the standard nil value.

func (Partial) Has

func (p Partial) Has(path string) bool

Has will check if a requested path exists in the config partial.

func (Partial) Int

func (p Partial) Int(path string, def ...int) int

Int will return the casting to int of the stored value in the requested path. If the value retrieved was not found or returned nil, then the default optional argument will be returned if given.

func (Partial) List added in v1.11.0

func (p Partial) List(path string, def ...[]interface{}) []interface{}

List will return the casting to a list of the stored value in the requested path. If the value retrieved was not found or returned nil, then the default optional argument will be returned if given.

func (Partial) Partial added in v1.11.0

func (p Partial) Partial(path string, def ...Partial) Partial

Partial will return the casting to a config partial of the stored value in the requested path. If the value retrieved was not found or returned nil, then the default optional argument will be returned if given.

func (Partial) String

func (p Partial) String(path string, def ...string) string

String will return the casting to string of the stored value in the requested path. If the value retrieved was not found or returned nil, then the default optional argument will be returned if given.

type Source

type Source interface {
	io.Closer
	Has(path string) bool
	Get(path string) interface{}
}

Source defines the base interface of a config source.

func NewEnvSource

func NewEnvSource(mappings map[string]string) (Source, error)

NewEnvSource instantiate a new source that read a list of environment variables into mapped config paths.

func NewFileSource

func NewFileSource(path string, format string, fs afero.Fs, factory DecoderFactory) (Source, error)

NewFileSource instantiate a new source that treats a file as the origin of the configuration content.

type SourceFactory

type SourceFactory interface {
	Register(strategy SourceStrategy) error
	Create(sourceType string, args ...interface{}) (Source, error)
	CreateFromConfig(cfg Partial) (Source, error)
}

SourceFactory defines a config source factory that uses a list of registered instantiation strategies to perform the config source instantiation.

func NewSourceFactory

func NewSourceFactory() SourceFactory

NewSourceFactory instantiate a new source factory.

type SourceStrategy

type SourceStrategy interface {
	Accept(sourceType string, args ...interface{}) bool
	AcceptFromConfig(cfg Partial) bool
	Create(args ...interface{}) (Source, error)
	CreateFromConfig(cfg Partial) (Source, error)
}

SourceStrategy interface defines the methods of the source factory strategy that will be used instantiate a particular source type.

func NewEnvSourceStrategy

func NewEnvSourceStrategy() SourceStrategy

NewEnvSourceStrategy instantiate a new environment source factory strategy that will enable the source factory to instantiate a new observable file configuration source.

func NewFileSourceStrategy

func NewFileSourceStrategy(fs afero.Fs, factory DecoderFactory) (SourceStrategy, error)

NewFileSourceStrategy instantiate a new file source factory strategy that will enable the source factory to instantiate a new file configuration source.

func NewObservableFileSourceStrategy

func NewObservableFileSourceStrategy(fs afero.Fs, factory DecoderFactory) (SourceStrategy, error)

NewObservableFileSourceStrategy instantiate a new observable file source factory strategy that will enable the source factory to instantiate a new observable file configuration source.

Jump to

Keyboard shortcuts

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