Documentation
¶
Overview ¶
module conf
import "github.com/dmfed/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 option1 option2
Values can also be read from any io.Reader or io.ReadCloser
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.GetSetting("mykey").Float64()
if err != nil {
// Means that value has not been found
// or can not be cast to desired type
}
// value now holds float64.
value2, _ := config.GetSetting("otherkey").String()
// value2 now holds string if "otherkey" was parsed, else an empty string.
Trying to extract non existing value will always return default value for the type.
See description of module's types and methods which are quite self-explanatory.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotFound is returned when trying to get empty value from Setting. ErrNotFound = errors.New("key was not found") // ErrParsingBool is returned when Setting.Bool() method is called and Setting.Value // can not be interpreted as boolean. ErrParsingBool = errors.New("value can not be interpreted as bool") )
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 ¶ added in v0.2.1
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) GetSetting ¶ added in v0.2.1
Get returns a Setting. If key was not found the returned Setting's Value will be empty string and Setting's Found field will be set to false
func (*Config) HasOption ¶ added in v0.2.1
HasOption returns true if line:
"key"
was found in the parsed file
func (*Config) HasSetting ¶ added in v0.2.1
HasSetting returns true if line:
"key = somevalue"
was found in the parsed data
type Setting ¶ added in v0.2.1
type Setting struct {
// Key holds the name of key parsed from the configuration
Key string
// Value holds the value of key parsed from the configuration
Value string
// Found is set to true if Key was found in the parsed config, false otherwise
Found bool
}
Setting represents key-value pair read from config file
func (Setting) Bool ¶ added in v0.2.1
Bool tries to interpret Setting's Value as bool "1", "true", "yes" (case insensitive) yields true "0", "false", "no" (case insensitive) yields false
func (Setting) Float64 ¶ added in v0.2.1
Float64 converts Setting's Value to float64 if possible If setting's key was not found in the config this method will return ErrNotFound
func (Setting) Int ¶ added in v0.2.1
Int converts Setting's Value to int if possible If setting's key was not found in the config this method will return ErrNotFound
func (Setting) Split ¶ added in v0.2.1
Split splits Setting's value with separator sep and returns []Setting. If separator was not found the method returns slice with only one Setting. This method is intended for use when config file has comma separated values like:
myoption = first,second,third
Split(",") will return slice with 3 separate Setting each holding one of "first, second, third" in their Value fields.