gconfig

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2022 License: MIT Imports: 15 Imported by: 4

README

gconfig

Documentation

Index

Constants

View Source
const (
	// DecoderFormatUnknown defines the value to be used to declare an
	// unknown config source format.
	DecoderFormatUnknown = "unknown"

	// DecoderFormatYAML defines the value to be used to declare a YAML
	// config source format.
	DecoderFormatYAML = "yaml"

	// DecoderFormatJSON defines the value to be used to declare a JSON
	// config source format.
	DecoderFormatJSON = "json"
)
View Source
const (
	// SourceTypeUnknown defines the value to be used to declare a
	// unknown config source type.
	SourceTypeUnknown = "unknown"

	// SourceTypeFile defines the value to be used to declare a
	// simple file config source type.
	SourceTypeFile = "file"

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

	// SourceTypeDirectory defines the value to be used to declare a
	// simple dir config source type.
	SourceTypeDirectory = "dir"

	// SourceTypeObservableDirectory defines the value to be used to
	// declare an observable directory config source type.
	SourceTypeObservableDirectory = "observable-dir"

	// SourceTypeRemote defines the value to be used to declare a
	// remote config source type.
	SourceTypeRemote = "remote"

	// SourceTypeObservableRemote defines the value to be used to
	// declare an observable remote config source type.
	SourceTypeObservableRemote = "observable-remote"

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

Variables

View Source
var (
	// ErrNilPointer defines a nil pointer argument error
	ErrNilPointer = fmt.Errorf("invalid nil pointer")

	// ErrConversion defines a type conversion error
	ErrConversion = fmt.Errorf("invalid type conversion")

	// 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")

	// ErrPathNotFound defines a path in Partial not found error.
	ErrPathNotFound = fmt.Errorf("config path not found")

	// ErrRemoteConfigPathNotFound defines a config path not found error.
	ErrRemoteConfigPathNotFound = fmt.Errorf("path of config not found")

	// ErrRemoteTimestampPathNotFound defines a config timestamp path not found error.
	ErrRemoteTimestampPathNotFound = fmt.Errorf("path of timestamp not found")

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

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

	// ErrInvalidSourceConfig defines an error that signal an
	// invalid source configuration Partial.
	ErrInvalidSourceConfig = fmt.Errorf("invalid config source config")
)
View Source
var (
	// DefaultFileFormat defines the file base config source default format
	// if the format is not present in the config.
	DefaultFileFormat = genv.String("GCONFIG_DEFAULT_FILE_FORMAT", DecoderFormatYAML)

	// DefaultRemoteFormat defines the remote base config source default format
	// if the format is not present in the config.
	DefaultRemoteFormat = genv.String("GCONFIG_DEFAULT_REMOTE_FORMAT", DecoderFormatJSON)

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

	// LoaderSourceID defines the id to be used as the default of the
	// entry config source id to be used as the loader entry.
	LoaderSourceID = genv.String("GCONFIG_LOADER_SOURCE_ID", "_sources")

	// LoaderSourcePath defines the entry config source path
	// to be used as the loader entry.
	LoaderSourcePath = genv.String("GCONFIG_LOADER_SOURCE_PATH", "config/config.yaml")

	// LoaderSourceFormat defines the entry config source format
	// to be used as the loader entry.
	LoaderSourceFormat = genv.String("GCONFIG_LOADER_SOURCE_FORMAT", DecoderFormatYAML)

	// LoaderSourceListPath defines the entry config source path of
	// loading sources.
	LoaderSourceListPath = genv.String("GCONFIG_LOADER_SOURCE_LIST_PATH", "configs")
)

Functions

This section is empty.

Types

type Config

type Config interface {
	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)
	Partial(path string, def ...Partial) (Partial, error)
}

Config defined an interface to an instance that holds configuration values

type Decoder

