conf

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2025 License: MIT Imports: 9 Imported by: 0

README

package conf

go get code.uint32.ru/tiny/conf to download.

import "code.uint32.ru/tiny/conf" to use in your code. 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, ohterwise Read / ReadFile methods will return an error.

Map object has Find, Get and GetDefault methods 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.

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

Constants

This section is empty.

Variables

View Source
var (
	ErrFormat       = errors.New("conf: line does not match \"key = value\" pattern")
	ErrDuplicateKey = errors.New("conf: duplicate key found")
)
View Source
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

type Map map[string]Value

Map holds key value pairs.

func Open added in v1.0.0

func Open(filename string) (Map, error)

Open reads values from file.

func Read added in v1.0.0

func Read(r io.Reader) (Map, error)

Read reads from r and returns Conf.

func (Map) Find added in v1.0.0

func (m Map) Find(key string) (Value, bool)

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

func (m Map) Get(key string) Value

Get returns a Value. If key was not found the returned Value will be empty.

func (Map) GetDefault added in v1.0.0

func (m Map) GetDefault(key, def string) Value

GetDefault looks up a Value for requested key. If lookup fails it returns Value set to def.

func (Map) Keys added in v1.0.0

func (m Map) Keys() []string

func (Map) Read added in v1.0.0

func (m Map) Read(r io.Reader) error

func (Map) ReadFile added in v1.0.0

func (m Map) ReadFile(name string) error

type Value added in v1.0.0

type Value string

func (Value) Bool added in v1.0.0

func (v Value) Bool() (bool, error)

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

func (v Value) Float64() (float64, error)

Float64 converts Value to float64. Returned error will be non nil if convesion failed.

func (Value) Float64Slice added in v1.0.0

func (v Value) Float64Slice() ([]float64, error)

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

func (v Value) Int() (int, error)

Int converts Value to int. Returned error will be non nil if convesion failed.

func (Value) IntSlice added in v1.0.0

func (v Value) IntSlice() ([]int, error)

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

func (v Value) Map() (Map, error)

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) String added in v1.0.0

func (v Value) String() string

String returns Value as string

func (Value) StringSlice added in v1.0.0

func (v Value) StringSlice() []string

StringSlice splits Value (separator is ",") and adds each of resulting values to []string trimming leading and trailing spaces from each string.

func (Value) URL added in v1.0.0

func (v Value) URL() (*url.URL, error)

URL tries to interpret value as url.URL

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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