conf

package module
v0.0.0-...-80d6b10 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2015 License: MIT Imports: 8 Imported by: 0

README

GoDoc

conf

conf is a simple configuration library for Go. It provides the functionality to import configuration files of any format.

Use go get to install or update the package:

go get -u github.com/tsne/conf

Examples

Loading a JSON configuration file:

package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"os"

	"github.com/tsne/conf"
)

func main() {
	configFile := flag.String("config", "myconf.json", "configuration file")
	flag.Parse()

	f, err := os.Open(*configFile)
	if err != nil {
		panic(err)
	}

	c := conf.MustLoad(f, json.Unmarshal)
	for k, v := range c {
		fmt.Printf("%v: %v\n", k, v)
	}
}

Decoding a struct:

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"time"

	"github.com/tsne/conf"
)

const myConfStr = `{
	"keepalive": true,
	"server": {
		"address": "192.168.1.7",
		"port": 8080
	},
	"timeouts": {
		"requests": "5s"
	}
}`

type serverConf struct {
	Address string `config:",required"`
	Port    int    `config:",required"`
}

func main() {
	c := conf.MustLoad(bytes.NewBufferString(myConfStr), json.Unmarshal)

	var requestTimeout time.Duration
	if err := c.Decode("timeouts.requests", &requestTimeout); err != nil {
		requestTimeout = 10 * time.Second
	}

	var svConf serverConf
	if err := c.Decode("server", &svConf); err != nil {
		panic(err)
	}

	fmt.Printf("listening to %s on port %d\n", svConf.Address, svConf.Port)
	fmt.Printf("request timeout: %s\n", requestTimeout)
}

Documentation

Overview

Package conf provides functionality to read configuration files of any format.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config map[string]interface{}

Config represents a container for all parsed configuration values.

func Load

func Load(r io.Reader, unmarshal Unmarshaler) (Config, error)

Load loads the configuration from the source r using unmarshal to parse the input stream. The returned Config object holds all parsed values.

func MustLoad

func MustLoad(r io.Reader, unmarshal Unmarshaler) Config

MustLoad ensures the loading of the configuration from the r using unmarshal to parse the input stream. This function calls Load and panics on error.

func (Config) Decode

func (c Config) Decode(key string, value interface{}) error

Decode stores the configuration value with the given key in value. If value has an invalid type an error is returned. Decode tries to convert the configuration value to value's type (e.g. the string "7" is converted to the integer 7). The given value has to be a pointer, otherwise an error is returned.

When decoding a struct each field could provide a 'config' tag. If the tag is "-" the field will be ignored. Otherwise it consists of a custom configuration key, followed by an optional comma and options. If the custom configuration key is empty the field name will be used. Currently only "required" is a valid option. If the "required" option will be set and the key does not exist an error is returned.

Tag examples:

// Field will be ignored
Field string `config:"-"`

// Field is required, the key is "Field"
Field string `config:",required"`

// Field is optional, the key is "foobar"
Field string `config:"foobar"`

// Field is required, the key is "foobar"
Field string `config:"foobar,required"`

func (Config) Value

func (c Config) Value(key string) (*Value, error)

Value returns the configuration value with the given key. If there is a hierarchy of configuration values a dot can be used to separate the different levels (e.g. "foo.bar" gets the value of the key 'bar' which lies under the key 'foo').

type Unmarshaler

type Unmarshaler func(data []byte, value interface{}) error

Unmarshaler represents a function which is able to parse the configuration data and store the parsed values into the given value.

type Value

type Value reflect.Value

Value represents a specific configuration value which can be converted to several types.

func (*Value) Bool

func (v *Value) Bool() (bool, error)

Bool tries to convert the configuration value to a boolean value. If the conversion fails an error is returned.

func (*Value) Float

func (v *Value) Float() (float64, error)

Float tries to convert the configuration value to a floating point value. If the conversion fails an error is returned.

func (*Value) Int

func (v *Value) Int() (int64, error)

Int tries to convert the configuration value to an integer value. If the conversion fails an error is returned.

func (*Value) String

func (v *Value) String() (string, error)

String tries to convert the configuration value to a string value. If the conversion fails an error is returned.

func (*Value) Uint

func (v *Value) Uint() (uint64, error)

Uint tries to convert the configuration value to an unsigned integer value. If the conversion fails an error is returned.

Jump to

Keyboard shortcuts

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