config

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2021 License: MIT Imports: 14 Imported by: 3

README

gapp-config

gapp

This library intent to provide a flexible and opinionated structure to build applications in a way that removes the problems of coding the same functionalities over and over regarding system components as configuration or logging.

Is the opinion of the library that the application scaffolding should revolve around the idea of a service container, seen in other types of frameworks. This container is responsible for storing factories of the application services that should be called for service instantiation, per need. This facilitates not only the composition of the application, but also directs the application architecture for a more dependency injection way of structuring services. By following this decoupled way of writing the application. will enable for more scalable and maintainable testing and production code.

Documentation

Index

Constants

View Source
const (

	// 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 a
	// observable file config source type.
	SourceTypeObservableFile = "observable_file"

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

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

	// ContainerID defines the id to be used as the default of a
	// config instance in the application container.
	ContainerID = "gapp.config"

	// ContainerSourceStrategyFileID defines the id to be used as
	// the default of a config file source factory strategy instance in the
	// application container.
	ContainerSourceStrategyFileID = ContainerID + ".source.strategy.file"

	// ContainerSourceStrategyObservableFileID defines the id to
	// the default of a config observable file source factory strategy instance
	// in the application container.
	ContainerSourceStrategyObservableFileID = ContainerID + ".source.strategy.observable_file"

	// ContainerSourceStrategyEnvironmentID defines the id to the default
	// of a config environment source factory strategy instance in the
	// application container.
	ContainerSourceStrategyEnvironmentID = ContainerID + ".source.strategy.environment"

	// ContainerSourceFactoryID defines the id to be used as the default
	// of a config source factory instance in the application container.
	ContainerSourceFactoryID = ContainerID + ".source.factory"

	// ContainerDecoderStrategyYamlID defines the id to be used
	// as the default of a yaml config decoder factory strategy instance in
	// the application container.
	ContainerDecoderStrategyYamlID = ContainerID + ".decoder.strategy.yaml"

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

	// ContainerLoaderID defines the id to be used as the default of a
	// config loader instance in the application container.
	ContainerLoaderID = ContainerID + ".loader"

	// EnvObserveFrequency defines the name of the environment variable
	// to be checked for a overriding value for the config observe frequency.
	EnvObserveFrequency = "GAPP_CONFIG_OBSERVE_FREQUENCY"

	// EnvEntrySourceActive defines the name of the environment variable
	// to be checked for a overriding value for the config entry source active.
	EnvEntrySourceActive = "GAPP_CONFIG_ENTRY_SOURCE_ACTIVE"

	// EnvEntrySourceID defines the name of the environment variable
	// to be checked for a overriding value for the config entry source id.
	EnvEntrySourceID = "GAPP_CONFIG_ENTRY_SOURCE_ID"

	// EnvEntrySourcePath defines the name of the environment variable
	// to be checked for a overriding value for the config entry source path.
	EnvEntrySourcePath = "GAPP_CONFIG_ENTRY_SOURCE_PATH"

	// EnvEntrySourceFormat defines the name of the environment variable
	// to be checked for a overriding value for the config entry source format.
	EnvEntrySourceFormat = "GAPP_CONFIG_ENTRY_SOURCE_FORMAT"
)

Variables

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

	// EntrySourceActive defines the entry config source active flag
	// used to signal the config loader to load the entry source or not
	EntrySourceActive = true

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

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

	// EntrySourceFormat defines the entry config source format
	// to be used as the loader entry.
	EntrySourceFormat = DecoderFormatYAML
)

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 {
	Close()
	Has(path string) bool
	Get(path string, def ...interface{}) interface{}
	GetBool(path string, def ...bool) bool
	GetInt(path string, def ...int) int
	GetInt8(path string, def ...int8) int8
	GetInt16(path string, def ...int16) int16
	GetInt32(path string, def ...int32) int32
	GetInt64(path string, def ...int64) int64
	GetUInt(path string, def ...uint) uint
	GetUInt8(path string, def ...uint8) uint8
	GetUInt16(path string, def ...uint16) uint16
	GetUInt32(path string, def ...uint32) uint32
	GetUInt64(path string, def ...uint64) uint64
	GetFloat32(path string, def ...float32) float32
	GetFloat64(path string, def ...float64) float64
	GetComplex64(path string, def ...complex64) complex64
	GetComplex128(path string, def ...complex128) complex128
	GetRune(path string, def ...rune) rune
	GetString(path string, def ...string) string
	HasSource(id string) bool
	AddSource(id string, priority int, src Source) error
	RemoveSource(id string)
	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, along side 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 {
	Close()
	Decode() (partial, error)
}

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

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 interface {
	Register(strategy DecoderStrategy) error
	Create(format string, args ...interface{}) (Decoder, error)
}

DecoderFactory defined the instance used to instantiate a new config stream 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 NewDecoderStrategyYaml

func NewDecoderStrategyYaml() DecoderStrategy

NewDecoderStrategyYaml 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 a observed configuration path has changed.

type Partial

type Partial interface {
	Has(path string) bool
	Get(path string, def ...interface{}) interface{}
	Int(path string, def ...int) int
	String(path string, def ...string) string
	Config(path string, def ...Partial) Partial
}

Partial defined a type used to store configuration information.

func NewPartial added in v1.1.0

func NewPartial(data map[interface{}]interface{}) Partial

NewPartial is a function that will instantiate a new configuration partial instance, initialized with the assigned mappings/values

type Source

type Source interface {
	Close()
	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, error)

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