gonfig

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2020 License: MIT Imports: 9 Imported by: 2

README

gonfig GoDoc Build Status Coverage Status Go Report Card

Configuration for Go Application Changing in Runtime

Motivation

There are parameters types:

  • Static program execution parameters requires application restart to apply new values.
  • Dynamic program execution parameters does not require application restart, but getting values requires syncronization.
  • User settings are the same as Dynamic

A parameter can be declared in several places and simultaneously:

  • hardcoded default values (1)
  • config file (2)
  • environment variable (3)
  • command line (4)

Values of parameters captured on higher step overwrites previous values.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// NonBindedFloat is returned by method Val
	// if Float is not initialized yet.
	NonBindedFloat float64 = 0.0

	// DefaultFmtStyle used in String method.
	DefaultFmtStyle = "%f"
)
View Source
var ErrDifferentKind = errors.New("different value kind")

ErrDifferentKind indicates raises when Set trying overwrite value with different AKind.

View Source
var ErrInvalidBool = errors.New("invalid value. Expected true or false")

ErrInvalidBool indicated failed parsing.

View Source
var NonBindedBool bool = false

NonBindedBool is returned by method Val if Bool is not initialized yet.

View Source
var NonBindedInt int = 0

NonBindedInt is returned by method Val if Int is not initialized yet.

View Source
var NonBindedString string = ""

NonBindedString is returned by method Val if String is not initialized yet.

Functions

func IsBool

func IsBool(s string) bool

IsBool tries to guess is string contains boolean or not.

Types

type AKind

type AKind uint8

AKind represents possible kinds of config param data type. The zero kind is not a valid kind.

const (
	// Unknown represents not specified kind (invalid).
	Unknown AKind = 0

	// AInt represents atomic int. It's int64 internally.
	AInt AKind = 1

	// ABool represents atomic bool. It's int32 internally.
	ABool AKind = 2

	// AString represents atomic string. It's pointers to string internally.
	AString AKind = 3

	// AFloat represents atomic float. It's float64 internally.
	AFloat AKind = 4
)

func (AKind) String

func (ak AKind) String() string

String implements Stringer interface.

type Bool

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

A Bool implements Valuer as atomic bool, implemented using int32 inside.

func NewBool

func NewBool() *Bool

NewBool returns atomic bool.

func (*Bool) Bind

func (a *Bool) Bind(b *Bool)

Bind binds current atomic variable to variable identified by to. As a result two Bool address to the same variable.

func (*Bool) IsBinded

func (a *Bool) IsBinded() bool

IsBinded returns true if Bool bineded to params container.

func (*Bool) Kind

func (a *Bool) Kind() AKind

Kind return ABool.

func (Bool) MarshalJSON

func (a Bool) MarshalJSON() ([]byte, error)

MarshalJSON implement Marshaller interface.

func (*Bool) Parse

func (a *Bool) Parse(s string) error

Parse converts input argument and assigns to value. Accepts Y,N, T,F, TRUE,FALSE, YES,NO, 1,0 in any register.

func (*Bool) Set

func (a *Bool) Set(b bool)

Set assigns value atomically.Initializes if was not before.

func (*Bool) String

func (a *Bool) String() string

String implements Stringer interface.

func (*Bool) UnmarshalJSON

func (a *Bool) UnmarshalJSON(buf []byte) error

UnmarshalJSON implement Unmarshaller interface.

func (*Bool) Val

func (a *Bool) Val() bool

Val returns value atomically. Returns NonBindedBool if it's not binded to params container.

type Config

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

Config is in-memory config params container. On init step Config accepts all param and values. Later all Valuers bind themselves to Config.

func (*Config) BindStruct

func (c *Config) BindStruct(structAddr interface{}) []error

BindStruct binds struct's attributes implementing interface Valuer to params from container. If param is not containerized yet it adds param into container.

Mapping between fields and params in container via tag "cfg". Example

type Listener struct {
		Port gonfig.Int `cfg:"port"`
}

BindStruct works properly with fields as structs and embedded anonymous structs.

func (*Config) BindVar

func (c *Config) BindVar(code string, addr Valuer) error

BindVar binds an Valuer to container. Creates param if not found.

func (*Config) Get

func (c *Config) Get(code string) (Valuer, bool)

Get returns Valuer instance by code.

func (*Config) IsExist

func (c *Config) IsExist(code string) bool

IsExist returns true if parameter identified by code is in container.

func (*Config) MustParam

func (c *Config) MustParam(code string, ak AKind) Valuer

MustParam returns Valuer identified by code. Creates if not exist. Panics if exists but Kind is different.

func (*Config) Param

func (c *Config) Param(code string, ak AKind) (Valuer, error)

Param returns Valuer identified by code. Creates if not exist. Returns error if exists but Kind is different.

func (*Config) Walk

func (c *Config) Walk(f func(code string, v Valuer, init, asked int))

Walk calls function f() for every parameter in container.

type ConfigSourcer

type ConfigSourcer interface {
	ApplyTo(g Configer, overwrite bool) error
}

