Documentation
¶
Index ¶
- Constants
- Variables
- func AddAlias(format, alias string)
- func AddDriver(driver Driver)
- func BindStruct(key string, dst any) error
- func Bool(key string, defVal ...bool) bool
- func ClearAll()
- func Data() map[string]any
- func Decode(dst any) error
- func Delimiter(sep byte) func(*Options)
- func DumpTo(out io.Writer, format string) (int64, error)
- func Duration(key string, defVal ...time.Duration) time.Duration
- func EnableCache(opts *Options)
- func Exists(key string, findByPath ...bool) bool
- func Float(key string, defVal ...float64) float64
- func Get(key string, findByPath ...bool) any
- func GetEnv(name string, defVal ...string) (val string)
- func GetValue(key string, findByPath ...bool) (any, bool)
- func Getenv(name string, defVal ...string) (val string)
- func Int(key string, defVal ...int) int
- func Int64(key string, defVal ...int64) int64
- func IntMap(key string) map[string]int
- func Ints(key string) []int
- func Keys() []string
- func LoadData(dataSource ...any) error
- func LoadExists(sourceFiles ...string) error
- func LoadExistsByFormat(format string, configFiles ...string) error
- func LoadFiles(sourceFiles ...string) error
- func LoadFilesByFormat(format string, configFiles ...string) error
- func LoadFlags(keys []string) error
- func LoadFromDir(dirPath, format string, loFns ...LoadOptFn) error
- func LoadOSEnvs(nameToKeyMap map[string]string)
- func LoadRemote(format, url string) error
- func LoadSources(format string, src []byte, more ...[]byte) error
- func LoadStrings(format string, str string, more ...string) error
- func MapOnExists(key string, dst any) error
- func MapStruct(key string, dst any) error
- func MustString(key string) string
- func ParseDefault(opts *Options)
- func ParseEnv(opts *Options)
- func ParseTime(opts *Options)
- func Readonly(opts *Options)
- func ReloadFiles() error
- func Reset()
- func SaveFileOnSet(fileName string, format string) func(options *Options)
- func Set(key string, val any, setByPath ...bool) error
- func SetData(data map[string]any)
- func String(key string, defVal ...string) string
- func StringMap(key string) map[string]string
- func Strings(key string) []string
- func StringsBySplit(key, sep string) []string
- func Sub(key string) map[string]any
- func SubDataMap(key string) maputil.Map
- func Uint(key string, defVal ...uint) uint
- func ValDecodeHookFunc(parseEnv, parseTime bool) mapstructure.DecodeHookFunc
- func WithDriver(drivers ...Driver)
- func WithHookFunc(fn HookFunc) func(*Options)
- func WithOptions(opts ...OptionFn)
- func WithTagName(tagName string) func(*Options)
- func WriteTo(out io.Writer) (int64, error)
- type Config
- func (c *Config) AddAlias(format, alias string)
- func (c *Config) AddDriver(driver Driver)
- func (c *Config) AliasMap() map[string]string
- func (c *Config) BindStruct(key string, dst any) error
- func (c *Config) Bool(key string, defVal ...bool) (value bool)
- func (c *Config) ClearAll()
- func (c *Config) ClearCaches()
- func (c *Config) ClearData()
- func (c *Config) Data() map[string]any
- func (c *Config) Decode(dst any) error
- func (c *Config) DelDriver(format string)
- func (c *Config) DriverNames() []string
- func (c *Config) DumpTo(out io.Writer, format string) (n int64, err error)
- func (c *Config) DumpToFile(fileName string, format string) (err error)
- func (c *Config) Duration(key string, defVal ...time.Duration) time.Duration
- func (c *Config) Error() error
- func (c *Config) Exists(key string, findByPath ...bool) (ok bool)
- func (c *Config) Float(key string, defVal ...float64) (value float64)
- func (c *Config) Get(key string, findByPath ...bool) any
- func (c *Config) GetValue(key string, findByPath ...bool) (value any, ok bool)
- func (c *Config) HasDecoder(format string) bool
- func (c *Config) HasEncoder(format string) bool
- func (c *Config) Int(key string, defVal ...int) (value int)
- func (c *Config) Int64(key string, defVal ...int64) (value int64)
- func (c *Config) IntMap(key string) (mp map[string]int)
- func (c *Config) Ints(key string) (arr []int)
- func (c *Config) IsEmpty() bool
- func (c *Config) Keys() []string
- func (c *Config) LoadData(dataSources ...any) (err error)
- func (c *Config) LoadExists(sourceFiles ...string) (err error)
- func (c *Config) LoadExistsByFormat(format string, configFiles ...string) (err error)
- func (c *Config) LoadFiles(sourceFiles ...string) (err error)
- func (c *Config) LoadFilesByFormat(format string, configFiles ...string) (err error)
- func (c *Config) LoadFlags(keys []string) (err error)
- func (c *Config) LoadFromDir(dirPath, format string, loFns ...LoadOptFn) (err error)
- func (c *Config) LoadOSEnvs(nameToKeyMap map[string]string)
- func (c *Config) LoadRemote(format, url string) (err error)
- func (c *Config) LoadSMap(smp map[string]string)
- func (c *Config) LoadSources(format string, src []byte, more ...[]byte) (err error)
- func (c *Config) LoadStrings(format string, str string, more ...string) (err error)
- func (c *Config) LoadedFiles() []string
- func (c *Config) LoadedUrls() []string
- func (c *Config) MapOnExists(key string, dst any) error
- func (c *Config) MapStruct(key string, dst any) error
- func (c *Config) MustString(key string) string
- func (c *Config) Name() string
- func (c *Config) Options() *Options
- func (c *Config) Readonly()
- func (c *Config) ReloadFiles() (err error)
- func (c *Config) Set(key string, val any, setByPath ...bool) (err error)
- func (c *Config) SetData(data map[string]any)
- func (c *Config) String(key string, defVal ...string) string
- func (c *Config) StringMap(key string) (mp map[string]string)
- func (c *Config) Strings(key string) (arr []string)
- func (c *Config) StringsBySplit(key, sep string) (ss []string)
- func (c *Config) Structure(key string, dst any) (err error)
- func (c *Config) Sub(key string) map[string]any
- func (c *Config) SubDataMap(key string) maputil.Map
- func (c *Config) ToJSON() string
- func (c *Config) Uint(key string, defVal ...uint) (value uint)
- func (c *Config) With(fn func(c *Config)) *Config
- func (c *Config) WithDriver(drivers ...Driver) *Config
- func (c *Config) WithOptions(opts ...OptionFn) *Config
- func (c *Config) WriteTo(out io.Writer) (n int64, err error)
- type Driver
- type HookFunc
- type LoadOptFn
- type LoadOptions
- type OptionFn
- type Options
- type Serializer
- type StdDriver
- func (d *StdDriver) Aliases() []string
- func (d *StdDriver) Decode(blob []byte, v any) (err error)
- func (d *StdDriver) Encode(v any) ([]byte, error)
- func (d *StdDriver) GetSerializer() Serializer
- func (d *StdDriver) Name() string
- func (d *StdDriver) WithAlias(alias string) *StdDriver
- func (d *StdDriver) WithAliases(aliases ...string) *StdDriver
Constants ¶
const ( JSON = "json" Yaml = "yaml" )
There are supported config format
const ( OnSetValue = "set.value" OnSetData = "set.data" OnLoadData = "load.data" OnReloadData = "reload.data" OnCleanData = "clean.data" )
there are some event names for config data changed.
Variables ¶
var ( ErrReadonly = errors.New("the config instance in 'readonly' mode") ErrKeyIsEmpty = errors.New("the config key is cannot be empty") ErrNotFound = errors.New("this key does not exist in the config") )
some common errors definitions
var YamlDriver = NewDriver("yaml", &yamlSerializer{}).WithAliases("yml")
YamlDriver instance fot yaml
Functions ¶
func AddAlias ¶ added in v0.2.0
func AddAlias(format, alias string)
AddAlias add alias for a format(driver name)
func AddDriver ¶ added in v0.2.0
func AddDriver(driver Driver)
AddDriver set a decoder and encoder driver for a format.
func BindStruct ¶ added in v0.2.0
BindStruct alias method of the 'Structure'
func Decode ¶ added in v0.2.0
Decode all config data to the dst ptr
Usage:
myConf := &MyConf{}
config.Decode(myConf)
func Duration ¶ added in v0.2.0
Duration get a time.Duration type value. if not found return default value
func Get ¶ added in v0.2.0
Get config value by key string, support get sub-value by key path(eg. 'map.key'),
- ok is true, find value from config
- ok is false, not found or error
func Getenv ¶ added in v0.2.0
Getenv get os ENV value by name. like os.Getenv, but support default value
Notice: - Key is not case-sensitive when getting
func LoadExists ¶ added in v0.2.0
LoadExists load one or multi files, will ignore not exist
Usage:
config.LoadExists(file1, file2, ...)
func LoadExistsByFormat ¶ added in v0.2.0
LoadExistsByFormat load one or multi files by give format, will fire OnLoadData event
func LoadFiles ¶ added in v0.2.0
LoadFiles load one or multi files, will fire OnLoadData event
Usage:
config.LoadFiles(file1, file2, ...)
func LoadFilesByFormat ¶ added in v0.2.0
LoadFilesByFormat load one or multi config files by give format, will fire OnLoadData event
func LoadFromDir ¶ added in v0.2.0
LoadFromDir Load custom format files from the given directory, the file name will be used as the key.
Example:
// file: /somedir/task.json
LoadFromDir("/somedir", "json")
// after load
Config.data = map[string]any{"task": file data}
func LoadOSEnvs ¶ added in v0.2.0
LoadOSEnvs load data from OS ENVs. format: {ENV_NAME: config_key}
func LoadRemote ¶ added in v0.2.0
LoadRemote load config data from remote URL.
func LoadSources ¶ added in v0.2.0
LoadSources load one or multi byte data
func LoadStrings ¶ added in v0.2.0
LoadStrings load one or multi string
func MapOnExists ¶ added in v0.2.0
MapOnExists mapping data to the dst structure only on key exists.
func MapStruct ¶ added in v0.2.0
MapStruct alias method of the 'Structure'
Usage:
dbInfo := &Db{}
config.MapStruct("db", dbInfo)
func MustString ¶ added in v0.2.0
MustString get a string by key, will panic on empty or not exists
func ParseDefault ¶ added in v0.2.0
func ParseDefault(opts *Options)
ParseDefault tag value on binding data to struct.
func ReloadFiles ¶ added in v0.2.0
func ReloadFiles() error
ReloadFiles reload config data use loaded files
func SaveFileOnSet ¶ added in v0.2.0
SaveFileOnSet set hook func, will panic on save error
func StringsBySplit ¶ added in v0.2.0
StringsBySplit get []string by split a string value.
func SubDataMap ¶ added in v0.2.0
SubDataMap get sub config data as maputil.Map
func ValDecodeHookFunc ¶ added in v0.2.0
func ValDecodeHookFunc(parseEnv, parseTime bool) mapstructure.DecodeHookFunc
ValDecodeHookFunc returns a mapstructure.DecodeHookFunc that parse ENV var, and more custom parse
func WithDriver ¶ added in v0.2.0
func WithDriver(drivers ...Driver)
WithDriver set multi drivers at once.
func WithHookFunc ¶ added in v0.2.0
WithHookFunc set hook func
func WithTagName ¶ added in v0.2.0
WithTagName set tag name for export to struct
Types ¶
type Config ¶ added in v0.2.0
type Config struct {
// contains filtered or unexported fields
}
Config structure definition
func NewWithOptions ¶ added in v0.2.0
NewWithOptions config instance. alias of New()
func (*Config) AddAlias ¶ added in v0.2.0
AddAlias add alias for a format(driver name)
Example:
config.AddAlias("ini", "conf")
func (*Config) BindStruct ¶ added in v0.2.0
BindStruct alias method of the 'Structure'
func (*Config) Bool ¶ added in v0.2.0
Bool looks up a value for a key in this section and attempts to parse that value as a boolean, along with a boolean result similar to a map lookup.
of following(case insensitive):
- true
- yes
- false
- no
- 1
- 0
The `ok` boolean will be false in the event that the value could not be parsed as a bool
func (*Config) ClearCaches ¶ added in v0.2.0
func (c *Config) ClearCaches()
ClearCaches clear caches
func (*Config) Data ¶ added in v0.2.0
Data get all config data.
Note: will don't apply any options, like ParseEnv
func (*Config) Decode ¶ added in v0.2.0
Decode all config data to the dst ptr.
It's equals:
c.Structure("", dst)
func (*Config) DriverNames ¶ added in v0.2.0
DriverNames get loaded driver names
func (*Config) DumpTo ¶ added in v0.2.0
DumpTo use the format(json,yaml,toml) dump config data to a writer
func (*Config) DumpToFile ¶ added in v0.2.0
DumpToFile use the format(json,yaml,toml) dump config data to a writer
func (*Config) Duration ¶ added in v0.2.0
Duration get a time.Duration type value. if not found return default value
func (*Config) HasDecoder ¶ added in v0.2.0
HasDecoder has decoder
func (*Config) HasEncoder ¶ added in v0.2.0
HasEncoder has encoder
func (*Config) LoadData ¶ added in v0.2.0
LoadData load data from map OR struct
The dataSources type allow:
- map[string]any
- map[string]string
func (*Config) LoadExists ¶ added in v0.2.0
LoadExists load and parse config files, but will ignore not exists file.
func (*Config) LoadExistsByFormat ¶ added in v0.2.0
LoadExistsByFormat load one or multi files by give format, will fire OnLoadData event
func (*Config) LoadFiles ¶ added in v0.2.0
LoadFiles load and parse config files, will fire OnLoadData event
func (*Config) LoadFilesByFormat ¶ added in v0.2.0
LoadFilesByFormat load one or multi files by give format, will fire OnLoadData event
func (*Config) LoadFlags ¶ added in v0.2.0
LoadFlags parse command line arguments, based on provide keys.
Usage:
// 'debug' flag is bool type
c.LoadFlags([]string{"env", "debug:bool"})
func (*Config) LoadFromDir ¶ added in v0.2.0
LoadFromDir Load custom format files from the given directory, the file name will be used as the key.
NOTE: will not be reloaded on call ReloadFiles(), if data loaded by the method.
Example:
// file: /somedir/task.json , will use filename 'task' as key
Config.LoadFromDir("/somedir", "json")
// after load, the data will be:
Config.data = map[string]any{"task": {file data}}
func (*Config) LoadOSEnvs ¶ added in v0.2.0
LoadOSEnvs load data from os ENVs. format: {ENV_NAME: config_key}
func (*Config) LoadRemote ¶ added in v0.2.0
LoadRemote load config data from remote URL.
Usage:
c.LoadRemote(config.JSON, "http://abc.com/api-config.json")
func (*Config) LoadSources ¶ added in v0.2.0
LoadSources load data from byte content.
Usage:
config.LoadSources(config.Yaml, []byte(` name: blog arr: key: val
`))
func (*Config) LoadStrings ¶ added in v0.2.0
LoadStrings load data from source string content.
func (*Config) LoadedFiles ¶ added in v0.2.0
LoadedFiles get loaded files name
func (*Config) LoadedUrls ¶ added in v0.2.0
LoadedUrls get loaded urls list
func (*Config) MapOnExists ¶ added in v0.2.0
MapOnExists mapping data to the dst structure only on key exists.
- Support ParseEnv on mapping
- Support ParseDefault on mapping
func (*Config) MustString ¶ added in v0.2.0
MustString get a string by key, will panic on empty or not exists
func (*Config) Readonly ¶ added in v0.2.0
func (c *Config) Readonly()
Readonly disable set data to config.
Usage:
config.LoadFiles(a, b, c) config.Readonly()
func (*Config) ReloadFiles ¶ added in v0.2.0
ReloadFiles reload config data use loaded files. use on watching loaded files change
func (*Config) String ¶ added in v0.2.0
String get a string by key, if not found return default value
func (*Config) StringsBySplit ¶ added in v0.2.0
StringsBySplit get []string by split a string value.
func (*Config) Structure ¶ added in v0.2.0
Structure get config data and binding to the dst structure.
- Support ParseEnv on mapping
- Support ParseDefault on mapping
Usage:
dbInfo := Db{}
config.Structure("db", &dbInfo)
func (*Config) Sub ¶ added in v0.2.0
Sub get sub config data by key
Note: will don't apply any options, like ParseEnv
func (*Config) SubDataMap ¶ added in v0.2.0
SubDataMap get sub config data as maputil.Map
TIP: will not enable parse Env and more
func (*Config) WithDriver ¶ added in v0.2.0
WithDriver set multi drivers at once.
func (*Config) WithOptions ¶ added in v0.2.0
WithOptions apply some options
type Driver ¶ added in v0.2.0
type Driver interface {
Name() string
Aliases() []string // alias format names, use for resolve format name
Serializer
GetSerializer() Serializer
}
Driver interface.
type LoadOptions ¶ added in v0.2.0
type LoadOptions struct {
// DataKey use for load config from dir.
// see https://github.com/gookit/config/issues/173
DataKey string
}
LoadOptions for load config from dir.
type Options ¶ added in v0.2.0
type Options struct {
// ParseEnv parse env in string value and default value. like: "${EnvName}" "${EnvName|default}"
ParseEnv bool
// ParseTime parses a duration string to time.Duration
// eg: 10s, 2m
ParseTime bool
// Readonly config is readonly
Readonly bool
// ParseDefault tag on binding data to struct. tag: default
ParseDefault bool
// EnableCache enable config data cache
EnableCache bool
// ParseKey parse key path, allow find value by key path. eg: 'key.sub' will find `map[key]sub`
ParseKey bool
// Delimiter the delimiter char for split key path, if `FindByPath=true`. default is '.'
Delimiter byte
// DumpFormat default write format
DumpFormat string
// ReadFormat default input format
ReadFormat string
// DecoderConfig setting for binding data to struct. such as: TagName
DecoderConfig *mapstructure.DecoderConfig
// HookFunc on data changed. you can do something...
HookFunc HookFunc
// MergeOptions settings for merge two data
MergeOptions []func(*mergo.Config)
}
Options config options
func (*Options) SetTagName ¶ added in v0.2.0
SetTagName for mapping data to struct
type Serializer ¶ added in v0.2.0
type StdDriver ¶ added in v0.2.0
type StdDriver struct {
// contains filtered or unexported fields
}
StdDriver struct
func NewDriver ¶ added in v0.2.0
func NewDriver(name string, serializer Serializer) *StdDriver
NewDriver new std driver instance.
func (*StdDriver) GetSerializer ¶ added in v0.2.0
func (d *StdDriver) GetSerializer() Serializer
func (*StdDriver) WithAliases ¶ added in v0.2.0
WithAliases set aliases for driver