config

package
v1.0.6-7 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2022 License: MIT Imports: 11 Imported by: 2

Documentation

Index

Constants

View Source
const FileConfigReaderPathKey = "path"

FileConfigReaderPathKey is a constant for path key

View Source
const SectionNameParameters = "parameters"

SectionNameParameters is a name of ConfigReader section

Variables

View Source
var JsonConfigReaderDescriptor = refer.NewDescriptor("pip-services", "config-reader", "json", "*", "1.0")
View Source
var MemoryConfigReaderDescriptor = refer.NewDescriptor("pip-services", "config-reader", "memory", "*", "1.0")
View Source
var YamlConfigReaderDescriptor = refer.NewDescriptor("pip-services", "config-reader", "yaml", "*", "1.0")

Functions

func NewDefaultConfigReaderFactory

func NewDefaultConfigReaderFactory() *build.Factory

NewDefaultConfigReaderFactory create a new instance of the factory.

Returns: *build.Factory

func ReadJsonConfig

func ReadJsonConfig(ctx context.Context, correlationId string, path string,
	parameters *cconfig.ConfigParams) (*cconfig.ConfigParams, error)

ReadJsonConfig reads configuration from a file, parameterize it with given values and returns a new ConfigParams object.

Parameters:
	- ctx context.Context
	- correlationId string transaction id to trace execution through call chain.
	- path string
	- parameters *cconfig.ConfigParams values to parameters the configuration.
Returns: *cconfig.ConfigParams, error

func ReadJsonObject

func ReadJsonObject(ctx context.Context, correlationId string, path string,
	parameters *cconfig.ConfigParams) (any, error)

ReadJsonObject reads configuration file, parameterizes its content and converts it into JSON object.

Parameters:
	- ctx context.Context
	- correlationId string transaction id to trace execution through call chain.
	- path string
	- parameters *cconfig.ConfigParams values to parameters the configuration.
Returns: any, error a JSON object with configuration.

func ReadYamlConfig

func ReadYamlConfig(ctx context.Context, correlationId string, path string,
	parameters *cconfig.ConfigParams) (*cconfig.ConfigParams, error)

ReadYamlConfig reads configuration from a file, parameterize it with given values and returns a new ConfigParams object.

Parameters:
	- ctx context.Context
	- correlationId string transaction id to trace execution through call chain.
	- path string
	- parameters *cconfig.ConfigParams values to parameters the configuration.
Returns: *cconfig.ConfigParams, error

func ReadYamlObject

func ReadYamlObject(ctx context.Context, correlationId string, path string,
	parameters *cconfig.ConfigParams) (any, error)

ReadYamlObject reads configuration file, parameterizes its content and converts it into JSON object.

Parameters:
	- ctx context.Context
	- correlationId string transaction id to trace execution through call chain.
	- path string
	- parameters *cconfig.ConfigParams values to parameters the configuration.
Returns: any, error a JSON object with configuration.

Types

type ConfigReader

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

ConfigReader abstract config reader that supports configuration parameterization.

Configuration parameters:
	parameters this entire section is used as template parameters

func NewConfigReader

func NewConfigReader() *ConfigReader

NewConfigReader creates a new instance of the config reader.

Returns: *ConfigReader

func (*ConfigReader) AddChangeListener added in v1.0.4

func (c *ConfigReader) AddChangeListener(ctx context.Context, listener crun.INotifiable)

AddChangeListener - Adds a listener that will be notified when configuration is changed

func (*ConfigReader) Configure

func (c *ConfigReader) Configure(ctx context.Context, config *cconfig.ConfigParams)

Configure configures component by passing configuration parameters.

Parameters:
	- ctx context.Context
	- config *config.ConfigParams configuration parameters to be set.

func (*ConfigReader) Parameterize

func (c *ConfigReader) Parameterize(config string, parameters *cconfig.ConfigParams) (string, error)

Parameterize configuration template given as string with dynamic parameters. The method uses Mustache template engine implemented in expressions module

Parameters:
	- config string a string with configuration template to be parameterized
	- parameters *config.ConfigParams dynamic parameters to inject into the template
Returns: string, error a parameterized configuration string abd error.

func (*ConfigReader) RemoveChangeListener added in v1.0.4

func (c *ConfigReader) RemoveChangeListener(ctx context.Context, listener crun.INotifiable)

RemoveChangeListener - Remove a previously added change listener.

type FileConfigReader

type FileConfigReader struct {
	*ConfigReader
	// contains filtered or unexported fields
}

