Documentation
¶
Overview ¶
conf implements a very simple config parser with permissive input syntax.
It parses input file or io.Reader looking for key value pairs separated by equal sign into conf.Map which is a map[string]conf.Value under the hood.
Input syntax rules are as follows:
1. Lines starting with "#" are considered to be comments and are discarded. If a line starts with any number of whitespace characters followed by "#" it is also discarded as comment.
2. If a line is not a comment it must follow the "key = value" pattern. Any of the following is valid:
key=value key= value key =value
Leading and trailing whitespace characters are trimmed before parsing line. Whitespaces surrounding key and value strings are also trimmed.
3. Keys in the input must be unique.
Map object has Find, Get and GetDefault mathod used to retrieve Value from Map by corresponding key.
Value is essentially a string which can be converted into several types using Value's methods:
String() returns Value as string StringSlice() interprets Value as a comma-separated list (whitespace around elements is trimmed) Int() converts Value to int IntSlice() tries to convert Value to []int Float64() converts Value to float64 Float64Slice() tries to convert to []float64 Map() tries to interpret Value as comma-separated list of key-value pairs like in "key1: value1, key2: value2, key3: value3" URL() calls url.Parse to convert Value to *url.URL
See documentation for description of all available methods.
Example usage:
m, _ := conf.ReadFile("myfile") retries, err := m.Get("retries").Int() if err != nil { // handle err } addr, err := m.Get("addr").URL() if err != nil { // handle err }
See example folder.
Index ¶
- Variables
- type Map
- type Value
- func (v Value) Bool() (bool, error)
- func (v Value) Float64() (float64, error)
- func (v Value) Float64Slice() ([]float64, error)
- func (v Value) Int() (int, error)
- func (v Value) IntSlice() ([]int, error)
- func (v Value) Map() (Map, error)
- func (v Value) String() string
- func (v Value) StringSlice() []string
- func (v Value) URL() (*url.URL, error)
Constants ¶
This section is empty.
Variables ¶
var ( ErrFormat = errors.New("conf: line does not match \"key = value\" pattern") ErrDuplicateKey = errors.New("conf: duplicate key found") )
var (
ErrCouldNotConvert = errors.New("conf: could not cast one or more values to required type")
)
Functions ¶
This section is empty.
Types ¶
type Map ¶ added in v1.0.0
Map holds key value pairs.
func (Map) Find ¶ added in v1.0.0
Find looks up a the key and returns Value associated with it. If bool is false then the returned Value would be zero.
func (Map) Get ¶ added in v1.0.0
Get returns a Value. If key was not found the returned Value will be empty.
func (Map) GetDefault ¶ added in v1.0.0
GetDefault looks up a Value for requested key. If lookup fails it returns Value set to def.
type Value ¶ added in v1.0.0
type Value string
func (Value) Bool ¶ added in v1.0.0
Bool tries to interpret Value as bool "1", "t", "T", true", "True", "TRUE" yields true "0", "f", "F, "false", "False", "FALSE" yields false If nothing matches will return false and conf.ErrCouldNotConvert.
func (Value) Float64 ¶ added in v1.0.0
Float64 converts Value to float64. Returned error will be non nil if convesion failed.
func (Value) Float64Slice ¶ added in v1.0.0
Float64Slice splits Value (separator is ",") and adds each of resulting values to []float64 if possible. Returns nil and non-nil error on first failure to convert.
func (Value) Int ¶ added in v1.0.0
Int converts Value to int. Returned error will be non nil if convesion failed.
func (Value) IntSlice ¶ added in v1.0.0
IntSlice splits Value (separator is ",") and adds each of resulting values to []int if possible. Returns nil and non-nil error on first failure to convert.
func (Value) Map ¶ added in v1.0.0
Map tries to interpret value as "key: value" pairs separated by comma like in the following string: "key1: value1, key2: value2, key3: value3".
func (Value) StringSlice ¶ added in v1.0.0
StringSlice splits Value (separator is ",") and adds each of resulting values to []string trimming leading and trailing spaces from each string.