revip

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2022 License: Unlicense Imports: 16 Imported by: 1

README

revip

Dead-simple configuration loader.

It supports:

  • JSON, TOML, YAML and you could add your own format unmarshaler (see Unmarshaler type)
  • file, reader and environment sources support, also you could add your own (see Option type and sources.go)
  • extendable postprocessing support (defaults, validation, expansion, see Option type and postprocess.go)
  • dot-notation to access configuration keys

Godoc


example
run

Basic example showing basics about configuration loading:

  • default values
  • validation rules
  • config expansion (loading key from file)
  • working with nested types
  • unmarshaling from JSON, YAML, TOML
$ cd ./example/basic
$ go run ./main.go
(main.Config) {
 SerialNumber: (int) 1,
 Nested: (*main.NestedConfig)(0xc00000e0c0)({
  Value: (string) (len=11) "hello world",
  Flag: (bool) false
 }),
 MapNested: (map[string]*main.NestedConfig) {
 },
 SliceNested: ([]*main.NestedConfig) {
 },
 StringSlice: ([]string) <nil>,
 IntSlice: ([]int) (len=3 cap=3) {
  (int) 666,
  (int) 777,
  (int) 888
 },
 key: (string) (len=18) "super secret value"
}

Run basic example with some keys befined through environment variables:

environment variables are defined in makefile

$ make
(main.Config) {
 SerialNumber: (int) 2,
 Nested: (*main.NestedConfig)(0xc00000e0c0)({
  Value: (string) (len=12) "\"hello user\"",
  Flag: (bool) false
 }),
 MapNested: (map[string]*main.NestedConfig) {
 },
 SliceNested: ([]*main.NestedConfig) {
 },
 StringSlice: ([]string) <nil>,
 IntSlice: ([]int) (len=3 cap=3) {
  (int) 888,
  (int) 777,
  (int) 666
 },
 key: (string) (len=18) "super secret value"
}

license

public domain

Documentation

Index

Constants

View Source
const (
	SchemeEmpty   = ""
	SchemeFile    = "file" // file://./config.yml
	SchemeEnviron = "env"  // env://prefix
)

Variables

View Source
var (
	// FromSchemes represents schemes supported for sources.
	FromSchemes = []string{
		SchemeFile,
		SchemeEnviron,
	}
	// ToSchemes represents schemes supported for destrinations.
	ToSchemes = []string{
		SchemeFile,
	}
)

Functions

func Postprocess

func Postprocess(c Config, options ...PostprocessOption) error

func TreePathString

func TreePathString(t Tree) string

Types

type Config

type Config interface{}

Config is a configuration represented by user-specified type.

type Container

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

Container represents configuration loaded by `Load`.

func Load

func Load(v Config, options ...SourceOption) (*Container, error)

Load applies each `options` in order to fill the configuration in `v` and constructs a `*Revip` data-structure.

func New

func New(c Config) *Container

New wraps configuration represented by `c` with come useful methods.

func (*Container) Clone

func (r *Container) Clone() (Config, error)

Clone returns a shallow copy of the configuration with the same type.

func (*Container) Copy

func (r *Container) Copy(v Config) error

Copy writes a shallow copy of the configuration into `v`.

func (*Container) DeepClone

func (r *Container) DeepClone() (Config, error)

DeepClone returns a deep copy of the configuration with the same type.

func (*Container) DeepCopy

func (r *Container) DeepCopy(v Config) error

DeepCopy writes a deep copy of the configuration into `v`.

func (*Container) Default

func (r *Container) Default() error

Default postprocess configuration with default values or returns an error.

func (*Container) Empty

func (r *Container) Empty()

Empty allocates a new empty configuration, discarding any previously loaded data.

func (*Container) EmptyClone

func (r *Container) EmptyClone() Config

EmptyClone returns empty configuration type clone.

func (*Container) Expand

func (r *Container) Expand() error

Expand postprocess configuration with expansion or returns an error.

func (*Container) Path

func (r *Container) Path(path string) (Config, error)

Path uses dot notation to retrieve substruct addressable by `path` or return an error if key was not found(`ErrNotFound`) or something gone terribly wrong.

func (*Container) Replace

func (r *Container) Replace(c Config)

Replace overrides internally stored configuration with passed value.

func (*Container) Unwrap

func (r *Container) Unwrap() Config

Unwrap returns a pointer to the inner configuration data structure.

func (*Container) Validate

func (r *Container) Validate() error

Validate postprocess configuration with validation or returns an error.

type Defaultable

type Defaultable interface {
	Default()
}

Defaultable is an interface which any `Config` could implement to define a custom default values for sub-tree it owns.

type DestinationOption

type DestinationOption func(c Config) error

func ToFile

func ToFile(path string, f Marshaler) DestinationOption

ToFile is an `DestinationOption` constructor which creates a thunk to write configuration to file addressable by `path` with content encoded with `f` marshaler.

func ToURL

func ToURL(u string, e Marshaler) (DestinationOption, error)

ToURL creates a destination from URL. Example URL's:

  • file://./config.yml

func ToWriter

func ToWriter(w io.Writer, f Marshaler) DestinationOption

ToWriter is an `DestinationOption` constructor which creates a thunk to write configuration to `r` and encode it with `f` marshaler.

type ErrFileNotFound

type ErrFileNotFound struct {
	Path string
	Err  error
}

ErrFileNotFound should be returned if configuration file was not found.

func (*ErrFileNotFound) Error

func (e *ErrFileNotFound) Error() string

type ErrMarshal

type ErrMarshal struct {
	At  string
	Err error
}

ErrMarshal should be returned if key marshaling failed.

func (*ErrMarshal) Error

func (e *ErrMarshal) Error() string

type ErrPathNotFound

type ErrPathNotFound struct {
	Path string
}

