config

package
v0.22.0 Latest Latest
Warning

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

Go to latest
Published: May 10, 2023 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package config define instances an application provider that publicise services used to manage an application configuration.

Index

Constants

View Source
const (
	// ID 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.
	ID = slate.ID + ".config"

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

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

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

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

	// LoaderID defines the id to be used as the container
	// registration id of a config loader instance.
	LoaderID = ID + ".loader"
)
View Source
const (
	// EnvID defines the slate.config package base environment variable name.
	EnvID = slate.EnvID + "_CONFIG"
)
View Source
const (
	// UnknownDecoderFormat defines the value to be used to
	// declare an unknown config source format.
	UnknownDecoderFormat = "unknown"
)
View Source
const (
	// UnknownSourceType defines the value to be used to declare an
	// unknown config source type.
	UnknownSourceType = "unknown"
)

Variables

View Source
var (
	// DefaultFileFormat defines the file base config source def
	// format if the format is not present in the config.
	DefaultFileFormat = env.String(EnvID+"_DEFAULT_FILE_FORMAT", "yaml")

	// DefaultRestFormat defines the rest base config source def
	// format if the format is not present in the config.
	DefaultRestFormat = env.String(EnvID+"_DEFAULT_REST_FORMAT", "json")

	// PathSeparator defines the element(s) that will be used to split
	// a config path string into path elements.
	PathSeparator = env.String(EnvID+"_PATH_SEPARATOR", ".")

	// LoaderActive defines if the config loader should be executed
	// while the provider boot
	LoaderActive = env.Bool(EnvID+"_LOADER_ACTIVE", true)

	// LoaderSourceID defines the id to be used as the def of the
	// entry config source id to be used as the loader entry.
	LoaderSourceID = env.String(EnvID+"_LOADER_SOURCE_ID", "_sources")

	// LoaderSourcePath defines the entry config source path
	// to be used as the loader entry.
	LoaderSourcePath = env.String(EnvID+"_LOADER_SOURCE_PATH", "config/sources.yaml")

	// LoaderSourceFormat defines the entry config source format
	// to be used as the loader entry.
	LoaderSourceFormat = env.String(EnvID+"_LOADER_SOURCE_FORMAT", "yaml")

	// LoaderSourceListPath defines the entry config source path of
	// loading sources.
	LoaderSourceListPath = env.String(EnvID+"_LOADER_SOURCE_LIST_PATH", "slate.config.sources")

	// ObserveFrequency defines the id to be used as the def of a
	// config observable source frequency time in seconds.
	ObserveFrequency = env.Int(EnvID+"_OBSERVE_FREQUENCY", 0)
)
View Source
var (
	// ErrPathNotFound defines a path in Config not found error.
	ErrPathNotFound = fmt.Errorf("config path not found")

	// ErrInvalidFormat defines an error that signal an
	// unexpected/unknown config source decoder format.
	ErrInvalidFormat = fmt.Errorf("invalid config format")

	// ErrInvalidSource defines an error that signal an
	// unexpected/unknown config source type.
	ErrInvalidSource = fmt.Errorf("invalid config source")

	// ErrSourceNotFound defines a source config source not found error.
	ErrSourceNotFound = fmt.Errorf("config source not found")

	// ErrDuplicateSource defines a duplicate config source
	// registration attempt.
	ErrDuplicateSource = fmt.Errorf("config source already registered")
)

Functions

func Convert added in v0.22.0

func Convert(
	val interface{},
) interface{}

Convert will try to convert a source argument value into a config accepted value. This means that if the value is a map, then it will be converted into a Config instance (recursively)

Types

type Config added in v0.16.0

type Config map[interface{}]interface{}

Config defines a section of a configuration information

func (*Config) Bool added in v0.16.0

func (c *Config) Bool(
	path string,
	def ...bool,
) (bool, error)

Bool will retrieve a value stored in the quested path cast to bool

func (*Config) Clone added in v0.16.0

func (c *Config) Clone() Config

