config

package
v0.27.0 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 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 observer instance.
	LoaderID = ID + ".observer"
)
View Source
const (
	// EnvID defines the slate.config package base environment variable name.
	EnvID = slate.EnvID + "_CONFIG"
)
View Source
const (
	// UnknownDecoder defines the value to be used to
	// declare an unknown partial source format.
	UnknownDecoder = "unknown"
)
View Source
const (
	// UnknownSource defines the value to be used to declare an
	// unknown partial source type.
	UnknownSource = "unknown"
)

Variables

View Source
var (
	// DefaultFileFormat defines the file base config source simple
	// 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 simple
	// 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 observer should be executed
	// while the provider boot
	LoaderActive = env.Bool(EnvID+"_LOADER_ACTIVE", true)

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

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

	// LoaderSourceFormat defines the entry config source format
	// to be used as the observer 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 simple of a
	// config observable source frequency time in seconds.
	ObserveFrequency = env.Int(EnvID+"_OBSERVE_FREQUENCY", 0)
)
View Source
var (
	// ErrInvalidEmptyPath defines an invalid empty path in Partial
	// assigning error.
	ErrInvalidEmptyPath = fmt.Errorf("invalid empty config path")

	// ErrPathNotFound defines a path in Partial 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 partial accepted value. This means that if the value is a map, then it will be converted into a Partial instance (recursively)

Types

type Config added in v0.16.0

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

Config defines an object responsible to config the application several defines sources and partial values observers.

func NewConfig added in v0.23.0

func NewConfig() *Config

NewConfig instantiate a new configuration object. This object will config 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.

func (*Config) AddObserver added in v0.23.0

func (c *Config) AddObserver(
	path string,
	callback Observer,
) error

AddObserver register a new observer to a configuration path.

func (*Config) AddSource added in v0.23.0

func (c *Config) AddSource(
	id string,
	priority int,
	src Source,
) error

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

func (*Config) Bool added in v0.16.0

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

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

func (*Config) Close added in v0.23.0

func (c *Config) Close() error

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

func (*Config) Entries added in v0.19.0

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

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

func (*Config) Float added in v0.16.0

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

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

func (*Config) Get added in v0.16.0

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

Get will retrieve a configuration value loaded from a source.

func (*Config) Has added in v0.16.0

func (c *Config) 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 (*Config) HasObserver added in v0.23.0

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

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

func (*Config) HasSource added in v0.23.0

func (c *Config) HasSource(
	id string,
) bool

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

func (*Config) Int added in v0.16.0

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

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

func (*Config) List added in v0.16.0

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

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

func (*Config) Partial added in v0.23.0

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

Partial will retrieve partial values loaded from a source.

func (*Config) Populate added in v0.16.0

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

Populate will retrieve a config value loaded from a source.

func (*Config) RemoveAllSources added in v0.23.0

func (c *Config) 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 (*Config) RemoveObserver added in v0.23.0

func (c *Config) RemoveObserver(
	path string,
)

RemoveObserver remove an observer to a configuration path.

func (*Config) RemoveSource added in v0.23.0

func (c *Config) 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 (*Config) Source added in v0.23.0

func (c *Config) Source(
	id string,
) (Source, error)

Source retrieve a previously registered source with a requested id.

func (*Config) SourcePriority added in v0.23.0

func (c *Config) 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 (*Config) String added in v0.16.0

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

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

type Decoder added in v0.23.0

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

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

type DecoderFactory added in v0.16.0

type DecoderFactory []DecoderStrategy

DecoderFactory defines a decoder instantiation factory.

func NewDecoderFactory added in v0.20.0

func NewDecoderFactory() *DecoderFactory

NewDecoderFactory will instantiate a new decoder factory instance.

func (*DecoderFactory) Create added in v0.16.0

func (f *DecoderFactory) Create(
	format string,
	args ...interface{},
) (Decoder, 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 DecoderStrategy,
) 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 DecoderStrategy added in v0.23.0

type DecoderStrategy interface {
	Accept(format string) 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.

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(
	configurer *Config,
	sourceCreator *SourceFactory,
) (*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 partial file defined by a path and format.

type ObsSource added in v0.23.0

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

ObsSource 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.

type Observer added in v0.23.0

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 defines a section of a configuration information

func (*Partial) Bool

func (p *Partial) Bool(
	path string,
	def ...bool,
) (bool, error)

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

func (*Partial) Clone

func (p *Partial) Clone() Partial

Clone will instantiate an identical instance of the original Partial

func (*Partial) Entries added in v0.23.0

func (p *Partial) Entries() []string

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

func (*Partial) Float

func (p *Partial) Float(
	path string,
	def ...float64,
) (float64, error)

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

func (*Partial) Get

func (p *Partial) 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 simple 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 partial.

func (*Partial) Int

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

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

func (*Partial) List

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

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

func (*Partial) Merge added in v0.23.0

func (p *Partial) Merge(
	src Partial,
)

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

func (*Partial) Partial

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

Partial will retrieve a value stored in the quested path cast to partial

func (*Partial) Populate added in v0.23.0

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

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

func (*Partial) Set added in v0.26.0

func (p *Partial) Set(
	path string,
	value interface{},
) (*Partial, error)

Set will store a value in the requested partial path.

func (*Partial) String

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

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

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.Container,
) (e error)

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

func (Provider) Register

func (Provider) Register(
	container *slate.Container,
) error

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

type Source added in v0.16.0

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

Source defines the base interface of a partial source.

type SourceFactory added in v0.16.0

type SourceFactory []SourceStrategy

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

func NewSourceFactory added in v0.20.0

func NewSourceFactory() *SourceFactory

NewSourceFactory will instantiate a new source factory instance

func (*SourceFactory) Create added in v0.16.0

func (f *SourceFactory) Create(
	cfg Partial,
) (Source, error)

Create will instantiate and return a new partial 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 SourceStrategy,
) error

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

type SourceStrategy added in v0.23.0

type SourceStrategy interface {
	Accept(cfg Partial) bool
	Create(cfg Partial) (Source, error)
}

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

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