Documentation ¶
Index ¶
- Variables
- func IsBool(s string) bool
- type AKind
- type Bool
- type Config
- func (c *Config) BindStruct(structAddr interface{}) []error
- func (c *Config) BindVar(code string, addr Valuer) error
- func (c *Config) Get(code string) (Valuer, bool)
- func (c *Config) IsExist(code string) bool
- func (c *Config) MustParam(code string, ak AKind) Valuer
- func (c *Config) Param(code string, ak AKind) (Valuer, error)
- func (c *Config) Walk(f func(code string, v Valuer, init, asked int))
- type ConfigSourcer
- type Configer
- type Float
- func (a *Float) Bind(to *Float)
- func (a *Float) IsBinded() bool
- func (a *Float) Kind() AKind
- func (a Float) MarshalJSON() ([]byte, error)
- func (a *Float) Parse(s string) error
- func (a *Float) Set(f float64)
- func (a *Float) String() string
- func (a *Float) UnmarshalJSON(buf []byte) error
- func (a *Float) Val() float64
- type Int
- type String
- func (a *String) Bind(i *String)
- func (a *String) IsBinded() bool
- func (a *String) Kind() AKind
- func (a String) MarshalJSON() ([]byte, error)
- func (a *String) Parse(s string) error
- func (a *String) Set(s string)
- func (a *String) String() string
- func (a *String) UnmarshalJSON(buf []byte) error
- func (a *String) Val() string
- type Valuer
Constants ¶
This section is empty.
Variables ¶
var ( // NonBindedFloat is returned by method Val // if Float is not initialized yet. NonBindedFloat float64 = 0.0 // DefaultFmtStyle used in String method. DefaultFmtStyle = "%f" )
var ErrDifferentKind = errors.New("different value kind")
ErrDifferentKind indicates raises when Set trying overwrite value with different AKind.
var ErrInvalidBool = errors.New("invalid value. Expected true or false")
ErrInvalidBool indicated failed parsing.
var NonBindedBool bool = false
NonBindedBool is returned by method Val if Bool is not initialized yet.
var NonBindedInt int = 0
NonBindedInt is returned by method Val if Int is not initialized yet.
var NonBindedString string = ""
NonBindedString is returned by method Val if String is not initialized yet.
Functions ¶
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 )
type Bool ¶
type Bool struct {
// contains filtered or unexported fields
}
A Bool implements Valuer as atomic bool, implemented using int32 inside.
func (*Bool) Bind ¶
Bind binds current atomic variable to variable identified by to. As a result two Bool address to the same variable.
func (Bool) MarshalJSON ¶
MarshalJSON implement Marshaller interface.
func (*Bool) Parse ¶
Parse converts input argument and assigns to value. Accepts Y,N, T,F, TRUE,FALSE, YES,NO, 1,0 in any register.
func (*Bool) UnmarshalJSON ¶
UnmarshalJSON implement Unmarshaller interface.
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 ¶
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) MustParam ¶
MustParam returns Valuer identified by code. Creates if not exist. Panics if exists but Kind is different.
type ConfigSourcer ¶
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.
type Float ¶
type Float struct {
// contains filtered or unexported fields
}
A Float implements Valuer as atomic float64, implemented using uint64 inside.
func (*Float) Bind ¶
Bind binds current atomic variable to variable identified by to. As a result two Float address to the same variable.
func (Float) MarshalJSON ¶
MarshalJSON implement Marshaller interface.
func (*Float) String ¶
String implements Stringer interface. Returns value as string in decimal format. DefaultFmtStyle is used for styling.
func (*Float) UnmarshalJSON ¶
UnmarshalJSON implement Unmarshaller interface.
type Int ¶
type Int struct {
// contains filtered or unexported fields
}
A Int implements atomic int.
func (*Int) Bind ¶
Bind binds current atomic variable to variable identified by to. As a result two Int address to the same variable.
func (Int) MarshalJSON ¶
MarshalJSON implement Marshaller interface.
func (*Int) UnmarshalJSON ¶
UnmarshalJSON implement Unmarshaller interface.
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 ¶
Bind binds current atomic variable to variable identified by to. As a result two String address to the same variable.
func (String) MarshalJSON ¶
MarshalJSON implement Marshaller interface.
func (*String) UnmarshalJSON ¶
UnmarshalJSON implement Unmarshaller interface.