config

package
Version: v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2023 License: MIT Imports: 10 Imported by: 2

Documentation

Overview

Contains implementation of the config design pattern.

ConfigReader's Parameterize method allows us to take a standard configuration, and, using a set of current parameters (e.g. environment variables), parameterize it. When we create the configuration of a container, we can use environment variables to tailor it to the system, dynamically add addresses, ports, etc.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewDefaultConfigReaderFactory

func NewDefaultConfigReaderFactory() *build.Factory

Create a new instance of the factory. Returns *build.Factory

func ReadJsonConfig

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

Reads configuration from a file, parameterize it with given values and returns a new ConfigParams object. Parameters:

  • 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(correlationId string, path string,
	parameters *cconfig.ConfigParams) (interface{}, error)

Reads configuration file, parameterizes its content and converts it into JSON object. Parameters:

  • correlationId string transaction id to trace execution through call chain.
  • path string
  • parameters *cconfig.ConfigParams values to parameters the configuration.

Returns interface{}, error a JSON object with configuration.

func ReadYamlConfig

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

Reads configuration from a file, parameterize it with given values and returns a new ConfigParams object. Parameters:

  • correlationId string transaction id to trace execution through call chain.
  • path string
  • parameters *cconfig.ConfigParams values to parameters the configuration or null to skip parameterization.

Returns *cconfig.ConfigParams, error

func ReadYamlObject

func ReadYamlObject(correlationId string, path string,
	parameters *cconfig.ConfigParams) (interface{}, error)

Reads configuration file, parameterizes its content and converts it into JSON object. Parameters:

  • correlationId string transaction id to trace execution through call chain.
  • path string
  • parameters *cconfig.ConfigParams values to parameters the configuration.

Returns interface{}, error a JSON object with configuration.

Types

type ConfigReader

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

Abstract config reader that supports configuration parameterization. Configuration parameters

parameters: this entire section is used as template parameters

func NewConfigReader

func NewConfigReader() *ConfigReader

Creates a new instance of the config reader. Returns *ConfigReader

func (*ConfigReader) AddChangeListener added in v1.3.0

func (c *ConfigReader) AddChangeListener(listener crun.INotifiable)

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

func (*ConfigReader) Configure

func (c *ConfigReader) Configure(config *cconfig.ConfigParams)

Configures component by passing configuration parameters. Parameters:

  • config *config.ConfigParams configuration parameters to be set.

func (*ConfigReader) Parameterize

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

Parameterized 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.3.0

func (c *ConfigReader) RemoveChangeListener(listener crun.INotifiable)

RemoveChangeListener - Remove a previously added change listener.

type FileConfigReader

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

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

Creates a new instance of the config reader. Returns *FileConfigReader

func NewFileConfigReader

func NewFileConfigReader(path string) *FileConfigReader

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(config *cconfig.ConfigParams)

Configures component by passing configuration parameters. Parameters:

  • config *cconfig.ConfigParams configuration parameters to be set.

func (*FileConfigReader) Path

func (c *FileConfigReader) Path() string

Get the path to configuration file.. Returns string the path to configuration file.

func (*FileConfigReader) SetPath

func (c *FileConfigReader) SetPath(path string)

Set the path to configuration file. Parameters:

  • path string a new path to configuration file.

type IConfigReader

type IConfigReader interface {
	// Reads configuration and parameterize it with given values.
	ReadConfig(correlationId string, parameters *cconf.ConfigParams) (*cconf.ConfigParams, error)

	//Adds a listener that will be notified when configuration is changed
	AddChangeListener(listener crun.INotifiable)

	// Remove a previously added change listener.
	RemoveChangeListener(listener crun.INotifiable)
}

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 to use 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
}

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("123", parameters)

func NewEmptyJsonConfigReader

func NewEmptyJsonConfigReader() *JsonConfigReader

Creates a new instance of the config reader. Returns *JsonConfigReader

func NewJsonConfigReader

func NewJsonConfigReader(path string) *JsonConfigReader

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(correlationId string,
	parameters *cconfig.ConfigParams) (result *cconfig.ConfigParams, err error)

Reads configuration from a file, parameterize it with given values and returns a new ConfigParams object. Parameters:

  • 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(correlationId string,
	parameters *cconfig.ConfigParams) (interface{}, error)

Reads configuration file, parameterizes its content and converts it into JSON object. Parameters:

  • correlationId string transaction id to trace execution through call chain.
  • parameters *cconfig.ConfigParams values to parameters the configuration.

Returns interface{}, error a JSON object with configuration adn error.

type MemoryConfigReader

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

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(config);

parameters := NewConfigParamsFromValue(process.env);

res, err := configReader.ReadConfig("123", parameters);
// Possible result: connection.host=10.1.1.100;connection.port=8080

func NewEmptyMemoryConfigReader

func NewEmptyMemoryConfigReader() *MemoryConfigReader

Creates a new instance of config reader. Returns *MemoryConfigReader

func NewMemoryConfigReader

func NewMemoryConfigReader(config *cconfig.ConfigParams) *MemoryConfigReader

Creates a new instance of config reader. Parameters:

  • config *cconfig.ConfigParams component configuration parameters

Returns *MemoryConfigReader

func (*MemoryConfigReader) AddChangeListener added in v1.3.0

func (c *MemoryConfigReader) AddChangeListener(listener crun.INotifiable)

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

func (*MemoryConfigReader) Configure

func (c *MemoryConfigReader) Configure(config *cconfig.ConfigParams)

Configures component by passing configuration parameters. Parameters:

  • config *cconfig.ConfigParams configuration parameters to be set.

func (*MemoryConfigReader) ReadConfig

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

Reads configuration and parameterize it with given values. Parameters:

  • 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.3.0

func (c *MemoryConfigReader) RemoveChangeListener(listener crun.INotifiable)

RemoveChangeListener - Remove a previously added change listener.

type YamlConfigReader

type YamlConfigReader struct {
	FileConfigReader
}

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("123", parameters);
// Result: key1=123;key2=ABC

func NewEmptyYamlConfigReader

func NewEmptyYamlConfigReader() *YamlConfigReader

Creates a new instance of the config reader. Returns *YamlConfigReader

func NewYamlConfigReader

func NewYamlConfigReader(path string) *YamlConfigReader

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(correlationId string,
	parameters *cconfig.ConfigParams) (result *cconfig.ConfigParams, err error)

Reads configuration from a file, parameterize it with given values and returns a new ConfigParams object. Parameters:

  • correlationId string transaction id to trace execution through call chain.
  • path string
  • parameters *cconfig.ConfigParams values to parameters the configuration or null to skip parameterization.

Returns *cconfgi.ConfigParams, error

func (*YamlConfigReader) ReadObject

func (c *YamlConfigReader) ReadObject(correlationId string,
	parameters *cconfig.ConfigParams) (interface{}, error)

Reads configuration file, parameterizes its content and converts it into JSON object. Parameters:

  • correlationId string transaction id to trace execution through call chain.
  • parameters *cconfig.ConfigParams values to parameters the configuration.

Returns interface{}, error a JSON object with configuration and error.

Jump to

Keyboard shortcuts

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