settings

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2015 License: BSD-3-Clause Imports: 10 Imported by: 0

README

Go Settings in YAML

The go-settings library implements a settings library on top of YAML. It is meant to provide ease of use without resorting to type conversions and custom YAML parsing.

Usage

The library provides two functions for reading settings from YAML: Read and Load. The Read function reads data from an io.Reader instance while the Load function will load data from the provided file path.

Using Read:

import (
    "fmt"
    "github.com/BlueDragonX/go-settings"
    "os"
)

var file *os.File
var err error
var s *settings.Settings

if file, err = os.Open; err != nil {
    fmt.Println(err)
    os.Exit(1)
}

if s, err = settings.Read(rdr); err != nil {
    fmt.Println(err)
    os.Exit(1)
}

fmt.Println("got settings from reader")

Using Load:

import (
    "fmt"
    "github.com/BlueDragonX/go-settings"
    "os"
)

if s, err = settings.Load("settings.yml"); if err == nil {
    fmt.Println(err)
    os.Exit(1)
}

fmt.Println("got settings from file")

There is also LoadOrExit which does not return an error. It will call Load and if it fails will print the error to stderr and exit. For example:

import (
    "fmt"
    "github.com/BlueDragonX/go-settings"
)

s = settings.LoadOrExit("settings.yml")

fmt.Println("got settings from file")

There are a handful of methods on the settings object which can be used to retrieve values. All methods take a key string as the first argument. The key string itself is a dot (.) separated list of object names. The different methods will handle conversion to different types. With a few exceptions they are self explanatory. These methods are:

  • Raw: Return the raw value as an interface{}.
  • Object: Return a settings object.
  • ObjectArray: Return an array of settings objects.
  • ObjectMap: Return a string index map of settings objects.
  • String
  • StringArray
  • StringMap
  • Int
  • IntArray
  • IntMap
  • Float
  • FloatArray
  • FloatMap
  • Bool
  • BoolArray
  • BoolMap

The get methods may return a predefined error value to indicate failure. These are:

  • KeyError: The key was not found.
  • TypeError: Conversion to the requested type failed.

Each get method has a corresponding Dflt method which takes a second parameter containing a default value to return should an error occur. You may append Dflt to the name of any get function for this functionality.

There are two methods used to set values. They will replace any values at the provide key with the ones provided. Both objects and arrays are supported. These are:

  • Set: Set an object key to the provided value.
  • Append: Append a value to the array at the given key.
  • Delete: Delete a value at the given key.

Thee set methods may return a predefined error value to indicate failure. These are:

  • ObjectError: A child object has an invalid type.
  • IndexError: A key cannot be converted to an integer for a child array.
  • RangeError: The index is out of range for a child array.

License

Copyright (c) 2014 Ryan Bourgeois. Licensed under BSD-Modified. See the LICENSE file for a copy of the license.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var IndexError error = errors.New("invalid index")
View Source
var KeyError error = errors.New("key not found")
View Source
var ObjectError error = errors.New("invalid object")
View Source
var RangeError error = errors.New("index out of range")
View Source
var TypeError error = errors.New("invalid type conversion")

Functions

func ParseSize

func ParseSize(s string) (int64, error)

Parse a size string into a number of bytes. Valid suffixes are b, k, kb, m, mb, g, gb, t, and tb. Case is ignored. A lack of suffix indicates bytes.

Types

type Settings

type Settings struct {
	Key    string
	Values map[interface{}]interface{}
}

func Load

func Load(path string) (*Settings, error)

Load and parse settings from the file at the provided path.

func LoadOrExit

func LoadOrExit(path string) *Settings

Load and parse settings from the file at the provided path. If an error occurs print it to stderr and call os.Exit(1).

func New

func New() *Settings

New returns the pointer to a freshly allocated settings struct.

func Parse

func Parse(data []byte) (*Settings, error)

Parse the provided YAML into a new Settings object.

func Read

func Read(reader io.Reader) (*Settings, error)

Read and parse settings from the provided reader.

func (*Settings) Append

func (s *Settings) Append(key string, value interface{}) error

Append a value to an array. Creates an array at that location if it does not exist. The following errors may be returned:

ObjectError - a child object is not a `map[interface{}]interface{}` or `[]interface{}` IndexError - a key cannot be converted to an integer for a child array

func (*Settings) Bool

func (s *Settings) Bool(key string) (bool, error)

Get a boolean value.

func (*Settings) BoolArray

func (s *Settings) BoolArray(key string) ([]bool, error)

Get an array of boolean values.

func (*Settings) BoolArrayDflt

func (s *Settings) BoolArrayDflt(key string, dflt []bool) []bool

Get an array of bool values. Return `dflt` if an error occurs.

func (*Settings) BoolDflt

func (s *Settings) BoolDflt(key string, dflt bool) bool

Get a bool value. Return `dflt` if an error occurs.

func (*Settings) BoolMap

func (s *Settings) BoolMap(key string) (map[string]bool, error)

Get a map of bools.

func (*Settings) BoolMapDflt

func (s *Settings) BoolMapDflt(key string, dflt map[string]bool) map[string]bool

Get a map of bool values. Return `dflt` if an error occurs.

func (*Settings) Delete

func (s *Settings) Delete(key string) error

Delete a key. May return an error on failure. A non-existent key is not an error case.

ObjectError - a child object is not a `map[interface{}]interface{}` or `[]interface{}` IndexError - a key cannot be converted to an integer for a child array

func (*Settings) Duration

func (s *Settings) Duration(key string) (time.Duration, error)

Get a duration value.

func (*Settings) DurationArray

func (s *Settings) DurationArray(key string) ([]time.Duration, error)

Get an array of durations values.

func (*Settings) DurationArrayDflt

