config

package module
v0.0.0-...-1920840 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2017 License: Apache-2.0 Imports: 17 Imported by: 0

README

config Build Status Coverage Status Go Report Cart GoDoc

Package config provides convenient access methods to configuration in JSON, YAML, XML or INI format.

Documentation can be found in godoc.

Documentation

Overview

Package config provides convenient access methods to configuration in JSON, YAML, XML or INI format.

Index

Constants

View Source
const (
	CONF = "conf"
	INI  = "ini"
	JSON = "json"
	XML  = "xml"
	YAML = "yaml"
	YML  = "yml"
)

Constants for available config types.

Variables

View Source
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

func GetDuration(c Config, path string) (value time.Duration, err error)

GetDuration returns duration value from config.

func GetDurations

func GetDurations(c Config, path string, delim string) (value []time.Duration, err error)

GetDurations returns duration values from config. Argument 'delim' may be used to split array into separate elements.

func GetTime

func GetTime(c Config, path string) (value time.Time, err error)

GetTime returns time value from config.

func GetTimeFormat

func GetTimeFormat(c Config, path string, format string) (value time.Time, err error)

GetTimeFormat returns time value from config using custom format to parsing.

func GetTimes

func GetTimes(c Config, path string, delim string) (value []time.Time, err error)

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

func LoadValue(c Config, path string, value interface{}) (err error)

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

func LoadValueIgnoringMissingFieldErrors(c Config, path string, value interface{}) (err error)

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

func CreateConfig(configData []byte, configType string) (Config, error)

CreateConfig creates and parses config of specified type from byte array.

func CreateConfigFromString

func CreateConfigFromString(configData string, configType string) (Config, error)

CreateConfigFromString creates and parses config of specified type from string.

func ReadConfig

func ReadConfig(configPath string) (Config, error)

ReadConfig reads and parses config from file. Config type is detected by file extension.

func ReadConfigFromReader

func ReadConfigFromReader(configReader io.Reader, configType string) (Config, error)

ReadConfigFromReader reads and parses config of specified type from reader.

func ReadTypedConfig

func ReadTypedConfig(configPath string, configType string) (Config, error)

ReadTypedConfig reads and parses config from file of specified type.

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

type Loadable interface {
	LoadValueFromConfig(data string) (err error)
}

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

type StringValueGrabber func(string) error

StringValueGrabber type of function that retrieves string value from config.

type StringValueLoader

type StringValueLoader func(string) (reflect.Value, error)

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.

Jump to

Keyboard shortcuts

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