Clone will instantiate an identical instance of the original Config

func (*Config) Config added in v0.16.0

func (c *Config) Config(
	path string,
	def ...Config,
) (IConfig, error)

Config will retrieve a value stored in the quested path cast to config

func (*Config) Entries added in v0.19.0

func (c *Config) Entries() []string

Entries will retrieve the list of stored entries in the configuration.

func (*Config) Float added in v0.16.0

func (c *Config) Float(
	path string,
	def ...float64,
) (float64, error)

Float will retrieve a value stored in the quested path cast to float

func (*Config) Get added in v0.16.0

func (c *Config) Get(
	path string,
	def ...interface{},
) (interface{}, error)

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 def value was given as the optional extra argument, then it will be returned instead of the standard nil value.

func (*Config) Has added in v0.16.0

func (c *Config) Has(
	path string,
) bool

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

func (*Config) Int added in v0.16.0

func (c *Config) Int(
	path string,
	def ...int,
) (int, error)

Int will retrieve a value stored in the quested path cast to int

func (*Config) List added in v0.16.0

func (c *Config) List(
	path string,
	def ...[]interface{},
) ([]interface{}, error)

List will retrieve a value stored in the quested path cast to list

func (*Config) Merge added in v0.22.0

func (c *Config) Merge(
	src Config,
)

Merge will increment the current config instance with the information stored in another config.

func (*Config) Populate added in v0.16.0

func (c *Config) Populate(
	path string,
	data interface{},
	icase ...bool,
) (interface{}, error)

Populate will try to populate the data argument with the data stored in the path config location.

func (*Config) String added in v0.16.0

func (c *Config) String(
	path string,
	def ...string,
) (string, error)

String will retrieve a value stored in the quested path cast to string

type DecoderFactory added in v0.16.0

type DecoderFactory []IDecoderStrategy

DecoderFactory defines a decoder instantiation factory.

func (*DecoderFactory) Create added in v0.16.0

func (f *DecoderFactory) Create(
	format string,
	args ...interface{},
) (IDecoder, error)

Create will instantiate the requested new decoder capable to parse the formatted content into a usable configuration.

func (*DecoderFactory) Register added in v0.16.0

func (f *DecoderFactory) Register(
	strategy IDecoderStrategy,
) error

Register will store a new decoder factory strategy to be used to evaluate a request of an instance capable to parse a specific format. If the strategy accepts the format, then it will be used to instantiate the appropriate decoder that will be used to decode the configuration content.

type IConfig

type IConfig interface {
	Entries() []string
	Has(path string) bool
	Get(path string, def ...interface{}) (interface{}, error)
	Bool(path string, def ...bool) (bool, error)
	Int(path string, def ...int) (int, error)
	Float(path string, def ...float64) (float64, error)
	String(path string, def ...string) (string, error)
	List(path string, def ...[]interface{}) ([]interface{}, error)
	Config(path string, def ...Config) (IConfig, error)
	Populate(path string, data any, icase ...bool) (any, error)
}

IConfig defined an interface to an instance that holds configuration values

type IDecoder

type IDecoder interface {
	io.Closer

	Decode() (IConfig, error)
}

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

type IDecoderFactory

type IDecoderFactory interface {
	Register(strategy IDecoderStrategy) error
	Create(format string, args ...interface{}) (IDecoder, error)
}

IDecoderFactory defined the interface of a config decoder factory instance.

func NewDecoderFactory added in v0.20.0

func NewDecoderFactory() IDecoderFactory

NewDecoderFactory will instantiate a new decoder factory instance.

type IDecoderStrategy

type IDecoderStrategy interface {
	Accept(format string) bool
	Create(args ...interface{}) (IDecoder, error)
}

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

type ILoader

type ILoader interface {
	Load() error
}

ILoader defines the interface of a config loader instance.

type IManager

