config

package
v0.0.0-...-93b1e75 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2023 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package config provides configuration service that can use arbitrary encoders to fill external structures with configuration data.

Typical usage might be such as that:

cfg := config.Must(config.NewFromBytes(data, encoder))

err = cfg.Get(&myStructure)
if err != nil {
    panic(err)
}

err = cfg.GetByKey("myKey", &myStructure)
if err != nil {
    panic(err)
}

Argument 'data' is raw configuration bytes in some format that provided encoder can parse. Argument 'encoder' can be one of predefined ones - json.Encoder, yaml.Encoder, toml.Encoder, or some custom one.

Variable 'myStructure' is your custom structure designed to hold configuration data. For example if JSON configuration data is '{"myKey":{"age":20, "name":"Peter"}}', then myStructure could be of type struct { Age int, Name string }

More likely you will use some of more specialized packages that work with specific sources of configuration data, like file on disk or etcd service.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoSuchKey error is returned when source configuration data doesn't have the wanted key.
	ErrNoSuchKey = errors.New("no such key")

	// ErrNotMap error is returned when retrieved data is not a map, but it has to be.
	ErrNotMap = errors.New("data by key is not a map")
)

Functions

This section is empty.

Types

type Config

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

Config structure that provides configuration service. Don't create it manually, use the functions down below instead.

func Must

func Must(config *Config, err error) *Config

Must function panics on any error that can rise after creating configuration service. Use this like that:

cfg := config.Must(config.NewFromBytes(...))

func NewFromBytes

func NewFromBytes(dataBytes []byte, encoder Encoder) (*Config, error)

NewFromBytes function creates new configuration service using source bytes and chosen encoder. Most likely you will use one of the predefined encoders: json.Encoder, yaml.Encoder or toml.Encoder.

func NewFromRaw

func NewFromRaw(data map[string]interface{}) *Config

NewFromRaw function creates new configuration service using already parsed raw configuration data. You will rarely use this function yourself.

func NewInner

func NewInner(key string, config *Config) (*Config, error)

NewInner function creates new configuration service using data under some key in another already existing configuration service.

func (*Config) Get

func (config *Config) Get(out interface{}) error

Get method fills structure that 'out' parameter points to with all configuration data. It will return an error if that structure doesn't have some field, or it is not of an appropriate type.

func (*Config) GetByKey

func (config *Config) GetByKey(key string, out interface{}) error

GetByKey method fills structure that 'out' parameter points to with configuration data lying under some key. Rules are the same as for Get method. You can use empty key to retrieve all data or use a composite key like 'key1.key2.key3' to retrieve some deep data.

func (*Config) GetRaw

func (config *Config) GetRaw() map[string]interface{}

GetRaw method retrieves raw representation of configuration data. It should be used only internally or in very special cases.

func (*Config) GetRawByKey

func (config *Config) GetRawByKey(key string) (interface{}, error)

GetRawByKey method retrieves raw representation of data that lies inside configuration under some key. You can use empty key to retrieve all data or use a composite key like 'key1.key2.key3' to retrieve some deep data. It should be used only internally or in very special cases.

func (*Config) IsNoSuchKeyError

func (config *Config) IsNoSuchKeyError(err error) bool

IsNoSuchKeyError checks if provided error is 'NoSuchKey' one.

type Encoder

type Encoder interface {
	Type() string
	Encode([]byte, *map[string]interface{}) error
}

Encoder is an object used to convert source bytes into structured representation of configuration.

Directories

Path Synopsis
encoder
json
Package json provides JSON encoder to use with config package.
Package json provides JSON encoder to use with config package.
toml
Package toml provides TOML encoder to use with config package.
Package toml provides TOML encoder to use with config package.
yaml
Package yaml provides YAML encoder to use with config package.
Package yaml provides YAML encoder to use with config package.
env
Package env is a wrapper around config package and allows reading configuration from some source and parsing it with some encoder, both defined in environment variables.
Package env is a wrapper around config package and allows reading configuration from some source and parsing it with some encoder, both defined in environment variables.
etcd
Package etcd is a wrapper around config package and allows reading configuration from some key in etcd server and parsing it with some encoder, where endpoints and key are defined in environment variables.
Package etcd is a wrapper around config package and allows reading configuration from some key in etcd server and parsing it with some encoder, where endpoints and key are defined in environment variables.
file
Package file is a wrapper around config package and allows reading configuration from some file on disk and parsing it with some encoder, where file name is defined in environment variable.
Package file is a wrapper around config package and allows reading configuration from some file on disk and parsing it with some encoder, where file name is defined in environment variable.
Package etcd is a wrapper around config package and allows reading configuration from some key in etcd server.
Package etcd is a wrapper around config package and allows reading configuration from some key in etcd server.
Package file is a wrapper around config package and allows reading configuration from some file on disk.
Package file is a wrapper around config package and allows reading configuration from some file on disk.
Package i2s provides convertor from raw data representation as map[string]interface{} into external structure using reflection.
Package i2s provides convertor from raw data representation as map[string]interface{} into external structure using reflection.

Jump to

Keyboard shortcuts

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