FileConfigReader is an abstract config reader that reads configuration from a file. Child classes add support for config files in their specific format like JSON, YAML or property files.

Configuration parameters:
	- path: path to configuration file
	- parameters: this entire section is used as template parameters

func NewEmptyFileConfigReader

func NewEmptyFileConfigReader() *FileConfigReader

NewEmptyFileConfigReader creates a new instance of the config reader.

Returns: *FileConfigReader

func NewFileConfigReader

func NewFileConfigReader(path string) *FileConfigReader

NewFileConfigReader creates a new instance of the config reader.

Parameters:
	- path string a path to configuration file.
Returns: *FileConfigReader

func (*FileConfigReader) Configure

func (c *FileConfigReader) Configure(ctx context.Context, config *cconfig.ConfigParams)

Configure component by passing configuration parameters.

Parameters:
	- ctx context.Context
	- config *cconfig.ConfigParams configuration parameters to be set.

func (*FileConfigReader) Path

func (c *FileConfigReader) Path() string

Path get the path to configuration file..

Returns: string the path to configuration file.

func (*FileConfigReader) SetPath

func (c *FileConfigReader) SetPath(path string)

SetPath set the path to configuration file.

Parameters:
	- path string a new path to configuration file.

type IConfigReader

type IConfigReader interface {

	// ReadConfig reads configuration and parameterize it with given values.
	ReadConfig(ctx context.Context, correlationId string, parameters *cconfig.ConfigParams) (*cconfig.ConfigParams, error)

	// AddChangeListener adds a listener that will be notified when configuration is changed
	AddChangeListener(ctx context.Context, listener crun.INotifiable)

	// RemoveChangeListener remove a previously added change listener.
	RemoveChangeListener(ctx context.Context, listener crun.INotifiable)
}

IConfigReader Interface for configuration readers that retrieve configuration from various sources and make it available for other components. Some IConfigReader implementations may support configuration parameterization. The parameterization allows using configuration as a template and inject there dynamic values. The values may come from application command like arguments or environment variables.

type JsonConfigReader

type JsonConfigReader struct {
	*FileConfigReader
}

JsonConfigReader is a config reader that reads configuration from JSON file. The reader supports parameterization using Handlebar template engine.

Configuration parameters:
	- path: path to configuration file
	- parameters: this entire section is used as template parameters
	- ...
see IConfigReader
see FileConfigReader
Example:
	======== config.json ======
	{ "key1": "{{KEY1_VALUE}}", "key2": "{{KEY2_VALUE}}" }
	===========================

	configReader := NewJsonConfigReader("config.json")
	parameters := NewConfigParamsFromTuples("KEY1_VALUE", 123, "KEY2_VALUE", "ABC")
	res, err := configReader.ReadConfig(context.Background(), "123", parameters)

func NewEmptyJsonConfigReader

func NewEmptyJsonConfigReader() *JsonConfigReader

NewEmptyJsonConfigReader creates a new instance of the config reader.

Returns: *JsonConfigReader

func NewJsonConfigReader

func NewJsonConfigReader(path string) *JsonConfigReader

NewJsonConfigReader creates a new instance of the config reader.

Parameters: path string a path to configuration file.
Returns: *JsonConfigReader

func (*JsonConfigReader) ReadConfig

func (c *JsonConfigReader) ReadConfig(ctx context.Context, correlationId string,
	parameters *cconfig.ConfigParams) (result *cconfig.ConfigParams, err error)

ReadConfig кeads configuration from a file, parameterize it with given values and returns a new ConfigParams object.

Parameters:
	- ctx context.Context
	- correlationId string transaction id to trace execution through call chain.
	- parameters *cconfig.ConfigParams values to parameters the configuration.
Returns: *cconfig.ConfigParams, error

func (*JsonConfigReader) ReadObject

func (c *JsonConfigReader) ReadObject(ctx context.Context, correlationId string,
	parameters *cconfig.ConfigParams) (any, error)

ReadObject reads configuration file, parameterizes its content and converts it into JSON object.

Parameters:
	- ctx context.Context
	- correlationId string transaction id to trace execution through call chain.
	- parameters *cconfig.ConfigParams values to parameters the configuration.
Returns: any, error a JSON object with configuration adn error.

type MemoryConfigReader

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

MemoryConfigReader is a config reader that stores configuration in memory. The reader supports parameterization using Mustache template engine implemented in expressions module. Configuration parameters: The configuration parameters are the configuration template