type IManager interface {
	io.Closer
	IConfig

	HasSource(id string) bool
	AddSource(id string, priority int, src ISource) error
	RemoveSource(id string) error
	RemoveAllSources() error
	Source(id string) (ISource, error)
	SourcePriority(id string, priority int) error
	HasObserver(path string) bool
	AddObserver(path string, callback IObserver) error
	RemoveObserver(path string)
}

IManager defined an interface to an instance that manages configuration

func NewManager

func NewManager() IManager

NewManager 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 IObsSource added in v0.22.0

type IObsSource interface {
	ISource
	Reload() (bool, error)
}

IObsSource interface extends the ISource 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.

type IObserver

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

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

type ISource

type ISource interface {
	Has(path string) bool
	Get(path string, def ...interface{}) (interface{}, error)
}

ISource defines the base interface of a config source.

type ISourceFactory

type ISourceFactory interface {
	Register(strategy ISourceStrategy) error
	Create(cfg IConfig) (ISource, error)
}

ISourceFactory defined the interface of a config source factory instance.

func NewSourceFactory added in v0.20.0

func NewSourceFactory() ISourceFactory

NewSourceFactory will instantiate a new source factory instance

type ISourceStrategy

type ISourceStrategy interface {
	Accept(config IConfig) bool
	Create(config IConfig) (ISource, error)
}

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

type Loader added in v0.16.0

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

Loader defines an object responsible to initialize a configuration manager.

func NewLoader added in v0.16.0

func NewLoader(
	manager IManager,
	sourceFactory ISourceFactory,
) (*Loader, error)

NewLoader instantiate a new configuration loader instance.

func (Loader) Load added in v0.16.0

func (l Loader) Load() error

Load loads the configuration from a base config file defined by a path and format.

type Manager added in v0.16.0

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

Manager defines an object responsible to manage the application several defines sources and config values observers.

func (*Manager) AddObserver added in v0.16.0

func (m *Manager) AddObserver(
	path string,
	callback IObserver,
) error

AddObserver register a new observer to a configuration path.

func (*Manager) AddSource added in v0.16.0

func (m *Manager) AddSource(
	id string,
	priority int,
	src ISource,
) error

AddSource register a new source with a specific id with a given priority.

func (*Manager) Bool added in v0.16.0

func (m *Manager) Bool(
	path string,
	def ...bool,
) (bool, error)

Bool will retrieve a bool configuration value loaded from a source.

func (*Manager) Close added in v0.16.0

func (m *Manager) Close() error

Close terminates the config instance. This will stop the observer trigger and call close on all registered sources.

func (*Manager) Config added in v0.16.0

func (m *Manager) Config(
	path string,
	def ...Config,
) (IConfig, error)

Config will retrieve config values loaded from a source.

func (*Manager) Entries added in v0.19.0

func (m *Manager) Entries() []string

Entries will retrieve the list of stored entries any registered source.

func (*Manager) Float added in v0.16.0

func (m *Manager) Float(
	path string,
	def ...float64,
) (float64, error)

Float will retrieve a floating point configuration value loaded from a source.

func (*Manager) Get added in v0.16.0

func (m *Manager) Get(
	path string,
	def ...interface{},
) (interface{}, error)

Get will retrieve a configuration value loaded from a source.

func (*Manager) Has added in v0.16.0

func (m *Manager) Has(
	path string,
) bool

Has will check if a path has been loaded. This means that if the values has been loaded by any registered source.

func (*Manager) HasObserver added in v0.16.0

func (m *Manager) HasObserver(
	path string,
) bool

HasObserver check if there is an observer to a configuration value path.

func (*Manager) HasSource added in v0.16.0

func (m *Manager) HasSource(
	id string,
) bool

HasSource check if a source with a specific id has been registered.

func (*Manager) Int added in v0.16.0

func (m *Manager) Int(
	path string,
	def ...int,
) (int, error)

Int will retrieve an integer configuration value loaded from a source.

func (*Manager) List added in v0.16.0

func (m *Manager) List(
	path string,
	def ...[]interface{},
) ([]interface{}, error)

List will retrieve a list configuration value loaded from a source.

func (*Manager) Populate added in v0.16.0

