Documentation ¶
Overview ¶
Package conf reads configuration from many format file, such as Java properties, yaml, toml, etc.
Index ¶
- func BindValue(p *Properties, v reflect.Value, t reflect.Type, param BindParam, filter Filter) error
- func Flatten(m map[string]interface{}) map[string]string
- func Register(name string, v Validator)
- func RegisterConverter(fn utils.Converter)
- func RegisterReader(r Reader, ext ...string)
- func RegisterSplitter(name string, fn Splitter)
- func RemoveSplitter(name string)
- func Validate(tag reflect.StructTag, i interface{}) error
- type BindArg
- type BindParam
- type Filter
- type GetOption
- type ParsedTag
- type Properties
- func (p *Properties) Bind(i interface{}, args ...BindArg) error
- func (p *Properties) Bytes(b []byte, ext string) error
- func (p *Properties) Copy() *Properties
- func (p *Properties) Get(key string, opts ...GetOption) string
- func (p *Properties) Has(key string) bool
- func (p *Properties) Keys() []string
- func (p *Properties) Load(file string) error
- func (p *Properties) Merge(m map[string]interface{}) error
- func (p *Properties) Read(r io.Reader, ext string) error
- func (p *Properties) Resolve(s string) (string, error)
- func (p *Properties) Set(key string, val interface{}) error
- type Reader
- type Splitter
- type Validator
- type Value
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BindValue ¶
func BindValue(p *Properties, v reflect.Value, t reflect.Type, param BindParam, filter Filter) error
BindValue binds properties to a value.
func RegisterConverter ¶
RegisterConverter registers its converter for non-primitive type such as time.Time, time.Duration, or other user-defined value type.
func RegisterReader ¶
RegisterReader registers its Reader for some kind of file extension.
func RegisterSplitter ¶
RegisterSplitter registers a Splitter and named it.
func RemoveSplitter ¶
func RemoveSplitter(name string)
RemoveSplitter removes a Splitter by its name, only for unit testing.
Types ¶
type BindParam ¶
type ParsedTag ¶
type ParsedTag struct { Key string // short property key Def string // default value HasDef bool // has default value Splitter string // splitter's name }
ParsedTag a value tag includes at most three parts: required key, optional default value, and optional splitter, the syntax is ${key:=value}||splitter.
type Properties ¶
type Properties struct {
// contains filtered or unexported fields
}
Properties stores the data with map[string]string and the keys are case-sensitive, you can get one of them by its key, or bind some of them to a value. There are too many formats of configuration files, and too many conflicts between them. Each format of configuration file provides its special characteristics, but usually they are not all necessary, and complementary. For example, `conf` disabled Java properties' expansion when reading file, but also provides similar function when getting or binding properties. A good rule of thumb is that treating application configuration as a tree, but not all formats of configuration files designed as a tree or not ideal, for instance Java properties isn't strictly verified. Although configuration can store as a tree, but it costs more CPU time when getting properties because it reads property node by node. So `conf` uses a tree to strictly verify and a flat map to store.
func Bytes ¶
func Bytes(b []byte, ext string) (*Properties, error)
Bytes creates *Properties from []byte, ext is the file name extension.
func Read ¶
func Read(r io.Reader, ext string) (*Properties, error)
Read creates *Properties from io.Reader, ext is the file name extension.
func (*Properties) Bind ¶
func (p *Properties) Bind(i interface{}, args ...BindArg) error
Bind binds properties to a value, the bind value can be primitive type, map, slice, struct. When binding to struct, the tag 'value' indicates which properties should be bind. The 'value' tags are defined by value:"${a:=b|splitter}", 'a' is the key, 'b' is the default value, 'splitter' is the Splitter's name when you want split string value into []string value.
func (*Properties) Bytes ¶
func (p *Properties) Bytes(b []byte, ext string) error
Bytes loads properties from []byte, ext is the file name extension.
func (*Properties) Copy ¶
func (p *Properties) Copy() *Properties
func (*Properties) Get ¶
func (p *Properties) Get(key string, opts ...GetOption) string
Get returns key's value, using Def to return a default value.
func (*Properties) Load ¶
func (p *Properties) Load(file string) error
Load loads properties from file.
func (*Properties) Merge ¶
func (p *Properties) Merge(m map[string]interface{}) error
Merge flattens the map and sets all keys and values.
func (*Properties) Read ¶
func (p *Properties) Read(r io.Reader, ext string) error
Read creates *Properties from io.Reader, ext is the file name extension.
func (*Properties) Resolve ¶
func (p *Properties) Resolve(s string) (string, error)
Resolve resolves string value that contains references to other properties, the references are defined by ${key:=def}.
func (*Properties) Set ¶
func (p *Properties) Set(key string, val interface{}) error
Set sets key's value to be a primitive type as int or string, or a slice or map nested with primitive type elements. One thing you should know is Set actions as overlap but not replace, that means when you set a slice or a map, an existing path will remain when it doesn't exist in the slice or map even they share a same prefix path.
type Value ¶
type Value interface {
OnRefresh(p *Properties, param BindParam) error
}
A Value represents a refreshable type.