ConfigSourcer is an interface wrapping a single method ApplyTo.

ApplyTo reads parameters from the source: database, file, env, etc. and adds them into config param container. Value overwrites if overwrite is true.

type Configer

type Configer interface {
	Param(code string, ak AKind) (Valuer, error)
	MustParam(code string, ak AKind) Valuer

	IsExist(code string) bool
	Get(code string) (Valuer, bool)

	BindStruct(structAddr interface{}) []error
	BindVar(code string, v Valuer) error
	Walk(func(code string, v Valuer, inited, asked int))
}

A Configer is an interface what wraps following methods:

Param returns Valuer identified by code. Creates if not exist. Returns error if exists but Kind is different.

MustParam returns Valuer identified by code. Creates if not exist. Panics if exists but Kind is different.

IsExist returns true if params identified by code is exists.

Get returns Valuer by code.

BindStruct walks through all struct fields and bind then if they implement Valuer interface.

BindVar binds a single var implementing Valuer interface.

func New

func New() Configer

New returns new container of config parameters. It's ok to have a single instance for the whole application.

type Float

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

A Float implements Valuer as atomic float64, implemented using uint64 inside.

func NewFloat

func NewFloat() *Float

NewFloat returns atomic float.

func (*Float) Bind

func (a *Float) Bind(to *Float)

Bind binds current atomic variable to variable identified by to. As a result two Float address to the same variable.

func (*Float) IsBinded

func (a *Float) IsBinded() bool

IsBinded returns true if Float bineded to params container.

func (*Float) Kind

func (a *Float) Kind() AKind

Kind returns AFloat.

func (Float) MarshalJSON

func (a Float) MarshalJSON() ([]byte, error)

MarshalJSON implement Marshaller interface.

func (*Float) Parse

func (a *Float) Parse(s string) error

Parse converts input argument and assigns to value.

func (*Float) Set

func (a *Float) Set(f float64)

Set assigns value atomically.Initializes if was not before.

func (*Float) String

func (a *Float) String() string

String implements Stringer interface. Returns value as string in decimal format. DefaultFmtStyle is used for styling.

func (*Float) UnmarshalJSON

func (a *Float) UnmarshalJSON(buf []byte) error

UnmarshalJSON implement Unmarshaller interface.

func (*Float) Val

func (a *Float) Val() float64

Val returns value atomically. Returns NonBindedFloat if it's not binded to params container.

type Int

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

A Int implements atomic int.

func NewInt

func NewInt() *Int

NewInt returns atomic int implemented using int64.

func (*Int) Bind

func (a *Int) Bind(to *Int)

Bind binds current atomic variable to variable identified by to. As a result two Int address to the same variable.

func (*Int) IsBinded

func (a *Int) IsBinded() bool

IsBinded returns true if Int bineded to params container.

func (*Int) Kind

func (a *Int) Kind() AKind

Kind returns AInt.

func (Int) MarshalJSON

func (a Int) MarshalJSON() ([]byte, error)

MarshalJSON implement Marshaller interface.

func (*Int) Parse

func (a *Int) Parse(s string) error

Parse converts input argument and assigns to value.

func (*Int) Set

func (a *Int) Set(i int)

Set assigns value atomically. Initializes if was not before.

func (*Int) String

func (a *Int) String() string

String implements Stringer interface.

func (*Int) UnmarshalJSON

func (a *Int) UnmarshalJSON(buf []byte) error

UnmarshalJSON implement Unmarshaller interface.

func (*Int) Val

func (a *Int) Val() int

Val returns value atomically. Returns NonBindedInt if it's not binded to params container.

type String

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

String implements atomic string.

func NewString

func NewString() *String

NewString returns atomic string implemented as atomic ptr.

func (*String) Bind

func (a *String) Bind(i *String)

Bind binds current atomic variable to variable identified by to. As a result two String address to the same variable.

func (*String) IsBinded

func (a *String) IsBinded() bool

IsBinded returns true if String bineded to params container.

func (*String) Kind

func (a *String) Kind() AKind

Kind returns AString.

func (String) MarshalJSON

func (a String) MarshalJSON() ([]byte, error)

MarshalJSON implement Marshaller interface.

func (*String) Parse

func (a *String) Parse(s string) error

Parse implements Valuer interface. Calls Set.

func (*String) Set

func (a *String) Set(s string)

Set assigns value atomically. Initializes if was not before.

func (*String) String

func (a *String) String() string

String implements Stringer interface.

func (*String) UnmarshalJSON

func (a *String) UnmarshalJSON(buf []byte) error

UnmarshalJSON implement Unmarshaller interface.

func (*String) Val

func (a *String) Val() string

Val returns value atomically. Returns NonBindedString if it's not binded to params container.

type Valuer

type Valuer interface {
	Kind() AKind
	Parse(string) error
	IsBinded() bool
}

Valuer is an interface what wraps following methods.

Kind returns data type of Valuer.

Parse converts string value to internal representation. in accordance with Kind.

IsBinded returns false is Valuer is not binded to Configer.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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