Documentation ¶
Overview ¶
Package gcfg provides reading, caching and managing for configuration.
Example (MapSliceChange) ¶
package main import ( "fmt" "github.com/gogf/gf/frame/g" "github.com/gogf/gf/internal/intlog" "github.com/gogf/gf/os/gcfg" ) func main() { intlog.SetEnabled(false) defer intlog.SetEnabled(true) // For testing/example only. content := `{"map":{"key":"value"}, "slice":[59,90]}` gcfg.SetContent(content) defer gcfg.RemoveContent() m := g.Cfg().GetMap("map") fmt.Println(m) // Change the key-value pair. m["key"] = "john" // It changes the underlying key-value pair. fmt.Println(g.Cfg().GetMap("map")) s := g.Cfg().GetArray("slice") fmt.Println(s) // Change the value of specified index. s[0] = 100 // It changes the underlying slice. fmt.Println(g.Cfg().GetArray("slice")) }
Output: map[key:value] map[key:john] [59 90] [100 90]
Index ¶
- Constants
- func ClearContent()
- func GetContent(file ...string) string
- func RemoveContent(file ...string)
- func SetContent(content string, file ...string)
- type Config
- func (c *Config) AddPath(path string) error
- func (c *Config) Available(file ...string) bool
- func (c *Config) Clear()
- func (c *Config) Contains(pattern string) bool
- func (c *Config) Dump()
- func (c *Config) FilePath(file ...string) (path string)
- func (c *Config) Get(pattern string, def ...interface{}) interface{}
- func (c *Config) GetArray(pattern string, def ...interface{}) []interface{}
- func (c *Config) GetBool(pattern string, def ...interface{}) bool
- func (c *Config) GetBytes(pattern string, def ...interface{}) []byte
- func (c *Config) GetDuration(pattern string, def ...interface{}) time.Duration
- func (c *Config) GetFileName() string
- func (c *Config) GetFloat32(pattern string, def ...interface{}) float32
- func (c *Config) GetFloat64(pattern string, def ...interface{}) float64
- func (c *Config) GetFloats(pattern string, def ...interface{}) []float64
- func (c *Config) GetGTime(pattern string, format ...string) *gtime.Time
- func (c *Config) GetInt(pattern string, def ...interface{}) int
- func (c *Config) GetInt16(pattern string, def ...interface{}) int16
- func (c *Config) GetInt32(pattern string, def ...interface{}) int32
- func (c *Config) GetInt64(pattern string, def ...interface{}) int64
- func (c *Config) GetInt8(pattern string, def ...interface{}) int8
- func (c *Config) GetInterfaces(pattern string, def ...interface{}) []interface{}
- func (c *Config) GetInts(pattern string, def ...interface{}) []int
- func (c *Config) GetJson(pattern string, def ...interface{}) *gjson.Json
- func (c *Config) GetJsonMap(pattern string, def ...interface{}) map[string]*gjson.Json
- func (c *Config) GetJsons(pattern string, def ...interface{}) []*gjson.Json
- func (c *Config) GetMap(pattern string, def ...interface{}) map[string]interface{}
- func (c *Config) GetMapStrStr(pattern string, def ...interface{}) map[string]string
- func (c *Config) GetMapToMap(pattern string, pointer interface{}, mapping ...map[string]string) error
- func (c *Config) GetMapToMapDeep(pattern string, pointer interface{}, mapping ...map[string]string) error
- func (c *Config) GetMapToMaps(pattern string, pointer interface{}, mapping ...map[string]string) error
- func (c *Config) GetMapToMapsDeep(pattern string, pointer interface{}, mapping ...map[string]string) error
- func (c *Config) GetString(pattern string, def ...interface{}) string
- func (c *Config) GetStrings(pattern string, def ...interface{}) []string
- func (c *Config) GetStruct(pattern string, pointer interface{}, mapping ...map[string]string) error
- func (c *Config) GetStructDeep(pattern string, pointer interface{}, mapping ...map[string]string) error
- func (c *Config) GetStructs(pattern string, pointer interface{}, mapping ...map[string]string) error
- func (c *Config) GetStructsDeep(pattern string, pointer interface{}, mapping ...map[string]string) error
- func (c *Config) GetTime(pattern string, format ...string) time.Time
- func (c *Config) GetUint(pattern string, def ...interface{}) uint
- func (c *Config) GetUint16(pattern string, def ...interface{}) uint16
- func (c *Config) GetUint32(pattern string, def ...interface{}) uint32
- func (c *Config) GetUint64(pattern string, def ...interface{}) uint64
- func (c *Config) GetUint8(pattern string, def ...interface{}) uint8
- func (c *Config) GetVar(pattern string, def ...interface{}) *gvar.Var
- func (c *Config) Set(pattern string, value interface{}) error
- func (c *Config) SetFileName(name string) *Config
- func (c *Config) SetPath(path string) error
- func (c *Config) SetViolenceCheck(check bool)
- func (c *Config) ToArray() []interface{}
- func (c *Config) ToMap() map[string]interface{}
- func (c *Config) ToMapToMap(pointer interface{}, mapping ...map[string]string) error
- func (c *Config) ToMapToMapDeep(pointer interface{}, mapping ...map[string]string) error
- func (c *Config) ToMapToMaps(pointer interface{}, mapping ...map[string]string) error
- func (c *Config) ToMapToMapsDeep(pointer interface{}, mapping ...map[string]string) error
- func (c *Config) ToStruct(pointer interface{}, mapping ...map[string]string) error
- func (c *Config) ToStructDeep(pointer interface{}, mapping ...map[string]string) error
- func (c *Config) ToStructs(pointer interface{}, mapping ...map[string]string) error
- func (c *Config) ToStructsDeep(pointer interface{}, mapping ...map[string]string) error
Examples ¶
Constants ¶
const (
DefaultConfigFile = "config.toml" // The default configuration file name.
)
const (
// Default group name for instance usage.
DefaultName = "config"
)
Variables ¶
This section is empty.
Functions ¶
func GetContent ¶
GetContent returns customized configuration content for specified <file>. The <file> is unnecessary param, default is DefaultConfigFile.
func RemoveContent ¶
func RemoveContent(file ...string)
RemoveContent removes the global configuration with specified <file>. If <name> is not passed, it removes configuration of the default group name.
func SetContent ¶
SetContent sets customized configuration content for specified <file>. The <file> is unnecessary param, default is DefaultConfigFile.
Types ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Configuration struct.
func Instance ¶
Instance returns an instance of Config with default settings. The parameter <name> is the name for the instance. But very note that, if the file "name.toml" exists in the configuration directory, it then sets it as the default configuration file. The toml file type is the default configuration file type.
func New ¶
New returns a new configuration management object. The parameter <file> specifies the default configuration file name for reading.
func (*Config) Available ¶
Available checks and returns whether configuration of given <file> is available.
func (*Config) Clear ¶
func (c *Config) Clear()
Clear removes all parsed configuration files content cache, which will force reload configuration content from file.
func (*Config) Dump ¶
func (c *Config) Dump()
Dump prints current Json object with more manually readable.
func (*Config) FilePath ¶
GetFilePath returns the absolute path of the specified configuration file. If <file> is not passed, it returns the configuration file path of the default name. If the specified configuration file does not exist, an empty string is returned.
func (*Config) Get ¶
Get retrieves and returns value by specified <pattern>. It returns all values of current Json object if <pattern> is given empty or string ".". It returns nil if no value found by <pattern>.
We can also access slice item by its index number in <pattern> like: "list.10", "array.0.name", "array.0.1.id".
It returns a default value specified by <def> if value for <pattern> is not found.
func (*Config) GetArray ¶
GetArray retrieves the value by specified <pattern>, and converts it to a slice of []interface{}.
func (*Config) GetBool ¶
GetBool retrieves the value by specified <pattern>, converts and returns it as bool. It returns false when value is: "", 0, false, off, nil; or returns true instead.
func (*Config) GetBytes ¶
GetBytes retrieves the value by specified <pattern> and converts it to []byte.
func (*Config) GetDuration ¶
GetDuration retrieves the value by specified <pattern> and converts it to time.Duration.
func (*Config) GetFileName ¶
GetFileName returns the default configuration file name.
func (*Config) GetFloat32 ¶
GetFloat32 retrieves the value by specified <pattern> and converts it to float32.
func (*Config) GetFloat64 ¶
GetFloat64 retrieves the value by specified <pattern> and converts it to float64.
func (*Config) GetFloats ¶
GetFloats retrieves the value by specified <pattern> and converts it to []float64.
func (*Config) GetGTime ¶
GetGTime retrieves the value by specified <pattern> and converts it to *gtime.Time.
func (*Config) GetInt16 ¶
GetInt16 retrieves the value by specified <pattern> and converts it to int16.
func (*Config) GetInt32 ¶
GetInt32 retrieves the value by specified <pattern> and converts it to int32.
func (*Config) GetInt64 ¶
GetInt64 retrieves the value by specified <pattern> and converts it to int64.
func (*Config) GetInt8 ¶
GetInt8 retrieves the value by specified <pattern> and converts it to int8.
func (*Config) GetInterfaces ¶
GetInterfaces is alias of GetArray. See GetArray.
func (*Config) GetInts ¶
GetInts retrieves the value by specified <pattern> and converts it to []int.
func (*Config) GetJson ¶
GetJson gets the value by specified <pattern>, and converts it to a un-concurrent-safe Json object.
func (*Config) GetJsonMap ¶
GetJsonMap gets the value by specified <pattern>, and converts it to a map of un-concurrent-safe Json object.
func (*Config) GetJsons ¶
GetJsons gets the value by specified <pattern>, and converts it to a slice of un-concurrent-safe Json object.
func (*Config) GetMap ¶
GetMap retrieves and returns the value by specified <pattern> as map[string]interface{}.
func (*Config) GetMapStrStr ¶
GetMapStrStr retrieves and returns the value by specified <pattern> as map[string]string.
func (*Config) GetMapToMap ¶
func (c *Config) GetMapToMap(pattern string, pointer interface{}, mapping ...map[string]string) error
GetMapToMap retrieves the value by specified <pattern> and converts it to specified map variable. See gconv.MapToMap.
func (*Config) GetMapToMapDeep ¶
func (c *Config) GetMapToMapDeep(pattern string, pointer interface{}, mapping ...map[string]string) error
GetMapToMapDeep retrieves the value by specified <pattern> and converts it to specified map variable recursively. See gconv.MapToMapDeep.
func (*Config) GetMapToMaps ¶
func (c *Config) GetMapToMaps(pattern string, pointer interface{}, mapping ...map[string]string) error
GetMapToMaps retrieves the value by specified <pattern> and converts it to specified map slice variable. See gconv.MapToMaps.
func (*Config) GetMapToMapsDeep ¶
func (c *Config) GetMapToMapsDeep(pattern string, pointer interface{}, mapping ...map[string]string) error
GetMapToMapsDeep retrieves the value by specified <pattern> and converts it to specified map slice variable recursively. See gconv.MapToMapsDeep.
func (*Config) GetString ¶
GetString retrieves the value by specified <pattern> and converts it to string.
func (*Config) GetStrings ¶
GetStrings retrieves the value by specified <pattern> and converts it to []string.
func (*Config) GetStruct ¶
GetStruct retrieves the value by specified <pattern> and converts it to specified object <pointer>. The <pointer> should be the pointer to an object.
func (*Config) GetStructDeep ¶
func (c *Config) GetStructDeep(pattern string, pointer interface{}, mapping ...map[string]string) error
GetStructDeep does GetStruct recursively. Deprecated, use GetStruct instead.
func (*Config) GetStructs ¶
func (c *Config) GetStructs(pattern string, pointer interface{}, mapping ...map[string]string) error
GetStructs converts any slice to given struct slice.
func (*Config) GetStructsDeep ¶
func (c *Config) GetStructsDeep(pattern string, pointer interface{}, mapping ...map[string]string) error
GetStructsDeep converts any slice to given struct slice recursively. Deprecated, use GetStructs instead.
func (*Config) GetTime ¶
GetTime retrieves the value by specified <pattern> and converts it to time.Time.
func (*Config) GetUint ¶
GetUint retrieves the value by specified <pattern> and converts it to uint.
func (*Config) GetUint16 ¶
GetUint16 retrieves the value by specified <pattern> and converts it to uint16.
func (*Config) GetUint32 ¶
GetUint32 retrieves the value by specified <pattern> and converts it to uint32.
func (*Config) GetUint64 ¶
GetUint64 retrieves the value by specified <pattern> and converts it to uint64.
func (*Config) GetUint8 ¶
GetUint8 retrieves the value by specified <pattern> and converts it to uint8.
func (*Config) Set ¶
Set sets value with specified <pattern>. It supports hierarchical data access by char separator, which is '.' in default. It is commonly used for updates certain configuration value in runtime.
func (*Config) SetFileName ¶
SetFileName sets the default configuration file name.
func (*Config) SetPath ¶
SetPath sets the configuration directory path for file search. The parameter <path> can be absolute or relative path, but absolute path is strongly recommended.
func (*Config) SetViolenceCheck ¶
SetViolenceCheck sets whether to perform hierarchical conflict checking. This feature needs to be enabled when there is a level symbol in the key name. It is off in default.
Note that, turning on this feature is quite expensive, and it is not recommended to allow separators in the key names. It is best to avoid this on the application side.
func (*Config) ToArray ¶
func (c *Config) ToArray() []interface{}
ToArray converts current Json object to []interface{}. It returns nil if fails.
func (*Config) ToMap ¶
ToMap converts current Json object to map[string]interface{}. It returns nil if fails.
func (*Config) ToMapToMap ¶
ToMapToMap converts current Json object to specified map variable. The parameter of <pointer> should be type of *map.
func (*Config) ToMapToMapDeep ¶
ToMapToMapDeep converts current Json object to specified map variable recursively. The parameter of <pointer> should be type of *map.
func (*Config) ToMapToMaps ¶
ToMapToMaps converts current Json object to specified map variable slice. The parameter of <pointer> should be type of []map/*map.
func (*Config) ToMapToMapsDeep ¶
ToMapToMapsDeep converts current Json object to specified map variable slice recursively. The parameter of <pointer> should be type of []map/*map.
func (*Config) ToStruct ¶
ToStruct converts current Json object to specified object. The <pointer> should be a pointer type of *struct.
func (*Config) ToStructDeep ¶
ToStructDeep converts current Json object to specified object recursively. The <pointer> should be a pointer type of *struct.