gconfig

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2022 License: MIT Imports: 16 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"

	// 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 = errors.New("invalid nil pointer")

	// ErrConversion defines a type conversion error
	ErrConversion = errors.New("invalid type conversion")

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

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

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

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

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

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

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

	// ErrInvalidSourceConfig defines an error that signal an
	// invalid source configuration partial.
	ErrInvalidSourceConfig = fmt.Errorf("invalid SourcedConfig source SourcedConfig")
)
View Source
var (
	// 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)
	Config(path string, def ...Config) (Config, 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 SourcedConfig 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 SourcedConfig 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 SourcedConfig partial.

type DecoderFactory

type DecoderFactory []DecoderStrategy

DecoderFactory defined the instance used to instantiate a new SourcedConfig 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 SourcedConfig 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 SourcedConfig 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 SourcedConfig obtain action

type Loader

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

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

func NewLoader

func NewLoader(cfg *SourcedConfig, 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 SourcedConfig file defined by a path and format.

type Observer

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

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

type Source

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

Source defines the base interface of a SourcedConfig source.

func NewSourceEnv

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

NewSourceEnv instantiate a new source that read a list of environment variables into mapped SourcedConfig 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 SourcedConfig source factory that uses a list of registered instantiation strategies to perform the SourcedConfig source instantiation.

func (SourceFactory) Create

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

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

func (SourceFactory) CreateFromConfig

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

CreateFromConfig will instantiate and return a new SourcedConfig 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 SourcedConfig 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 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 SourcedConfig source instantiation strategy to be used by the SourcedConfig 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. Also, validates that there is the path and content format extra parameters, and if they are strings.

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 SourcedConfig

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

SourcedConfig defines the instance of a configuration managing structure.

func NewConfig

func NewConfig(period time.Duration) *SourcedConfig

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.

func (*SourcedConfig) AddObserver

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

AddObserver register a new observer to a configuration path.

func (*SourcedConfig) AddSource

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

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

func (*SourcedConfig) Bool

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

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

func (*SourcedConfig) Close

func (c *SourcedConfig) Close() error

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

func (*SourcedConfig) Config

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

Config will retrieve a SourcedConfig configuration value loaded from a source.

func (*SourcedConfig) Float

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

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

func (*SourcedConfig) Get

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

Get will retrieve a configuration value loaded from a source.

func (*SourcedConfig) Has

func (c *SourcedConfig) 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 (*SourcedConfig) HasObserver

func (c *SourcedConfig) HasObserver(path string) bool

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

func (*SourcedConfig) HasSource

func (c *SourcedConfig) HasSource(id string) bool

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

func (*SourcedConfig) Int

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

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

func (*SourcedConfig) List

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

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

func (*SourcedConfig) RemoveAllSources

func (c *SourcedConfig) 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 (*SourcedConfig) RemoveObserver

func (c *SourcedConfig) RemoveObserver(path string)

RemoveObserver remove an observer to a configuration path.

func (*SourcedConfig) RemoveSource

func (c *SourcedConfig) 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 (*SourcedConfig) Source

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

Source retrieve a previously registered source with a requested id.

func (*SourcedConfig) SourcePriority

func (c *SourcedConfig) 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 (*SourcedConfig) String

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

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

Jump to

Keyboard shortcuts

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