Documentation ¶
Index ¶
- Constants
- Variables
- func AddDriver(driver Driver)
- func Bool(key string, defVal ...bool) bool
- func ClearAll()
- func Data() map[string]interface{}
- func DumpTo(out io.Writer, format string) (int64, error)
- func EnableCache(opts *Options)
- func Exists(key string, findByPath ...bool) bool
- func Float(key string, defVal ...float64) float64
- func Get(key string, findByPath ...bool) interface{}
- func GetEnv(name string, defVal ...string) (val string)
- func GetValue(key string, findByPath ...bool) (interface{}, bool)
- 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 LoadData(dataSource ...interface{}) error
- func LoadDir(dir, suffix string) error
- func LoadExists(sourceFiles ...string) error
- func LoadFiles(sourceFiles ...string) error
- func LoadFlags(keys []string) error
- func LoadOSEnv(keys []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 MapStruct(key string, v interface{}) error
- func ParseEnv(opts *Options)
- func Readonly(opts *Options)
- func Set(key string, val interface{}, setByPath ...bool) error
- func SetDecoder(format string, decoder Decoder)
- func SetEncoder(format string, encoder Encoder)
- func String(key string, defVal ...string) string
- func StringMap(key string) map[string]string
- func Strings(key string) []string
- func StripJSONComments(src string) string
- func Uint(key string, defVal ...uint) uint
- func WithOptions(opts ...func(*Options))
- func WriteTo(out io.Writer) (int64, error)
- type Config
- func (c *Config) AddDriver(driver Driver)
- 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]interface{}
- func (c *Config) DelDriver(format string)
- func (c *Config) DumpTo(out io.Writer, format string) (n int64, err error)
- 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) interface{}
- func (c *Config) GetValue(key string, findByPath ...bool) (value interface{}, 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) LoadData(dataSources ...interface{}) (err error)
- func (c *Config) LoadDir(dir, suffix string) (err error)
- func (c *Config) LoadExists(sourceFiles ...string) (err error)
- func (c *Config) LoadFiles(sourceFiles ...string) (err error)
- func (c *Config) LoadFlags(keys []string) (err error)
- func (c *Config) LoadOSEnv(keys []string)
- func (c *Config) LoadRemote(format, url string) (err error)
- 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) MapStruct(key string, v interface{}) (err error)
- func (c *Config) Name() string
- func (c *Config) Options() *Options
- func (c *Config) Readonly()
- func (c *Config) Set(key string, val interface{}, setByPath ...bool) (err error)
- func (c *Config) SetDecoder(format string, decoder Decoder)
- func (c *Config) SetDecoders(decoders map[string]Decoder)
- func (c *Config) SetEncoder(format string, encoder Encoder)
- func (c *Config) SetEncoders(encoders map[string]Encoder)
- 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) Structure(key string, v interface{}) (err error)
- func (c *Config) ToJSON() string
- func (c *Config) Uint(key string, defVal ...uint) (value uint)
- func (c *Config) WithOptions(opts ...func(*Options))
- func (c *Config) WriteTo(out io.Writer) (n int64, err error)
- type Decoder
- type Driver
- type Encoder
- type Options
Constants ¶
const ( Ini = "ini" Hcl = "hcl" Yml = "yml" JSON = "json" Yaml = "yaml" Toml = "toml" )
There are supported config format
Variables ¶
var JSONDriver = &jsonDriver{name: JSON}
JSONDriver instance fot json
Functions ¶
func AddDriver ¶
func AddDriver(driver Driver)
AddDriver set a decoder and encoder driver for a format.
func Get ¶
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 LoadExists ¶
LoadExists load one or multi files, will ignore not exist
func LoadRemote ¶
LoadRemote load config data from remote URL.
func LoadSources ¶
LoadSources load one or multi byte data
func LoadStrings ¶
LoadStrings load one or multi string
func SetDecoder ¶
SetDecoder add/set a format decoder
func SetEncoder ¶
SetEncoder set a encoder for the format
func StripJSONComments ¶
StripJSONComments for a JSON string
Types ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config structure definition
func NewWithOptions ¶
NewWithOptions config instance
func (*Config) Bool ¶
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) LoadExists ¶
LoadExists load and parse config files, but will ignore not exists file.
func (*Config) LoadFlags ¶
LoadFlags parse command line arguments, based on provide keys. Usage:
// debug flag is bool type c.LoadFlags([]string{"env", "debug:bool"})
func (*Config) LoadRemote ¶
LoadRemote load config data from remote URL. Usage:
c.LoadRemote(config.JSON, "http://abc.com/api-config.json")
func (*Config) LoadSources ¶
LoadSources load data from byte content. Usage:
config.LoadSources(config.Yml, []byte(` name: blog arr: key: val
`))
func (*Config) LoadStrings ¶
LoadStrings load data from source string content.
func (*Config) LoadedFiles ¶
LoadedFiles get loaded files name
func (*Config) Readonly ¶
func (c *Config) Readonly()
Readonly disable set data to config. Usage:
config.LoadFiles(a, b, c) config.Readonly()
func (*Config) SetDecoder ¶
SetDecoder set decoder
func (*Config) SetDecoders ¶
SetDecoders set decoders
func (*Config) SetEncoder ¶
SetEncoder set a encoder for the format
func (*Config) SetEncoders ¶
SetEncoders set encoders
func (*Config) Structure ¶
Structure get config data and map to a structure. usage:
dbInfo := Db{} config.Structure("db", &dbInfo)
func (*Config) WithOptions ¶
WithOptions apply some options
type Options ¶
type Options struct { // parse env value. like: "${EnvName}" "${EnvName|default}" ParseEnv bool // config is readonly Readonly bool // enable config data cache EnableCache bool // default write format DumpFormat string // default input format ReadFormat string }
Options config options
Directories ¶
Path | Synopsis |
---|---|
向os ENV提供加载.env数据
|
向os ENV提供加载.env数据 |
parser
Package parser is a Parser for parse INI format content to golang data There are example data: # comments name = inhere age = 28 debug = true hasQuota1 = 'this is val' hasQuota2 = "this is val1" shell = ${SHELL} noEnv = ${NotExist|defValue} ; array in def section tags[] = a tags[] = b tags[] = c ; comments [sec1] key = val0 some = value stuff = things ; array in section types[] = x types[] = y how to use, please see examples:
|
Package parser is a Parser for parse INI format content to golang data There are example data: # comments name = inhere age = 28 debug = true hasQuota1 = 'this is val' hasQuota2 = "this is val1" shell = ${SHELL} noEnv = ${NotExist|defValue} ; array in def section tags[] = a tags[] = b tags[] = c ; comments [sec1] key = val0 some = value stuff = things ; array in section types[] = x types[] = y how to use, please see examples: |
Package json use the https://github.com/json-iterator/go for parse json
|
Package json use the https://github.com/json-iterator/go for parse json |
Package toml is driver use TOML format content as config source Usage please see example:
|
Package toml is driver use TOML format content as config source Usage please see example: |
Package yaml is a driver use YAML format content as config source Usage please see example:
|
Package yaml is a driver use YAML format content as config source Usage please see example: |