type Decoder interface {
	io.Closer
	Decode() (Config, 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 NewDecoderJSON

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

NewDecoderJSON instantiate a new yaml configuration decoder object used to parse a yaml configuration source into a config Partial.

func NewDecoderYAML

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

NewDecoderYAML instantiate a new yaml configuration decoder object used to parse a yaml configuration source into a config Partial.

type DecoderFactory

type DecoderFactory []DecoderStrategy

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

func (DecoderFactory) Create

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

func (*DecoderFactory) Register

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

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 DecoderStrategyJSON

type DecoderStrategyJSON struct{}

DecoderStrategyJSON defines a strategy used to instantiate a JSON config logStream decoder.

func (DecoderStrategyJSON) Accept

func (DecoderStrategyJSON) Accept(format string) bool

Accept will check if the decoder factory strategy can instantiate a decoder giving the format and the creation request parameters.

func (DecoderStrategyJSON) Create

func (DecoderStrategyJSON) Create(args ...interface{}) (Decoder, error)

Create will instantiate the desired decoder instance with the given reader instance as source of the content to decode.

type DecoderStrategyYAML

type DecoderStrategyYAML struct{}

DecoderStrategyYAML defines a strategy used to instantiate a YAML config logStream decoder.

func (DecoderStrategyYAML) Accept

func (DecoderStrategyYAML) Accept(format string) bool

Accept will check if the decoder factory strategy can instantiate a decoder giving the format and the creation request parameters.

func (DecoderStrategyYAML) Create

func (DecoderStrategyYAML) Create(args ...interface{}) (Decoder, error)

Create will instantiate the desired decoder instance with the given reader instance as source of the content to decode.

type HTTPClient

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTPClient defines the interface of an instance capable to perform the remote config obtain action

type Loader

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

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

func NewLoader

func NewLoader(cfg Manager, factory *SourceFactory) (*Loader, error)

NewLoader instantiate a new configuration loader.

func (Loader) Load

func (l Loader) Load() error

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

type Manager added in v1.2.0

type Manager interface {
	Config
	Sourced
	Observable
}

Manager defined an interface to a instance that manages configuration

func NewConfig

func NewConfig(period time.Duration) Manager

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 Observable added in v1.2.0

type Observable interface {
	io.Closer

	HasObserver(path string) bool
	AddObserver(path string, callback Observer) error
	RemoveObserver(path string)
}

Observable defined an interface to an instance that can observe configuration changes

type Observer

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

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

type Partial added in v1.2.0

type Partial map[interface{}]interface{}

Partial defines a section of a configuration information

func (*Partial) Bool added in v1.2.0

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 added in v1.3.0

func (p Partial) Clone() Partial

Clone will instantiate an identical instance of the original Partial

func (*Partial) Float added in v1.2.0

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 added in v1.2.0

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

func (*Partial) Has added in v1.2.0

func (p *Partial) Has(path string) bool

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

func (*Partial) Int added in v1.2.0

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 added in v1.2.0

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) Partial added in v1.2.0

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

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

func (*Partial) String added in v1.2.0

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

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

type Source

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

Source defines the base interface of a config source.

func NewSourceDir added in v1.4.0

func NewSourceDir(path, format string, recursive bool, fs afero.Fs, factory *DecoderFactory) (Source, error)

NewSourceDir instantiate a new source that loads several config files from a system file system directory.

func NewSourceEnv

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

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

func NewSourceFile

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

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

func NewSourceRemote

func NewSourceRemote(client HTTPClient, uri, format string, factory *DecoderFactory, configPath string) (Source, error)

NewSourceRemote @todo doc

type SourceFactory

type SourceFactory []SourceStrategy

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

func (SourceFactory) Create

func (f SourceFactory) Create(stype string, args ...interface{}) (Source, error)

Create will instantiate and return a new config source by the type requested.

func (SourceFactory) CreateFromConfig

func (f SourceFactory) CreateFromConfig(cfg Config) (Source, error)

CreateFromConfig 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

func (f *SourceFactory) Register(strategy SourceStrategy) error

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

type SourceObservable

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

SourceObservable 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 NewSourceObservableFile

func NewSourceObservableFile(path, format string, fs afero.Fs, factory *DecoderFactory) (SourceObservable, error)

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

func NewSourceObservableRemote

func NewSourceObservableRemote(client HTTPClient, uri, format string, factory *DecoderFactory, timestampPath, configPath string) (SourceObservable, error)

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

type SourceStrategy

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

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

func NewSourceStrategyDir added in v1.4.0

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

NewSourceStrategyDir instantiate a new dir source factory strategy that will enable the source factory to instantiate file configuration sources from a system directory.

func NewSourceStrategyFile

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

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

func NewSourceStrategyObservableFile

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

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

func NewSourceStrategyObservableRemote

func NewSourceStrategyObservableRemote(decoderFactory *DecoderFactory) (SourceStrategy, error)

NewSourceStrategyObservableRemote instantiate a new observable remote source factory strategy that will enable the source factory to instantiate a new remote configuration source.

func NewSourceStrategyRemote

func NewSourceStrategyRemote(decoderFactory *DecoderFactory) (SourceStrategy, error)

NewSourceStrategyRemote instantiate a new remote source factory strategy that will enable the source factory to instantiate a new remote configuration source.

type SourceStrategyEnv

type SourceStrategyEnv struct{}

SourceStrategyEnv defines an environment config source instantiation strategy to be used by the config sources factory instance.

func (SourceStrategyEnv) Accept

func (SourceStrategyEnv) Accept(stype string) bool

Accept will check if the source factory strategy can instantiate a new source of the requested type.

func (SourceStrategyEnv) AcceptFromConfig

func (s SourceStrategyEnv) AcceptFromConfig(cfg Config) bool

AcceptFromConfig will check if the source factory strategy can instantiate a source where the data to check comes from a configuration Partial instance.

func (SourceStrategyEnv) Create

func (s SourceStrategyEnv) Create(args ...interface{}) (Source, error)

Create will instantiate the desired environment source instance.

func (SourceStrategyEnv) CreateFromConfig

func (s SourceStrategyEnv) CreateFromConfig(cfg Config) (Source, error)

CreateFromConfig will instantiate the desired environment source instance where the initialization data comes from a configuration Partial instance.

type Sourced added in v1.2.0

type Sourced interface {
	io.Closer

	HasSource(id string) bool
	AddSource(id string, priority int, src Source) error
	RemoveSource(id string) error
	RemoveAllSources() error
	Source(id string) (Source, error)
	SourcePriority(id string, priority int) error
}

Sourced defined an interface to an instance that holds configuration values from different sources

Jump to

Keyboard shortcuts

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