func (s *Settings) DurationArrayDflt(key string, dflt []time.Duration) []time.Duration

Get an array of duration values. Return `dflt` if an error occurs.

func (*Settings) DurationDflt

func (s *Settings) DurationDflt(key string, dflt time.Duration) time.Duration

Get a duration value. Return `dflt` if an error occurs.

func (*Settings) DurationMap

func (s *Settings) DurationMap(key string) (map[string]time.Duration, error)

Get a map of durations.

func (*Settings) DurationMapDflt

func (s *Settings) DurationMapDflt(key string, dflt map[string]time.Duration) map[string]time.Duration

Get a map of duration values. Return `dflt` if an error occurs.

func (*Settings) Float

func (s *Settings) Float(key string) (float64, error)

Get a float value.

func (*Settings) FloatArray

func (s *Settings) FloatArray(key string) ([]float64, error)

Get an array of float values.

func (*Settings) FloatArrayDflt

func (s *Settings) FloatArrayDflt(key string, dflt []float64) []float64

Get an array of float values. Return `dflt` if an error occurs.

func (*Settings) FloatDflt

func (s *Settings) FloatDflt(key string, dflt float64) float64

Get a float value. Return `dflt` if an error occurs.

func (*Settings) FloatMap

func (s *Settings) FloatMap(key string) (map[string]float64, error)

Get a map of floats.

func (*Settings) FloatMapDflt

func (s *Settings) FloatMapDflt(key string, dflt map[string]float64) map[string]float64

Get a map of float values. Return `dflt` if an error occurs.

func (*Settings) Has

func (s *Settings) Has(key string) bool

Has returns true if a value exists.

func (*Settings) Int

func (s *Settings) Int(key string) (int, error)

Get an integer value.

func (*Settings) IntArray

func (s *Settings) IntArray(key string) ([]int, error)

Get an array of integer values.

func (*Settings) IntArrayDflt

func (s *Settings) IntArrayDflt(key string, dflt []int) []int

Get an array of integer values. Return `dflt` if an error occurs.

func (*Settings) IntDflt

func (s *Settings) IntDflt(key string, dflt int) int

Get an integer value. Return `dflt` if an error occurs.

func (*Settings) IntMap

func (s *Settings) IntMap(key string) (map[string]int, error)

Get a map of integers.

func (*Settings) IntMapDflt

func (s *Settings) IntMapDflt(key string, dflt map[string]int) map[string]int

Get a map of integer values. Return `dflt` if an error occurs.

func (*Settings) Object

func (s *Settings) Object(key string) (*Settings, error)

Get a settings object.

func (*Settings) ObjectArray

func (s *Settings) ObjectArray(key string) ([]*Settings, error)

Get an array of settings objects.

func (*Settings) ObjectArrayDflt

func (s *Settings) ObjectArrayDflt(key string, dflt []*Settings) []*Settings

Get an array of settings objects. Return `dflt` if an error occurs.

func (*Settings) ObjectDflt

func (s *Settings) ObjectDflt(key string, dflt *Settings) *Settings

Get a settings object. Return `dflt` if an error occurs.

func (*Settings) ObjectMap

func (s *Settings) ObjectMap(key string) (map[string]*Settings, error)

Get a map of settings objects.

func (*Settings) ObjectMapDflt

func (s *Settings) ObjectMapDflt(key string, dflt map[string]*Settings) map[string]*Settings

Get a map of settings objects. Return `dflt` if an error occurs.

func (*Settings) Raw

func (s *Settings) Raw(key string) (interface{}, error)

Get a value from the settings object.

func (*Settings) RawDflt

func (s *Settings) RawDflt(key string, dflt interface{}) interface{}

Get a value from the settings object. Return `dflt` if an error occurs.

func (*Settings) Set

func (s *Settings) Set(key string, value interface{}) error

Set a value in the settings object. This overrides all keys in the path that are not already objects. The following errors may be returned:

ObjectError - a child object is not a `map[interface{}]interface{}` or `[]interface{}` IndexError - a key cannot be converted to an integer for a child array RangeError - the index is out of range for a child array

func (*Settings) Size

func (s *Settings) Size(key string) (int64, error)

Get a settings value as a size (in bytes).

func (*Settings) SizeArray

func (s *Settings) SizeArray(key string) ([]int64, error)

Get an array of size values.

func (*Settings) SizeArrayDflt

func (s *Settings) SizeArrayDflt(key string, dflt []int64) []int64

Get an array of size values. Return `dflt` if an error occurs.

func (*Settings) SizeDflt

func (s *Settings) SizeDflt(key string, dflt int64) int64

Get a size value. Return `dflt` if an error occurs.

func (*Settings) SizeMap

func (s *Settings) SizeMap(key string) (map[string]int64, error)

Get a map of sizes.

func (*Settings) SizeMapDflt

func (s *Settings) SizeMapDflt(key string, dflt map[string]int64) map[string]int64

Get a map of size values. Return `dflt` if an error occurs.

func (*Settings) String

func (s *Settings) String(key string) (string, error)

Get a string value.

func (*Settings) StringArray

func (s *Settings) StringArray(key string) ([]string, error)

Get an array of string values.

func (*Settings) StringArrayDflt

func (s *Settings) StringArrayDflt(key string, dflt []string) []string

Get an array of string values. Return `dflt` if an error occurs.

func (*Settings) StringDflt

func (s *Settings) StringDflt(key string, dflt string) string

Get a string value. Return `dflt` if an error occurs.

func (*Settings) StringMap

func (s *Settings) StringMap(key string) (map[string]string, error)

Get a map of strings.

func (*Settings) StringMapDflt

func (s *Settings) StringMapDflt(key string, dflt map[string]string) map[string]string

Get a map of string values. Return `dflt` if an error occurs.

Jump to

Keyboard shortcuts

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