ucfg

package module
v0.3.5 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2016 License: Apache-2.0 Imports: 11 Imported by: 0

README

Build Status Go Report Card

ucfg - Universal Configuration

ucfg is a Golang library to handle yaml and json configuration files in your Golang project. It was developed for the libbeat framework and used by all beats.

API Documentation

The full API Documentation can be found here.

Examples

A few examples on how ucfg can be used. All examples below assume, that the following packages are imported:

import (
	"github.com/elastic/go-ucfg"
	"github.com/elastic/go-ucfg/yaml"
)

### Dot notations

ufcg allows you to load yaml configuration files using dots instead of indentation. For example instead of having:

config:
  user: name

with ucfg you can write:

config.user: name

This makes configurations easier and simpler.

To load such a config file in Golang, use the following command:

config, err := yaml.NewConfigWithFile(path, ucfg.PathSep("."))
Validation and Defaults

ucfg allows to automatically validate fields and set defaults for fields in case they are not defined.

// Defines struct to read config from
type ExampleConfig struct {
    Counter  string 	`config:"username" validate:"min=0, max=9"`
}

// Defines default config option
var (
    defaultConfig = ExampleConfig{
		    Counter: 4,
    }
}

func main() {
    config, err := yaml.NewConfigWithFile(path, ucfg.PathSep("."))
    config.Unpack(defaultConfig)
}

The above uses Counter as the config variable. ucfg assures that the value is between 0 and 9 and will return an error if this is not the case. In addition, if the value is not set, it will default to 4.

Requirements

ucfg has the following requirements:

  • Golang 1.5+

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMissing = errors.New("missing field")

	ErrDuplicateValidator = errors.New("validator already registered")

	ErrTypeNoArray = errors.New("field is no array")

	ErrTypeMismatch = errors.New("type mismatch")

	ErrKeyTypeNotString = errors.New("key must be a string")

	ErrIndexOutOfRange = errors.New("out of range index")

	ErrPointerRequired = errors.New("pointer required for unpacking configurations")

	ErrArraySizeMistach = errors.New("Array size mismatch")

	ErrExpectedObject = errors.New("expected object")

	ErrNilConfig = errors.New("config is nil")

	ErrNilValue = errors.New("nil value is invalid")

	ErrTODO = errors.New("TODO - implement me")

	ErrDuplicateKeey = errors.New("duplicate key")

	ErrOverflow = errors.New("integer overflow")

	ErrNegative = errors.New("negative value")

	ErrZeroValue = errors.New("zero value")

	ErrRequired = errors.New("missing required field")

	ErrEmpty = errors.New("empty field")
)

error Reasons

View Source
var (
	ErrConfig         = errors.New("Configuration error")
	ErrImplementation = errors.New("Implementation error")
	ErrUnknown        = errors.New("Unspecified")
)

error classes

View Source
var ResolveEnv = Resolve(func(name string) (string, error) {
	value := os.Getenv(name)
	if value == "" {
		return "", ErrMissing
	}
	return value, nil
})

Functions

func RegisterValidator added in v0.3.0

func RegisterValidator(name string, cb ValidatorCallback) error

Types

type Config

type Config struct {
	// contains filtered or unexported fields
}

func New

func New() *Config

func NewFrom

func NewFrom(from interface{}, opts ...Option) (*Config, error)

func (*Config) Bool

func (c *Config) Bool(name string, idx int, opts ...Option) (bool, error)

func (*Config) Child

func (c *Config) Child(name string, idx int, opts ...Option) (*Config, error)

func (*Config) CountField

func (c *Config) CountField(name string, opts ...Option) (int, error)

number of elements for this field. If config value is a list, returns number of elements in list

func (*Config) Float

func (c *Config) Float(name string, idx int, opts ...Option) (float64, error)

func (*Config) GetFields

func (c *Config) GetFields() []string

func (*Config) HasField

func (c *Config) HasField(name string) bool

func (*Config) Int

func (c *Config) Int(name string, idx int, opts ...Option) (int64, error)

func (*Config) IsArray added in v0.3.3

func (c *Config) IsArray() bool

func (*Config) IsDict added in v0.3.3

func (c *Config) IsDict() bool

func (*Config) Merge

func (c *Config) Merge(from interface{}, options ...Option) error

func (*Config) Parent added in v0.3.0

func (c *Config) Parent() *Config

func (*Config) Path added in v0.3.0

func (c *Config) Path(sep string) string

func (*Config) PathOf added in v0.3.0

func (c *Config) PathOf(field, sep string) string

func (*Config) SetBool

func (c *Config) SetBool(name string, idx int, value bool, opts ...Option) error

func (*Config) SetChild

func (c *Config) SetChild(name string, idx int, value *Config, opts ...Option) error

func (*Config) SetFloat

func (c *Config) SetFloat(name string, idx int, value float64, opts ...Option) error

func (*Config) SetInt

func (c *Config) SetInt(name string, idx int, value int64, opts ...Option) error

func (*Config) SetString

func (c *Config) SetString(name string, idx int, value string, opts ...Option) error

func (*Config) SetUint added in v0.3.0

func (c *Config) SetUint(name string, idx int, value uint64, opts ...Option) error

func (*Config) String

func (c *Config) String(name string, idx int, opts ...Option) (string, error)

func (*Config) Uint added in v0.3.0

func (c *Config) Uint(name string, idx int, opts ...Option) (uint64, error)

func (*Config) Unpack

func (c *Config) Unpack(to interface{}, options ...Option) error

type Error added in v0.3.0

type Error interface {
	error
	Reason() error

	// error class, one of ErrConfig, ErrImplementation, ErrUnknown
	Class() error

	Message() string

	// [optional] path of config element error occurred for
	Path() string

	// [optional] stack trace
	Trace() string
}

type Meta added in v0.3.0

type Meta struct {
	Source string
}

Meta holds additional meta data per config value

type Option

type Option func(*options)
var VarExp Option = func(o *options) {
	o.varexp = true
}

func Env added in v0.3.0

func Env(e *Config) Option

func MetaData added in v0.3.0

func MetaData(meta Meta) Option

func PathSep

func PathSep(sep string) Option

func Resolve added in v0.3.0

func Resolve(fn func(name string) (string, error)) Option

func StructTag

func StructTag(tag string) Option

func ValidatorTag added in v0.3.0

func ValidatorTag(tag string) Option

type Unpacker added in v0.3.0

type Unpacker interface {
	Unpack(interface{}) error
}

type Validator added in v0.3.0

type Validator interface {
	Validate() error
}

type ValidatorCallback added in v0.3.0

type ValidatorCallback func(interface{}, string) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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