conf

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2021 License: MIT Imports: 11 Imported by: 3

README

conf

Code Analysis Go Reference codecov GitHub tag (latest SemVer)

The configuration reader with as few dependencies as possible.

The library provides the base code only and the interfaces. All parsers and readers must be places in the separate repositories.

Dependecies

  • The github.com/spf13/cast has been added as dependency to avoid the code duplication. This library has only one dependecy of github.com/stretchr/testify. I will make a hard copy of it if the number of dependencies are increased.
  • The github.com/stretchr/testify is used for testing only.

Alternatives

  • viper (https://github.com/spf13/viper) is the most know library, it's very heavy and very reach in defferent features.
  • koanf (https://github.com/knadh/koanf) is an attempt to make a better version of the viper, but also contains all parsers in same repo, so the list of dependeciies is pretty huge.

License

MIT licensed. See the bundled LICENSE file for more details.

Documentation

Overview

Package conf provides and implements the interface to manage the Configurations.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoParser is an error returned if the parser was not given
	ErrNoParser = errors.New("no parser")
	// ErrNoStream is an error returned if the stream was not given
	ErrNoStream = errors.New("no data stream")
)
View Source
var BoolValues = map[string]bool{
	"yes": true,
	"Yes": true,
	"y":   true,
	"Y":   true,
	"On":  true,
	"on":  true,
}

BoolValues is a global extendable list of the string values that should be converted as true or false

Example:

conf.GetSet("flag", "sí")
conf.GetBool("flag") == false
conf.TrueValues["sí"] = true
conf.GetBool("flag") == true

Functions

func Get

func Get(key string) interface{}

Get returns a value for a given key if it is set or default value Returns `nil` if key not found The alias to work with an instance of the global configuration manager.

func GetBool

func GetBool(key string) bool

GetBool casts a value for a given key to Bool The alias to work with an instance of the global configuration manager.

func GetDuration

func GetDuration(key string) time.Duration

GetDuration casts a value for a given key to `time.Duration` The alias to work with an instance of the global configuration manager.

func GetFloat32

func GetFloat32(key string) float32

GetFloat32 casts a value for a given key to Float32 The alias to work with an instance of the global configuration manager.

func GetFloat64

func GetFloat64(key string) float64

GetFloat64 casts a value for a given key to Float64 The alias to work with an instance of the global configuration manager.

func GetInt

func GetInt(key string) int

GetInt casts a value for a given key to Int The alias to work with an instance of the global configuration manager.

func GetInt16

func GetInt16(key string) int16

GetInt16 casts a value for a given key to Int16 The alias to work with an instance of the global configuration manager.

func GetInt32

func GetInt32(key string) int32

GetInt32 casts a value for a given key to Int32 The alias to work with an instance of the global configuration manager.

func GetInt64

func GetInt64(key string) int64

GetInt64 casts a value for a given key to Int64 The alias to work with an instance of the global configuration manager.

func GetInt8

func GetInt8(key string) int8

GetInt8 casts a value for a given key to Int8 The alias to work with an instance of the global configuration manager.

func GetString

func GetString(key string) string

GetString casts a value for a given key to String The alias to work with an instance of the global configuration manager.

func GetTime

func GetTime(key string) time.Time

GetTime casts a value for a given key to `time.Time` The alias to work with an instance of the global configuration manager.

func Keys

func Keys() []string

Keys returns the list of the stored keys The alias to work with an instance of the global configuration manager.

func Load

func Load(ctx context.Context) error

Load calls the `Read` function of all readers in provided order The alias to work with an instance of the global configuration manager.

Types

type Conf

type Conf interface {
	// WithReaders stores the given readers to load the data in the Load function
	WithReaders(readers ...Reader) Conf
	// WithTransformers stores the given transformers to change the output of the Get function.
	// All trasformers will be applied in the given order.
	WithTransformers(transformers ...Transform) Conf

	// Reset creates an empty storage and clears the old one
	// It does not clear the default values
	// Can be used in the Unit Tests
	Reset() Conf
	// Load calls the `Read` function of all readers  in provided order
	Load(ctx context.Context) error
	// Keys returns the list of the stored keys
	Keys() []string
	// SetDefault sets a default value for a key
	SetDefault(key string, value interface{}) Conf
	// Set overrides the current value of a given key.
	Set(key string, value interface{}) Conf

	Get(key string) interface{}
	// GetString casts a value for a given key to String
	GetString(key string) string
	// GetInt casts a value for a given key to Int
	GetInt(key string) int
	// GetInt8 casts a value for a given key to Int8
	GetInt8(key string) int8
	// GetInt16 casts a value for a given key to Int16
	GetInt16(key string) int16
	// GetInt32 casts a value for a given key to Int32
	GetInt32(key string) int32
	// GetInt64 casts a value for a given key to Int64
	GetInt64(key string) int64
	// GetBool casts a value for a given key to Bool
	GetBool(key string) bool
	// GetFloat32 casts a value for a given key to Float32
	GetFloat32(key string) float32
	// GetFloat64 casts a value for a given key to Float64
	GetFloat64(key string) float64
	// GetTime casts a value for a given key to `time.Time`
	GetTime(key string) time.Time
	// GetDuration casts a value for a given key to `time.Duration`
	GetDuration(key string) time.Duration
}

Conf is a registry interface

func GlobalConf

func GlobalConf() Conf

GlobalConf returns the global Conf object

func New

func New() Conf

New crates the an instance of Conf interface

func Reset

func Reset() Conf

Reset creates an empty storage and clears the old one It does not clear the default values Can be used in the Unit Tests The alias to work with an instance of the global configuration manager.

func Set

func Set(key string, value interface{}) Conf

Set overrides the current value of a given key. The alias to work with an instance of the global configuration manager.

func SetDefault

func SetDefault(key string, value interface{}) Conf

SetDefault sets a default value for a key The alias to work with an instance of the global configuration manager.

func WithReaders

func WithReaders(readers ...Reader) Conf

WithReaders overrides the readers with the given ones The alias to work with an instance of the global configuration manager.

func WithTransformers

func WithTransformers(transformers ...Transform) Conf

WithTransformers stores the given transformers to change the output of the Get function All trasformers will be applied in the given order. The alias to work with an instance of the global configuration manager.

type ParseFunc

type ParseFunc func(ctx context.Context, r io.Reader) (interface{}, error)

ParseFunc is a type for the parsing function

type Parser

type Parser interface {
	Reader

	WithParser(parser ParseFunc) Parser
	WithPrefix(prefix string) Parser
}

Parser is an extension for the Reader interface.

func NewFileParser

func NewFileParser(filename string) (Parser, error)

NewFileParser creates an instance of the Parser and opens the given file

func NewStreamParser

func NewStreamParser(stream io.Reader) Parser

NewStreamParser creates an instance of the Parser to read from a given stream

type Reader

type Reader interface {
	// Read reads the data and returns a raw data
	Read(ctx context.Context) (interface{}, error)
	// Returns a prefix to be used for all keys of the values provided by the reader
	Prefix() string
}

Reader is an interface for the configuration readers

type Transform

type Transform func(key, value interface{}, c Conf) interface{}

Transform is a function to transform the data

Jump to

Keyboard shortcuts

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