config

package
v0.40.4 Latest Latest
Warning

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

Go to latest
Published: May 19, 2022 License: Apache-2.0 Imports: 7 Imported by: 3

Documentation

Overview

Package config provides a means of configuration using simple key value pairs.

Index

Constants

This section is empty.

Variables

View Source
var ErrConfigParamNotFound = errors.New("Param not found")

ErrConfigParamNotFound - Error returned when the config does not contain the parameter requested

View Source
var ErrUnknownConfig = errors.New("config not found")

Functions

func Equals

func Equals(cfg ReadableConfig, compareProps map[string]string) bool

Equals compares a config against a map and returns whether the map contains all the values of the config with the same values.

func GetFloat

func GetFloat(cs ReadableConfig, k string) (float64, error)

GetFloat retrieves a string value from a ReadableConfig and converts it to a float.

func GetInt

func GetInt(cs ReadableConfig, k string) (int64, error)

GetInt retrieves a string value from a ReadableConfig and converts it to an integer.

func GetString

func GetString(cs ReadableConfig, k string) (string, error)

GetString retrieves a string value from a ReadableConfig

func GetUint

func GetUint(cs ReadableConfig, k string) (uint64, error)

GetUint retrieves a string value from a ReadableConfig and converts it to an unsigned integer.

func SetFloat

func SetFloat(c WritableConfig, key string, val float64) error

SetFloat sets a value in the WritableConfig for a given key to the string converted value of a float

func SetInt

func SetInt(c WritableConfig, key string, val int64) error

SetInt sets a value in the WritableConfig for a given key to the string converted value of an integer

func SetString

func SetString(c WritableConfig, key string, val string) error

func SetStrings

func SetStrings(c WritableConfig, updates map[string]string) error

SetStrings sets configuration values from the values in the updates map

func SetUint

func SetUint(c WritableConfig, key string, val uint64) error

SetUint sets a value in the Writable for a given key to the string converted value of an unsigned int

Types

type ConfigHierarchy

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

ConfigHierarchy is a hierarchical read-only configuration store. When a key is looked up in the ConfigHierarchy it will go through its configs in order and will return the first value for a given key that is found. Configs are iterated in order, so the configurations added first have the highest priority.

func NewConfigHierarchy

func NewConfigHierarchy() *ConfigHierarchy

NewConfigHierarchy creates an empty ConfigurationHierarchy

func (*ConfigHierarchy) AddConfig

func (ch *ConfigHierarchy) AddConfig(name string, cs ReadWriteConfig)

AddConfig adds a ReadWriteConfig to the hierarchy. Newly added configs are at a lower priority than the configs that were added previously. Though the ConfigHierarchy does not support modification of stored values in the configs directly, the configs it manages must implement the WritableConfig interface.

func (*ConfigHierarchy) GetConfig

func (ch *ConfigHierarchy) GetConfig(name string) (ReadWriteConfig, bool)

GetConfig retrieves a config by name. ReadWriteConfig instances can be modified

func (*ConfigHierarchy) GetString

func (ch *ConfigHierarchy) GetString(k string) (string, error)

GetString iterates through all configs in the order they were added looking for a given key. The first valid value found will be returned.

func (*ConfigHierarchy) GetStringOrDefault

func (ch *ConfigHierarchy) GetStringOrDefault(key, defStr string) string

func (*ConfigHierarchy) Iter

func (ch *ConfigHierarchy) Iter(cb func(string, string) (stop bool))

Iter will perform a callback for each value in a config until all values have been exhausted or until the callback returns true indicating that it should stop. For ConfigHierchies, parameter names which are provided to the callbacks are of the format config_name::param_name

func (*ConfigHierarchy) SetStrings

func (ch *ConfigHierarchy) SetStrings(updates map[string]string) error

SetStrings will set the value of configuration parameters in memory, and persist any changes to the backing file. For ConfigHierarchies update parameter names must be of the format config_name::param_name

func (*ConfigHierarchy) Size

func (ch *ConfigHierarchy) Size() int

Size returns the number of properties contained within the config. For config hierarchy it returns the sum of the sizes of all elements in the hierarchy. This is the number of elements that would be seen when calling Iter on the hierarchy.

func (*ConfigHierarchy) Unset

func (ch *ConfigHierarchy) Unset(params []string) error

Unset removes a configuration parameter from the config

type FileConfig

type FileConfig struct {
	// The path of the config file in the filesystem
	Path string
	// contains filtered or unexported fields
}

FileConfig is backed by a file in the filesystem.

func FromFile

func FromFile(path string, fs filesys.ReadWriteFS) (*FileConfig, error)

FromFile reads configuration from a file on the given filesystem. Calls to SetStrings will result in this file being updated.

