Documentation
¶
Overview ¶
module conf
import "code.uint32.ru/tiny/conf"
Module conf implements a very simple config parser with two types of values: key value pairs and single word options. Each of these must be put on separate line in a file like this:
key1 = value key2 = value1, value2, value3 key3=v1,v2,v3 option1 option2
Values can also be read from any io.Reader or io.ReadCloser. Note that values in key value pairs mey either be separated with spaces or not. Typical use case would look like this:
config, err := conf.ParseFile("filename")
if err != nil {
// Means we failed to read from file
// config variable is now nil and unusable
}
value, err := config.Find("mykey")
if err != nil {
// Means that key has not been found
}
// value now holds conf.Setting.
n, err := value.Float64() // tries to parse float from Setting.Value field.
//if err is nil then n holds float64.
There is also a quicker Get() method which returns no errors ("i'm feeling lucky" way to lookup values). If it does not find requested key, the returned Setting has empty string in Value field.
value2 := config.Get("otherkey")
mybool, err := value2.Bool() // tries to interpret Setting.Value field as bool
// mybool now holds boolean if "otherkey" was found and error returned
// by Bool() method is nil.
Even shorter syntax would be:
listnumbers, err := config.Get("numbers").IntSlice()
// Note that we'd still like to check for errors even if
// we're sure the key exists to make sure all values are converted.
// listnumbers holds slice of ints. If err is nil all of found values
// have been converted successfully.
To check whether single-word options were found use:
if config.HasOption("wordoption") {
// do something
}
See description of module's other methods which are quite self-explanatory.
Index ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Settings store key value pairs ("key = value" in config file)
// all key value pairs found when parsing input are accumulated in this map.
Settings map[string]string
// Options map stores single word options ("option" in config file)
Options map[string]struct{}
}
Config holds parsed keys and values. Settings and Options can be accessed with Config.Settings and Config.Options maps directly.
func ParseFile ¶
ParseFile reads values from file. It returns nil and error if os.Open(filename) fails. It would be wise to always check returned error. ParseFile captures two types of values: "key = value" and "option". Either key value pair or option must be put in its own line in the file. Key or option must be a single word. In example line:
"option1 option2"
only option1 will be captured. option2 needs to be in a separate line in the file to take effect. In line "key = value1,value2,value3" all of value1, value2, and value3 will be captured. They can be later accesed separately with Setting's Split() method.
func ParseReadCloser ¶
func ParseReadCloser(r io.ReadCloser) *Config
ParseReadCloser reads from r, returns Config and calls r.Close(). See also ParseFile.
func ParseReader ¶
ParseReader reads from r and returns Config. See also ParseFile.
func (*Config) Find ¶
Find looks up a Setting and returns it. If returned error is not nil the requested key was not found and returned Setting has empty string in Value field.
func (*Config) Get ¶
Get returns a Setting. If key was not found the returned Setting Value will be empty string.
func (*Config) GetDefault ¶
GetDefault looks up a Setting with requested key. If lookup fails it returns Setting with Value field set to def.
type Setting ¶
type Setting struct {
Value string
}
Setting represents key-value pair read from config file. It's Value field holds the value of key parsed from the configuration
func (Setting) Bool ¶
Bool tries to interpret Setting's Value as bool "1", "true", "on", "yes" (case insensitive) yields true "0", "false", "off", "no" (case insensitive) yields false If nothing matches will return false and conf.ErrParsingBool
func (Setting) Float64 ¶
Float64 converts Setting Value to float64. Returned error will be non nil if convesion failed.
func (Setting) Float64Slice ¶
Float64Slice splits Setting Value (separator is ",") and adds each of resulting values to []float64 if possible. If one or more values can not be converted to float64 those will be dropped and method will return conf.ErrCouldNotConvert. Check error to be sure that all required values were parsed.
func (Setting) Int ¶
Int converts Setting Value to int. Returned error will be non nil if convesion failed.
func (Setting) IntSlice ¶
IntSlice splits Setting Value (separator is ",") and adds each of resulting values to []int if possible. If one or more values can not be converted to float64 those will be dropped and method will return conf.ErrCouldNotConvert. Check error to be sure that all required values were parsed.
func (Setting) String ¶
String returns option Value as string This method also implements Stringer interface from fmt module
func (Setting) StringSlice ¶
StringSlice splits Setting's Value (separator is ",") and adds each of resulting values to []string trimming leading and trailing spaces from each string.