config

package
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2022 License: MIT Imports: 4 Imported by: 90

Documentation

Index

Constants

View Source
const SectionOptions = "options"

Variables

View Source
var NameResolver = &_TNameResolver{}

NameResolver is a helper class that allows to extract component name from configuration parameters. The name can be defined in "id", "name" parameters or inside a component descriptor.

Example:
	config := NewConfigParamsFromTuples(
		"descriptor", "myservice:connector:aws:connector1:1.0",
		"param1", "ABC",
		"param2", 123
	);
	name := NameResolver.Resolve(config); // Result: connector1
View Source
var OptionsResolver = &_TOptionsResolver{}

OptionsResolver is a helper class to parameters from "options" configuration section.

Example:
	config := NewConfigParamsFromTuples(
		...
		"options.param1", "ABC",
		"options.param2", 123
	);

options := OptionsResolver.resolve(config); // Result: param1=ABC;param2=123

Functions

This section is empty.

Types

type ConfigParams

type ConfigParams struct {
	*data.StringValueMap
}

ConfigParams Contains a key-value map with configuration parameters. All values stored as strings and can be serialized as JSON or string forms. When retrieved the values can be automatically converted on read using GetAsXXX methods. The keys are case-sensitive, so it is recommended to use consistent C-style as: "my_param"

Configuration parameters can be broken into sections and subsections using dot notation as: "section1.subsection1.param1". Using GetSection method all parameters from specified section can be extracted from a ConfigMap.

The ConfigParams supports serialization from/to plain strings as: "key1=123;key2=ABC;key3=2016-09-16T00:00:00.00Z"

ConfigParams are used to pass configurations to IConfigurable objects. They also serve as a basis for more concrete configurations such as ConnectionParams or CredentialParams (in the Pip.Services components package).

see IConfigurable
see StringValueMap

Example:
	config := NewConfigParamsFromTuples(
		"section1.key1", "AAA",
		"section1.key2", 123,
		"section2.key1", true
	);

	config.GetAsString("section1.key1"); // Result: AAA
	config.GetAsInteger("section1.key1"); // Result: 0

	section1 = config.GetSection("section2");
	section1.GetAsString("key1"); // Result: true

func NewConfigParams

func NewConfigParams(values map[string]string) *ConfigParams

NewConfigParams creates a new ConfigParams from map.

Parameters: values ...map[string]string
Returns: *ConfigParams a newly created ConfigParams.

func NewConfigParamsFromMaps

func NewConfigParamsFromMaps(maps ...map[string]string) *ConfigParams

NewConfigParamsFromMaps creates a new ConfigParams by merging two or more maps. Maps defined later in the list override values from previously defined maps.

Parameters: maps ...map[string]string an array of maps to be merged
Returns: *ConfigParams a newly created ConfigParams.

func NewConfigParamsFromString

func NewConfigParamsFromString(line string) *ConfigParams

NewConfigParamsFromString creates a new ConfigParams object filled with key-value pairs serialized as a string.

see convert.StringValueMap.fromString
Parameters: line: string a string with serialized key-value pairs as "key1=value1;key2=value2;..."
Example: "Key1=123;Key2=ABC;Key3=2016-09-16T00:00:00.00Z"
Returns: *ConfigParams a new ConfigParams object.

func NewConfigParamsFromTuples

func NewConfigParamsFromTuples(tuples ...any) *ConfigParams

NewConfigParamsFromTuples creates a new ConfigParams object filled with provided key-value pairs called tuples. Tuples parameters contain a sequence of key1, value1, key2, value2, ... pairs.

see convert.StringValueMap.fromTuplesArray
Parameters: tuples ...any the tuples to fill a new ConfigParams object.
Returns ConfigParams a new ConfigParams object.

func NewConfigParamsFromTuplesArray

func NewConfigParamsFromTuplesArray(tuples []any) *ConfigParams

NewConfigParamsFromTuplesArray creates a new StringValueMap from a list of key-value pairs called tuples. The method is similar to fromTuples but tuples are passed as array instead of parameters.

Parameters: tuples []any a list of values where odd elements
	are keys and the following even elements are values
Returns *ConfigParams a newly created ConfigParams.

func NewConfigParamsFromValue

func NewConfigParamsFromValue(value any) *ConfigParams

NewConfigParamsFromValue creates a new ConfigParams object filled with key-value pairs from specified object.

Parameters: value any an object with key-value pairs used to initialize a new ConfigParams.
Returns: *ConfigParams a new ConfigParams object.

func NewEmptyConfigParams

func NewEmptyConfigParams() *ConfigParams

NewEmptyConfigParams creates a new empty ConfigParams object. Returns: *ConfigParams a new empty ConfigParams object.

func (*ConfigParams) AddSection

func (c *ConfigParams) AddSection(section string, sectionParams *ConfigParams)

AddSection adds parameters into this ConfigParams under specified section. Keys for the new parameters are appended with section dot prefix.

Parameters:
	- section: string name of the section where add new parameters
	- sectionParams: *ConfigParams new parameters to be added.

func (*ConfigParams) GetSection

func (c *ConfigParams) GetSection(section string) *ConfigParams

GetSection gets parameters from specific section stored in this ConfigMap. The section name is removed from parameter keys.

Parameters: section: string name of the section to retrieve configuration parameters from.
Returns: *ConfigParams all configuration parameters that belong to the section named 'section'.

func (*ConfigParams) GetSectionNames

func (c *ConfigParams) GetSectionNames() []string

GetSectionNames gets a list with all 1st level section names.

Returns: []string a list of section names stored in this ConfigMap.

func (*ConfigParams) Override

func (c *ConfigParams) Override(configParams *ConfigParams) *ConfigParams

Override overrides parameters with new values from specified ConfigParams and returns a new ConfigParams object.

see NewConfigParamsFromMaps
Parameters: configParams: *ConfigParams ConfigMap with parameters to override the current values.
Returns *ConfigParams a new ConfigParams object.

func (*ConfigParams) SetDefaults

func (c *ConfigParams) SetDefaults(defaults *ConfigParams) *ConfigParams

SetDefaults set default values from specified ConfigParams and returns a new ConfigParams object.

see NewConfigParamsFromMaps
Parameters: defaultConfigParams: *ConfigParams ConfigMap with default parameter values.
Returns *ConfigParams a new ConfigParams object.

type IConfigurable

type IConfigurable interface {

	// Configure object by passing configuration parameters.
	//	Parameters:
	//		- ctx context.Context
	//		- config: ConfigParams configuration parameters to be set.
	Configure(ctx context.Context, config *ConfigParams)
}

IConfigurable An interface to set configuration parameters to an object.

It can be added to any existing class by implementing a single configure() method.

If you need to emphasis the fact that Configure() method can be called multiple times to change object configuration in runtime, use IReconfigurable interface instead.

Example:

type MyStruct struct  {
     myParam string
}

func NewMyStruct() *MyStruct {
    return &MyStruct{
        myParam: "default value",
    },
}

// Implement configure
func (c* MyStruct) Configure(ctx context.Context, config *cconf.ConfigParams)  {
    c.myParam = config.GetAsStringWithDefault("options.param", myParam);
    ...
}

type IReconfigurable

type IReconfigurable interface {
	IConfigurable
}

IReconfigurable an interface to set configuration parameters to an object.

It is similar to IConfigurable interface, but emphasises the fact that Configure() method can be called more than once to change object configuration in runtime.

Jump to

Keyboard shortcuts

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