ErrPathNotFound should be returned if key (path) was not found in configuration.

func (*ErrPathNotFound) Error

func (e *ErrPathNotFound) Error() string

type ErrPostprocess

type ErrPostprocess struct {
	Path string
	Err  error
}

ErrPostprocess represents an error occured at the postprocess stage (set defaults, validation, etc)

func (*ErrPostprocess) Error

func (e *ErrPostprocess) Error() string

type ErrUnexpectedKind

type ErrUnexpectedKind struct {
	Type     reflect.Type
	Got      reflect.Kind
	Expected []reflect.Kind
}

ErrUnexpectedKind represents an unexpected interface{} value kind received by some function. For example passing non pointer value to a function which expects pointer (like json.Unmarshal)

func (*ErrUnexpectedKind) Error

func (e *ErrUnexpectedKind) Error() string

type ErrUnexpectedScheme

type ErrUnexpectedScheme struct {
	Got      string
	Expected []string
}

ErrUnexpectedScheme represents an unexpected URL scheme.

func (*ErrUnexpectedScheme) Error

func (e *ErrUnexpectedScheme) Error() string

type ErrUnmarshal

type ErrUnmarshal struct {
	At  string
	Err error
}

ErrUnmarshal should be returned if key unmarshaling failed.

func (*ErrUnmarshal) Error

func (e *ErrUnmarshal) Error() string

type Expandable

type Expandable interface {
	Expand() error
}

Expandable is an interface which any `Config` could implement to define an expansion rules for sub-tree it owns.

type Marshaler

type Marshaler = func(v interface{}) ([]byte, error)

Marshaler describes a generic marshal interface for data encoding which could be used to extend supported formats by defining new `Option` implementations.

var (
	JsonMarshaler Marshaler = json.Marshal
	YamlMarshaler Marshaler = yaml.Marshal
	TomlMarshaler Marshaler = toml.Marshal
)

type PostprocessOption

type PostprocessOption func(c Tree) error

func WithDefaults

func WithDefaults() PostprocessOption

func WithExpansion

func WithExpansion() PostprocessOption

func WithNoNilPointers

func WithNoNilPointers() PostprocessOption

func WithValidation

func WithValidation() PostprocessOption

type SourceOption

type SourceOption func(c Config) error

func FromEnviron

func FromEnviron(prefix string) SourceOption

FromEnviron is an `SourceOption` constructor which creates a thunk to read configuration from environment. It uses `github.com/kelseyhightower/envconfig` underneath.

func FromFile

func FromFile(path string, f Unmarshaler) SourceOption

FromFile is an `SourceOption` constructor which creates a thunk to read configuration from file addressable by `path` with content decoded with `f` unmarshaler.

func FromReader

func FromReader(r io.Reader, f Unmarshaler) SourceOption

FromReader is an `SourceOption` constructor which creates a thunk to read configuration from `r` and decode it with `f` unmarshaler. Current implementation buffers all data in memory.

func FromURL

func FromURL(u string, d Unmarshaler) (SourceOption, error)

FromURL creates a source from URL. Example URL's:

  • file://./config.yml
  • env://prefix

type Tree

type Tree interface {
	Interface() interface{}
	Value() reflect.Value
	WithValue(reflect.Value) Tree
	Next() []Tree
	WithNext(Tree) Tree
	Previous() Tree
	WithPrevious(Tree) Tree
	Name() string
}

func NewTree

func NewTree(value reflect.Value, handler func(Tree) error) (Tree, error)

func TreePathSlice

func TreePathSlice(t Tree) []Tree

type TreeArrayFieldNode

type TreeArrayFieldNode struct {
	Tree
	Field int
}

func (*TreeArrayFieldNode) Name

func (n *TreeArrayFieldNode) Name() string

type TreeMapFieldNode

type TreeMapFieldNode struct {
	Tree
	Field reflect.Value
}

func (*TreeMapFieldNode) Name

func (n *TreeMapFieldNode) Name() string

type TreeNode

type TreeNode struct {
	Val    reflect.Value
	Childs []Tree
	Parent Tree
}

func (*TreeNode) Interface

func (n *TreeNode) Interface() interface{}

func (*TreeNode) Name

func (n *TreeNode) Name() string

func (*TreeNode) Next

func (n *TreeNode) Next() []Tree

func (*TreeNode) Previous

func (n *TreeNode) Previous() Tree

func (*TreeNode) Value

func (n *TreeNode) Value() reflect.Value

func (*TreeNode) WithNext

func (n *TreeNode) WithNext(t Tree) Tree

func (*TreeNode) WithPrevious

func (n *TreeNode) WithPrevious(t Tree) Tree

func (*TreeNode) WithValue

func (n *TreeNode) WithValue(v reflect.Value) Tree

type TreeSliceFieldNode

type TreeSliceFieldNode struct {
	Tree
	Field int
}

func (*TreeSliceFieldNode) Name

func (n *TreeSliceFieldNode) Name() string

type TreeStructFieldNode

type TreeStructFieldNode struct {
	Tree
	Field reflect.StructField
}

func (*TreeStructFieldNode) Name

func (n *TreeStructFieldNode) Name() string

type Unmarshaler

type Unmarshaler = func(in []byte, v interface{}) error

Unmarshaler describes a generic unmarshal interface for data decoding which could be used to extend supported formats by defining new `SourceOption` implementations.

var (
	JsonUnmarshaler Unmarshaler = json.Unmarshal
	YamlUnmarshaler Unmarshaler = yaml.Unmarshal
	TomlUnmarshaler Unmarshaler = toml.Unmarshal
)

type Validatable

type Validatable interface {
	Validate() error
}

Validatable is an interface which any `Config` could implement to define a validation rules for sub-tree it owns.

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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