func (m *Manager) Populate(
	path string,
	data interface{},
	icase ...bool,
) (interface{}, error)

Populate will retrieve a config value loaded from a source.

func (*Manager) RemoveAllSources added in v0.16.0

func (m *Manager) RemoveAllSources() error

RemoveAllSources remove all the registered sources from the registration list of the configuration. This will also update the configuration content and re-validate the observed paths.

func (*Manager) RemoveObserver added in v0.16.0

func (m *Manager) RemoveObserver(
	path string,
)

RemoveObserver remove an observer to a configuration path.

func (*Manager) RemoveSource added in v0.16.0

func (m *Manager) RemoveSource(
	id string,
) error

RemoveSource remove a source from the registration list of the configuration. This will also update the configuration content and re-validate the observed paths.

func (*Manager) Source added in v0.16.0

func (m *Manager) Source(
	id string,
) (ISource, error)

Source retrieve a previously registered source with a requested id.

func (*Manager) SourcePriority added in v0.16.0

func (m *Manager) SourcePriority(
	id string,
	priority int,
) error

SourcePriority set a priority value of a previously registered source with the specified id. This may change the defined values if there was an override process of the configuration paths of the changing source.

func (*Manager) String added in v0.16.0

func (m *Manager) String(
	path string,
	def ...string,
) (string, error)

String will retrieve a string configuration value loaded from a source.

type Provider

type Provider struct{}

Provider defines the slate.config module service provider to be used on the application initialization to register the config service.

func (Provider) Boot

func (p Provider) Boot(
	container slate.IContainer,
) (e error)

Boot will start the configuration config instance by calling the configuration loader with the defined provider base entry information.

func (Provider) Register

func (Provider) Register(
	container slate.IContainer,
) error

Register will register the configuration module instances in the application container.

type SourceFactory added in v0.16.0

type SourceFactory []ISourceStrategy

SourceFactory defines an object responsible to instantiate a new config source.

func (*SourceFactory) Create added in v0.16.0

func (f *SourceFactory) Create(
	config IConfig,
) (ISource, error)

Create will instantiate and return a new config source where the data used to decide the strategy to be used and also the initialization data comes from a configuration storing Partial instance.

func (*SourceFactory) Register added in v0.16.0

func (f *SourceFactory) Register(
	strategy ISourceStrategy,
) error

Register will register a new source factory strategy to be used on creation request.

Directories

Path Synopsis
Package decoder defines the base types and structures of all configuration decoding functionalities.
Package decoder defines the base types and structures of all configuration decoding functionalities.
json
Package json defines the JSON format decoder and decoder creation strategy to be integrated into the config package decoder factory instance.
Package json defines the JSON format decoder and decoder creation strategy to be integrated into the config package decoder factory instance.
yaml
Package yaml defines the YAML format decoder and decoder creation strategy to be integrated into the config package decoder factory instance.
Package yaml defines the YAML format decoder and decoder creation strategy to be integrated into the config package decoder factory instance.
Package source defines the base types and structures of all configuration source functionalities.
Package source defines the base types and structures of all configuration source functionalities.
aggregate
Package aggregate defines the aggregate source creation strategy to be integrated into the config package source factory instance.
Package aggregate defines the aggregate source creation strategy to be integrated into the config package source factory instance.
dir
Package dir defines the directory source creation strategy to be integrated into the config package source factory instance.
Package dir defines the directory source creation strategy to be integrated into the config package source factory instance.
env
Package env defines the environment source creation strategy to be integrated into the config package source factory instance.
Package env defines the environment source creation strategy to be integrated into the config package source factory instance.
file
Package file defines the file type sources creation strategies to be integrated into the config package source factory instance.
Package file defines the file type sources creation strategies to be integrated into the config package source factory instance.
rest
Package rest defines the REST connection source creation strategy to be integrated into the config package source factory instance.
Package rest defines the REST connection source creation strategy to be integrated into the config package source factory instance.

Jump to

Keyboard shortcuts

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