conf

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2021 License: MIT Imports: 7 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

View Source
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)
	Settings map[string]string
	// Options store single valued options ("option" in config file)
	Options map[string]struct{}
}

Config holds keys and values parsed from file, io.Reader, or io.ReadCloser You can access Config.Settings and Config.Options directly.

func ParseFile

func ParseFile(filename string) (*Config, error)

ParseFile reads Config 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. Thus option2 needs to be put in a separate line in the file. In line "key = value1 value2 value3" all of value1, value2, and value3 will be captured.

func ParseReadCloser added in v0.2.1

func ParseReadCloser(r io.ReadCloser) *Config

ParseReadCloser reads Config from r and calls r.Close(). See also ParseFile.

func ParseReader

func ParseReader(r io.Reader) *Config

ParseReader reads Config from r. See also ParseFile.

func (*Config) GetSetting added in v0.2.1

func (c *Config) GetSetting(key string) (op Setting)

Get returns Setting. If key was not found Setting's Value will be empty string

func (*Config) HasOption added in v0.2.1

func (c *Config) HasOption(option string) (exists bool)

HasOption returns true if line:

"key"

was present in the parsed file

func (*Config) HasSetting added in v0.2.1

func (c *Config) HasSetting(key string) (exists bool)

HasSetting returns true if line:

"key = somevalue"

was present in the parsed file

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
}

Option represents single valuse read from config

func (Setting) Bool added in v0.2.1

func (st Setting) Bool() (bool, error)

Bool tries to interpret option 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

func (st Setting) Float64() (float64, error)

Float64 converts Option's Value to float64 if possible If setting was not found in the config this method will return ErrNotFound

func (Setting) Int added in v0.2.1

func (st Setting) Int() (int, error)

Int converts Option's Value to int if possible If option was not found in the config this method will return ErrNotFound

func (Setting) Split added in v0.2.1

func (st Setting) Split(sep string) []Setting

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 (or any other delimiter) separated values like:

myoption = first,second,third

Split(",") will return slice with 3 separate Setting each holding one of "first, second, third"

func (Setting) String added in v0.2.1

func (st Setting) String() (string, error)

String returns option Value as string

Jump to

Keyboard shortcuts

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