Documentation ¶
Overview ¶
Package config provides convenient access methods to configuration in JSON, YAML, XML or INI format.
Index ¶
- Constants
- Variables
- func GetDuration(c Config, path string) (value time.Duration, err error)
- func GetDurations(c Config, path string, delim string) (value []time.Duration, err error)
- func GetTime(c Config, path string) (value time.Time, err error)
- func GetTimeFormat(c Config, path string, format string) (value time.Time, err error)
- func GetTimes(c Config, path string, delim string) (value []time.Time, err error)
- func GetTimesFormat(c Config, path string, format string, delim string) (value []time.Time, err error)
- func GrabStringValue(c Config, path string, grabber StringValueGrabber) (err error)
- func GrabStringValues(c Config, path string, delim string, creator ValueSliceCreator, ...) (err error)
- func LoadValue(c Config, path string, value interface{}) (err error)
- func LoadValueIgnoringMissingFieldErrors(c Config, path string, value interface{}) (err error)
- func TunedLoadValue(c Config, settings LoadSettings, path string, value interface{}) (err error)
- type Config
- func CreateConfig(configData []byte, configType string) (Config, error)
- func CreateConfigFromString(configData string, configType string) (Config, error)
- func ReadConfig(configPath string) (Config, error)
- func ReadConfigFromReader(configReader io.Reader, configType string) (Config, error)
- func ReadTypedConfig(configPath string, configType string) (Config, error)
- type LoadSettings
- type Loadable
- type StringValueGrabber
- type StringValueLoader
- type ValueGrabber
- type ValueSliceCreator
Constants ¶
const ( CONF = "conf" INI = "ini" JSON = "json" XML = "xml" YAML = "yaml" YML = "yml" )
Constants for available config types.
Variables ¶
var ( ErrorNotFound = errors.New("Not found") ErrorIncorrectPath = errors.New("Incorrect path") ErrorUnknownConfigType = errors.New("Unknown config type") ErrorIncorrectValueType = errors.New("Incorrect value type") ErrorUnsupportedTypeToLoadValue = errors.New("Unsupported field type") ErrorIncorrectValueToLoadFromConfig = errors.New("Inccorect value to load from config") )
Errors returned from the library.
Functions ¶
func GetDuration ¶
GetDuration returns duration value from config.
func GetDurations ¶
GetDurations returns duration values from config. Argument 'delim' may be used to split array into separate elements.
func GetTimeFormat ¶
GetTimeFormat returns time value from config using custom format to parsing.
func GetTimes ¶
GetTimes returns time values from config. Argument 'delim' may be used to split array into separate elements.
func GetTimesFormat ¶
func GetTimesFormat(c Config, path string, format string, delim string) (value []time.Time, err error)
GetTimesFormat returns time value from config using custom format to parsing. Argument 'delim' may be used to split array into separate elements.
func GrabStringValue ¶
func GrabStringValue(c Config, path string, grabber StringValueGrabber) (err error)
GrabStringValue retrieves value from config using specified grabber.
func GrabStringValues ¶
func GrabStringValues(c Config, path string, delim string, creator ValueSliceCreator, grabber StringValueGrabber) (err error)
GrabStringValues retrieves values from config using specified grabber and slice creator.
func LoadValue ¶
LoadValue loads value from config to specified variable. Argument 'value' must be pointer. Function can load simple types (bool, int, uint, float, string), arrays of simple types and structure. Structure can be loaded field by field, in this case for path construction in first place used tag 'config', in second--name of field. Also structure can be loaded using custom loader (of type 'StringValueLoader') or 'Loadable' interface.
func LoadValueIgnoringMissingFieldErrors ¶
LoadValueIgnoringMissingFieldErrors loads value from config to variable ignoring some errors (parsing errors, absent value, and etc). Argument 'value' must be pointer. Function can load simple types (bool, int, uint, float, string), arrays of simple types and structure. Structure can be loaded field by field, in this case for path construction in first place used tag 'config', in second--name of field. Also structure can be loaded using custom loader (of type 'StringValueLoader') or 'Loadable' interface.
func TunedLoadValue ¶
func TunedLoadValue(c Config, settings LoadSettings, path string, value interface{}) (err error)
TunedLoadValue loads value from config to variable using specified settings. Argument 'value' must be pointer. Function can load simple types (bool, int, uint, float, string), arrays of simple types and structure. Structure can be loaded field by field, in this case for path construction in first place used tag 'config', in second--name of field. Also structure can be loaded using custom loader (of type 'StringValueLoader') or 'Loadable' interface.
Types ¶
type Config ¶
type Config interface { // GrabValue may be used to retrieve single value of complex type. GrabValue(path string, grabber ValueGrabber) (err error) // GrabValues may be used to retrieve list of values of complex type. Argument 'delim' may // be used into method to split list into separate elements. GrabValues(path string, delim string, creator ValueSliceCreator, grabber ValueGrabber) (err error) // GetString returns string value by specified path. GetString(path string) (value string, err error) // GetBool returns bool value by specified path. GetBool(path string) (value bool, err error) // GetFloat returns float value by specified path. GetFloat(path string) (value float64, err error) // GetInt returns int value by specified path. GetInt(path string) (value int64, err error) // GetStrings returns list of string by specified path. Argument 'delim' may be used // to split list into separate elements. GetStrings(path string, delim string) (value []string, err error) // GetBools returns list of bool by specified path. Argument 'delim' may be used // to split list into separate elements. GetBools(path string, delim string) (value []bool, err error) // GetFloats returns list of float by specified path. Argument 'delim' may be used // to split list into separate elements. GetFloats(path string, delim string) (value []float64, err error) // GetInts returns list of int by specified path. Argument 'delim' may be used // to split list into separate elements. GetInts(path string, delim string) (value []int64, err error) // GetConfigPart returns as 'Config' config part by specified path. GetConfigPart(path string) (config Config, err error) }
Config represents configuration with convenient access methods.
func CreateConfig ¶
CreateConfig creates and parses config of specified type from byte array.
func CreateConfigFromString ¶
CreateConfigFromString creates and parses config of specified type from string.
func ReadConfig ¶
ReadConfig reads and parses config from file. Config type is detected by file extension.
func ReadConfigFromReader ¶
ReadConfigFromReader reads and parses config of specified type from reader.
type LoadSettings ¶
type LoadSettings struct { // Delimiter that will be used to split array into separate elements. Delim string // Flag that specifies whether to ignore missing field errors. IgnoreMissingFieldErrors bool // Custom loaders. Loaders map[string]StringValueLoader }
LoadSettings is settings that used to load values from config.
func GetDefaultLoadSettings ¶
func GetDefaultLoadSettings(ignoreMissingFieldErrors bool) LoadSettings
GetDefaultLoadSettings returns settings that may be used to load value from config.
type Loadable ¶
Loadable is interface that contains method to load value from config.
LoadValueFromConfig must parse string, change receiver value and return error on fail.
type StringValueGrabber ¶
StringValueGrabber type of function that retrieves string value from config.
type StringValueLoader ¶
StringValueLoader type of function that loads string value from config.
type ValueGrabber ¶
type ValueGrabber func(interface{}) error
ValueGrabber type of function that retrieves value from config.
type ValueSliceCreator ¶
type ValueSliceCreator func(length int)
ValueSliceCreator type of function that creates slice for value grabber.