Files
conf/README.md

56 lines
1.9 KiB
Markdown
Raw Normal View History

2025-04-13 22:46:22 +03:00
# package conf
2021-03-20 21:10:53 +03:00
**go get code.uint32.ru/tiny/conf** to download.
**import "code.uint32.ru/tiny/conf"** to use in your code.
2025-04-13 22:46:22 +03:00
conf implements a very simple config parser with permissive input syntax.
2021-03-20 21:10:53 +03:00
2025-04-13 22:46:22 +03:00
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.
2021-03-20 21:10:53 +03:00
2025-04-13 22:46:22 +03:00
Input syntax rules are as follows:
2021-03-20 21:10:53 +03:00
2025-04-13 22:46:22 +03:00
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.
2021-03-20 21:10:53 +03:00
2025-04-13 22:46:22 +03:00
Map object has Find, Get and GetDefault methods used to retrieve Value from Map by corresponding key.
2021-03-20 21:10:53 +03:00
2025-04-13 22:46:22 +03:00
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:
2021-03-20 21:10:53 +03:00
```go
2025-04-13 22:46:22 +03:00
m, _ := conf.ReadFile("myfile")
retries, err := m.Get("retries").Int()
if err != nil {
// handle err
2021-03-20 21:10:53 +03:00
}
2025-04-13 22:46:22 +03:00
addr, err := m.Get("addr").URL()
if err != nil {
// handle err
}
2021-03-20 21:10:53 +03:00
```
2025-04-13 22:46:22 +03:00
See example folder.