func NewFileConfig

func NewFileConfig(path string, fs filesys.ReadWriteFS, properties map[string]string) (*FileConfig, error)

NewFileConfig creates a new empty config and writes it to a newly created file. If a file already exists at this location it will be overwritten. If a directory does not exist where this file should live, it will be created.

func (*FileConfig) GetString

func (fc *FileConfig) GetString(k string) (string, error)

GetString retrieves a string from the cached config state

func (*FileConfig) GetStringOrDefault

func (fc *FileConfig) GetStringOrDefault(k, defStr string) string

GetString retrieves a string from the cached config state

func (*FileConfig) Iter

func (fc *FileConfig) Iter(cb func(string, string) (stop bool))

Iter will perform a callback for ech value in a config until all values have been exhausted or until the callback returns true indicating that it should stop.

func (*FileConfig) SetStrings

func (fc *FileConfig) SetStrings(updates map[string]string) error

SetStrings will set the value of configuration parameters in memory, and persist any changes to the backing file.

func (*FileConfig) Size

func (fc *FileConfig) Size() int

Size returns the number of properties contained within the config

func (*FileConfig) Unset

func (fc *FileConfig) Unset(params []string) error

Unset removes a configuration parameter from the config

type MapConfig

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

MapConfig is a simple config for in memory or test configuration. Calls to SetStrings will are valid for the lifecycle of a program, but are not persisted anywhere and will return to their default values on the next run of a program.

func NewEmptyMapConfig

func NewEmptyMapConfig() *MapConfig

func NewMapConfig

func NewMapConfig(properties map[string]string) *MapConfig

NewMapConfig creates a config from a map.

func (*MapConfig) GetString

func (mc *MapConfig) GetString(k string) (string, error)

GetString retrieves a value for a given key.

func (*MapConfig) GetStringOrDefault

func (mc *MapConfig) GetStringOrDefault(key, defStr string) string

func (*MapConfig) Iter

func (mc *MapConfig) Iter(cb func(string, string) (stop bool))

Iter will perform a callback for ech value in a config until all values have been exhausted or until the callback returns true indicating that it should stop.

func (*MapConfig) SetStrings

func (mc *MapConfig) SetStrings(updates map[string]string) error

SetStrings sets the values for a map of updates.

func (*MapConfig) Size

func (mc *MapConfig) Size() int

Size returns the number of properties contained within the config

func (*MapConfig) Unset

func (mc *MapConfig) Unset(params []string) error

Unset removes a configuration parameter from the config

type PrefixConfig

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

PrefixConfig decorates read and write access to the underlying config by appending a prefix to the accessed keys on reads and writes. Used for the sqlserver.global persisted system variables.

func NewPrefixConfig

func NewPrefixConfig(cfg ReadWriteConfig, prefix string) PrefixConfig

func (PrefixConfig) GetString

func (nsc PrefixConfig) GetString(key string) (value string, err error)

func (PrefixConfig) GetStringOrDefault

func (nsc PrefixConfig) GetStringOrDefault(key, defStr string) string

func (PrefixConfig) Iter

func (nsc PrefixConfig) Iter(cb func(string, string) (stop bool))

func (PrefixConfig) SetStrings

func (nsc PrefixConfig) SetStrings(updates map[string]string) error

func (PrefixConfig) Size

func (nsc PrefixConfig) Size() int

func (PrefixConfig) Unset

func (nsc PrefixConfig) Unset(params []string) error

type ReadWriteConfig

type ReadWriteConfig interface {
	ReadableConfig
	WritableConfig
}

ReadWriteConfig interface provides a mechanism for both getting and setting key value pairs in a config

type ReadableConfig

type ReadableConfig interface {

	// GetString retrieves a string given a key.  If there is no config property with the given key then
	// config.ErrConfigParamNotFound will be returned. Other errors may be returned depending on the
	// ReadableConfig implementation.
	GetString(key string) (value string, err error)

	// GetStringOrDefault retrieves a string from the config hierarchy and returns it if available. Otherwise it returns
	// the default string value
	GetStringOrDefault(key, defStr string) string

	// Iter will perform a callback for each value in a config until all values have been exhausted or until the
	// callback returns true indicating that it should stop.
	Iter(func(string, string) (stop bool))

	// Size returns the number of properties contained within the config
	Size() int
}

ReadableConfig interface provides a mechanisms for getting key value pairs from a config

type WritableConfig

type WritableConfig interface {

	// SetStrings uses the updates map to set configuration parameter values.
	SetStrings(updates map[string]string) error

	// Unset removes a configuration parameter from the config
	Unset(params []string) error
}

WritableConfig interface provides a mechanism for setting key value pairs in a config

Jump to

Keyboard shortcuts

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