see IConfigReader
Example
	config := NewConfigParamsFromTuples(
		"connection.host", "{{SERVICE_HOST}}",
		"connection.port", "{{SERVICE_PORT}}{{^SERVICE_PORT}}8080{{/SERVICE_PORT}}"
	);
	configReader := NewMemoryConfigReader();
	configReader.Configure(context.Background(), config);
	parameters := NewConfigParamsFromValue(process.env);
	res, err := configReader.ReadConfig(context.Background(), "123", parameters);
		Possible result: connection.host=10.1.1.100;connection.port=8080

func NewEmptyMemoryConfigReader

func NewEmptyMemoryConfigReader() *MemoryConfigReader

NewEmptyMemoryConfigReader creates a new instance of config reader.

Returns: *MemoryConfigReader

func NewMemoryConfigReader

func NewMemoryConfigReader(config *cconfig.ConfigParams) *MemoryConfigReader

NewMemoryConfigReader creates a new instance of config reader.

Parameters: config *cconfig.ConfigParams component configuration parameters
Returns: *MemoryConfigReader

func (*MemoryConfigReader) AddChangeListener added in v1.0.4

func (c *MemoryConfigReader) AddChangeListener(ctx context.Context, listener crun.INotifiable)

AddChangeListener - Adds a listener that will be notified when configuration is changed

func (*MemoryConfigReader) Configure

func (c *MemoryConfigReader) Configure(ctx context.Context, config *cconfig.ConfigParams)

Configure component by passing configuration parameters.

Parameters:
	- ctx context.Context
	- config *cconfig.ConfigParams configuration parameters to be set.

func (*MemoryConfigReader) ReadConfig

func (c *MemoryConfigReader) ReadConfig(ctx context.Context, correlationId string,
	parameters *cconfig.ConfigParams) (*cconfig.ConfigParams, error)

ReadConfig reads configuration and parameterize it with given values.

Parameters:
	- ctx context.Context
	- correlationId string transaction id to trace execution through call chain.
	- parameters *cconfig.ConfigParams values to parameters
		the configuration or null to skip parameterization.
Returns: *cconfig.ConfigParams, error configuration or error.

func (*MemoryConfigReader) RemoveChangeListener added in v1.0.4

func (c *MemoryConfigReader) RemoveChangeListener(ctx context.Context, listener crun.INotifiable)

RemoveChangeListener - Remove a previously added change listener.

type YamlConfigReader

type YamlConfigReader struct {
	*FileConfigReader
}

YamlConfigReader is a config reader that reads configuration from YAML file. The reader supports parameterization using Handlebars template engine.

Configuration parameters:
	- path: path to configuration file
	- parameters: this entire section is used as template parameters
	...
see IConfigReader
see FileConfigReader
Example:
	======== config.yml ======
	key1: "{{KEY1_VALUE}}"
	key2: "{{KEY2_VALUE}}"
	===========================

	configReader := NewYamlConfigReader("config.yml")
	parameters := NewConfigParamsFromTuples("KEY1_VALUE", 123, "KEY2_VALUE", "ABC");
	res, err := configReader.ReadConfig(context.Background(), "123", parameters);
		// Result: key1=123;key2=ABC

func NewEmptyYamlConfigReader

func NewEmptyYamlConfigReader() *YamlConfigReader

NewEmptyYamlConfigReader сreates a new instance of the config reader.

Returns: *YamlConfigReader

func NewYamlConfigReader

func NewYamlConfigReader(path string) *YamlConfigReader

NewYamlConfigReader creates a new instance of the config reader.

Parameters: path string a path to configuration file.
Returns: *YamlConfigReader

func (*YamlConfigReader) ReadConfig

func (c *YamlConfigReader) ReadConfig(ctx context.Context, correlationId string,
	parameters *cconfig.ConfigParams) (result *cconfig.ConfigParams, err error)

ReadConfig reads configuration from a file, parameterize it with given values and returns a new ConfigParams object.

Parameters:
	- ctx context.Context
	- correlationId string transaction id to trace execution through call chain.
	- parameters *cconfig.ConfigParams values to parameters the configuration.
Returns: *cconfig.ConfigParams, error

func (*YamlConfigReader) ReadObject

func (c *YamlConfigReader) ReadObject(ctx context.Context, correlationId string,
	parameters *cconfig.ConfigParams) (any, error)

ReadObject reads configuration file, parameterizes its content and converts it into JSON object.

Parameters:
	- ctx context.Context
	- correlationId string transaction id to trace execution through call chain.
	- parameters *cconfig.ConfigParams values to parameters the configuration.
Returns: any, error a JSON object with configuration adn error.

Jump to

Keyboard shortcuts

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