mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
add configuration updating feature for package gcfg
This commit is contained in:
@ -150,13 +150,13 @@ func (j *Json) GetString(pattern string, def ...interface{}) string {
|
||||
return gconv.String(j.Get(pattern, def...))
|
||||
}
|
||||
|
||||
// GetBytes retrieves the value by specified <pattern> and converts it to byte.
|
||||
// GetBytes retrieves the value by specified <pattern> and converts it to []byte.
|
||||
func (j *Json) GetBytes(pattern string, def ...interface{}) []byte {
|
||||
return gconv.Bytes(j.Get(pattern, def...))
|
||||
}
|
||||
|
||||
// GetBool gets the value by specified <pattern>,
|
||||
// and converts it to bool.
|
||||
// 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 (j *Json) GetBool(pattern string, def ...interface{}) bool {
|
||||
|
||||
2
go.mod
2
go.mod
@ -14,8 +14,8 @@ require (
|
||||
github.com/json-iterator/go v1.1.10
|
||||
github.com/mattn/go-runewidth v0.0.9 // indirect
|
||||
github.com/olekukonko/tablewriter v0.0.1
|
||||
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae // indirect
|
||||
golang.org/x/net v0.0.0-20200602114024-627f9648deb9
|
||||
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae // indirect
|
||||
golang.org/x/text v0.3.2
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
|
||||
)
|
||||
|
||||
@ -328,8 +328,10 @@ func (c *Config) getJson(file ...string) *gjson.Json {
|
||||
name = c.name
|
||||
}
|
||||
r := c.jsons.GetOrSetFuncLock(name, func() interface{} {
|
||||
content := ""
|
||||
filePath := ""
|
||||
var (
|
||||
content = ""
|
||||
filePath = ""
|
||||
)
|
||||
if content = GetContent(name); content == "" {
|
||||
filePath = c.filePath(name)
|
||||
if filePath == "" {
|
||||
@ -341,6 +343,7 @@ func (c *Config) getJson(file ...string) *gjson.Json {
|
||||
content = gfile.GetContents(filePath)
|
||||
}
|
||||
}
|
||||
// Note that the underlying configuration json object operations are concurrent safe.
|
||||
if j, err := gjson.LoadContent(content, true); err == nil {
|
||||
j.SetViolenceCheck(c.vc)
|
||||
// Add monitor for this configuration file,
|
||||
|
||||
@ -16,6 +16,24 @@ import (
|
||||
"github.com/gogf/gf/os/gtime"
|
||||
)
|
||||
|
||||
// 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 (c *Config) Set(pattern string, value interface{}) error {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.Set(pattern, value)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 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 (c *Config) Get(pattern string, def ...interface{}) interface{} {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.Get(pattern, def...)
|
||||
@ -23,13 +41,15 @@ func (c *Config) Get(pattern string, def ...interface{}) interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetVar returns a gvar.Var with value by given <pattern>.
|
||||
func (c *Config) GetVar(pattern string, def ...interface{}) *gvar.Var {
|
||||
if j := c.getJson(); j != nil {
|
||||
return gvar.New(j.Get(pattern, def...))
|
||||
return j.GetVar(pattern, def...)
|
||||
}
|
||||
return gvar.New(nil)
|
||||
}
|
||||
|
||||
// Contains checks whether the value by specified <pattern> exist.
|
||||
func (c *Config) Contains(pattern string) bool {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.Contains(pattern)
|
||||
@ -37,6 +57,7 @@ func (c *Config) Contains(pattern string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// GetMap retrieves and returns the value by specified <pattern> as map[string]interface{}.
|
||||
func (c *Config) GetMap(pattern string, def ...interface{}) map[string]interface{} {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetMap(pattern, def...)
|
||||
@ -44,6 +65,7 @@ func (c *Config) GetMap(pattern string, def ...interface{}) map[string]interface
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetMapStrStr retrieves and returns the value by specified <pattern> as map[string]string.
|
||||
func (c *Config) GetMapStrStr(pattern string, def ...interface{}) map[string]string {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetMapStrStr(pattern, def...)
|
||||
@ -51,6 +73,8 @@ func (c *Config) GetMapStrStr(pattern string, def ...interface{}) map[string]str
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetArray retrieves the value by specified <pattern>,
|
||||
// and converts it to a slice of []interface{}.
|
||||
func (c *Config) GetArray(pattern string, def ...interface{}) []interface{} {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetArray(pattern, def...)
|
||||
@ -58,6 +82,7 @@ func (c *Config) GetArray(pattern string, def ...interface{}) []interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetBytes retrieves the value by specified <pattern> and converts it to []byte.
|
||||
func (c *Config) GetBytes(pattern string, def ...interface{}) []byte {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetBytes(pattern, def...)
|
||||
@ -65,6 +90,7 @@ func (c *Config) GetBytes(pattern string, def ...interface{}) []byte {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetString retrieves the value by specified <pattern> and converts it to string.
|
||||
func (c *Config) GetString(pattern string, def ...interface{}) string {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetString(pattern, def...)
|
||||
@ -72,6 +98,7 @@ func (c *Config) GetString(pattern string, def ...interface{}) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// GetStrings retrieves the value by specified <pattern> and converts it to []string.
|
||||
func (c *Config) GetStrings(pattern string, def ...interface{}) []string {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetStrings(pattern, def...)
|
||||
@ -79,6 +106,8 @@ func (c *Config) GetStrings(pattern string, def ...interface{}) []string {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetInterfaces is alias of GetArray.
|
||||
// See GetArray.
|
||||
func (c *Config) GetInterfaces(pattern string, def ...interface{}) []interface{} {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetInterfaces(pattern, def...)
|
||||
@ -86,6 +115,10 @@ func (c *Config) GetInterfaces(pattern string, def ...interface{}) []interface{}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 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 (c *Config) GetBool(pattern string, def ...interface{}) bool {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetBool(pattern, def...)
|
||||
@ -93,6 +126,7 @@ func (c *Config) GetBool(pattern string, def ...interface{}) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// GetFloat32 retrieves the value by specified <pattern> and converts it to float32.
|
||||
func (c *Config) GetFloat32(pattern string, def ...interface{}) float32 {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetFloat32(pattern, def...)
|
||||
@ -100,6 +134,7 @@ func (c *Config) GetFloat32(pattern string, def ...interface{}) float32 {
|
||||
return 0
|
||||
}
|
||||
|
||||
// GetFloat64 retrieves the value by specified <pattern> and converts it to float64.
|
||||
func (c *Config) GetFloat64(pattern string, def ...interface{}) float64 {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetFloat64(pattern, def...)
|
||||
@ -107,6 +142,7 @@ func (c *Config) GetFloat64(pattern string, def ...interface{}) float64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
// GetFloats retrieves the value by specified <pattern> and converts it to []float64.
|
||||
func (c *Config) GetFloats(pattern string, def ...interface{}) []float64 {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetFloats(pattern, def...)
|
||||
@ -114,6 +150,7 @@ func (c *Config) GetFloats(pattern string, def ...interface{}) []float64 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetInt retrieves the value by specified <pattern> and converts it to int.
|
||||
func (c *Config) GetInt(pattern string, def ...interface{}) int {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetInt(pattern, def...)
|
||||
@ -121,6 +158,7 @@ func (c *Config) GetInt(pattern string, def ...interface{}) int {
|
||||
return 0
|
||||
}
|
||||
|
||||
// GetInt8 retrieves the value by specified <pattern> and converts it to int8.
|
||||
func (c *Config) GetInt8(pattern string, def ...interface{}) int8 {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetInt8(pattern, def...)
|
||||
@ -128,6 +166,7 @@ func (c *Config) GetInt8(pattern string, def ...interface{}) int8 {
|
||||
return 0
|
||||
}
|
||||
|
||||
// GetInt16 retrieves the value by specified <pattern> and converts it to int16.
|
||||
func (c *Config) GetInt16(pattern string, def ...interface{}) int16 {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetInt16(pattern, def...)
|
||||
@ -135,6 +174,7 @@ func (c *Config) GetInt16(pattern string, def ...interface{}) int16 {
|
||||
return 0
|
||||
}
|
||||
|
||||
// GetInt32 retrieves the value by specified <pattern> and converts it to int32.
|
||||
func (c *Config) GetInt32(pattern string, def ...interface{}) int32 {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetInt32(pattern, def...)
|
||||
@ -142,6 +182,7 @@ func (c *Config) GetInt32(pattern string, def ...interface{}) int32 {
|
||||
return 0
|
||||
}
|
||||
|
||||
// GetInt64 retrieves the value by specified <pattern> and converts it to int64.
|
||||
func (c *Config) GetInt64(pattern string, def ...interface{}) int64 {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetInt64(pattern, def...)
|
||||
@ -149,6 +190,7 @@ func (c *Config) GetInt64(pattern string, def ...interface{}) int64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
// GetInts retrieves the value by specified <pattern> and converts it to []int.
|
||||
func (c *Config) GetInts(pattern string, def ...interface{}) []int {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetInts(pattern, def...)
|
||||
@ -156,6 +198,7 @@ func (c *Config) GetInts(pattern string, def ...interface{}) []int {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetUint retrieves the value by specified <pattern> and converts it to uint.
|
||||
func (c *Config) GetUint(pattern string, def ...interface{}) uint {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetUint(pattern, def...)
|
||||
@ -163,6 +206,7 @@ func (c *Config) GetUint(pattern string, def ...interface{}) uint {
|
||||
return 0
|
||||
}
|
||||
|
||||
// GetUint8 retrieves the value by specified <pattern> and converts it to uint8.
|
||||
func (c *Config) GetUint8(pattern string, def ...interface{}) uint8 {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetUint8(pattern, def...)
|
||||
@ -170,6 +214,7 @@ func (c *Config) GetUint8(pattern string, def ...interface{}) uint8 {
|
||||
return 0
|
||||
}
|
||||
|
||||
// GetUint16 retrieves the value by specified <pattern> and converts it to uint16.
|
||||
func (c *Config) GetUint16(pattern string, def ...interface{}) uint16 {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetUint16(pattern, def...)
|
||||
@ -177,6 +222,7 @@ func (c *Config) GetUint16(pattern string, def ...interface{}) uint16 {
|
||||
return 0
|
||||
}
|
||||
|
||||
// GetUint32 retrieves the value by specified <pattern> and converts it to uint32.
|
||||
func (c *Config) GetUint32(pattern string, def ...interface{}) uint32 {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetUint32(pattern, def...)
|
||||
@ -184,6 +230,7 @@ func (c *Config) GetUint32(pattern string, def ...interface{}) uint32 {
|
||||
return 0
|
||||
}
|
||||
|
||||
// GetUint64 retrieves the value by specified <pattern> and converts it to uint64.
|
||||
func (c *Config) GetUint64(pattern string, def ...interface{}) uint64 {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetUint64(pattern, def...)
|
||||
@ -191,6 +238,7 @@ func (c *Config) GetUint64(pattern string, def ...interface{}) uint64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
// GetTime retrieves the value by specified <pattern> and converts it to time.Time.
|
||||
func (c *Config) GetTime(pattern string, format ...string) time.Time {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetTime(pattern, format...)
|
||||
@ -198,6 +246,7 @@ func (c *Config) GetTime(pattern string, format ...string) time.Time {
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
// GetDuration retrieves the value by specified <pattern> and converts it to time.Duration.
|
||||
func (c *Config) GetDuration(pattern string, def ...interface{}) time.Duration {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetDuration(pattern, def...)
|
||||
@ -205,6 +254,7 @@ func (c *Config) GetDuration(pattern string, def ...interface{}) time.Duration {
|
||||
return 0
|
||||
}
|
||||
|
||||
// GetGTime retrieves the value by specified <pattern> and converts it to *gtime.Time.
|
||||
func (c *Config) GetGTime(pattern string, format ...string) *gtime.Time {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetGTime(pattern, format...)
|
||||
@ -212,6 +262,8 @@ func (c *Config) GetGTime(pattern string, format ...string) *gtime.Time {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetJson gets the value by specified <pattern>,
|
||||
// and converts it to a un-concurrent-safe Json object.
|
||||
func (c *Config) GetJson(pattern string, def ...interface{}) *gjson.Json {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetJson(pattern, def...)
|
||||
@ -219,6 +271,8 @@ func (c *Config) GetJson(pattern string, def ...interface{}) *gjson.Json {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetJsons gets the value by specified <pattern>,
|
||||
// and converts it to a slice of un-concurrent-safe Json object.
|
||||
func (c *Config) GetJsons(pattern string, def ...interface{}) []*gjson.Json {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetJsons(pattern, def...)
|
||||
@ -226,6 +280,8 @@ func (c *Config) GetJsons(pattern string, def ...interface{}) []*gjson.Json {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetJsonMap gets the value by specified <pattern>,
|
||||
// and converts it to a map of un-concurrent-safe Json object.
|
||||
func (c *Config) GetJsonMap(pattern string, def ...interface{}) map[string]*gjson.Json {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetJsonMap(pattern, def...)
|
||||
@ -233,6 +289,8 @@ func (c *Config) GetJsonMap(pattern string, def ...interface{}) map[string]*gjso
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetStruct retrieves the value by specified <pattern> and converts it to specified object
|
||||
// <pointer>. The <pointer> should be the pointer to an object.
|
||||
func (c *Config) GetStruct(pattern string, pointer interface{}, mapping ...map[string]string) error {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetStruct(pattern, pointer, mapping...)
|
||||
@ -240,6 +298,7 @@ func (c *Config) GetStruct(pattern string, pointer interface{}, mapping ...map[s
|
||||
return errors.New("configuration not found")
|
||||
}
|
||||
|
||||
// GetStructDeep does GetStruct recursively.
|
||||
func (c *Config) GetStructDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetStructDeep(pattern, pointer, mapping...)
|
||||
@ -247,6 +306,7 @@ func (c *Config) GetStructDeep(pattern string, pointer interface{}, mapping ...m
|
||||
return errors.New("configuration not found")
|
||||
}
|
||||
|
||||
// GetStructs converts any slice to given struct slice.
|
||||
func (c *Config) GetStructs(pattern string, pointer interface{}, mapping ...map[string]string) error {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetStructs(pattern, pointer, mapping...)
|
||||
@ -254,6 +314,7 @@ func (c *Config) GetStructs(pattern string, pointer interface{}, mapping ...map[
|
||||
return errors.New("configuration not found")
|
||||
}
|
||||
|
||||
// GetStructsDeep converts any slice to given struct slice recursively.
|
||||
func (c *Config) GetStructsDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetStructsDeep(pattern, pointer, mapping...)
|
||||
@ -261,6 +322,8 @@ func (c *Config) GetStructsDeep(pattern string, pointer interface{}, mapping ...
|
||||
return errors.New("configuration not found")
|
||||
}
|
||||
|
||||
// GetMapToMap retrieves the value by specified <pattern> and converts it to specified map variable.
|
||||
// See gconv.MapToMap.
|
||||
func (c *Config) GetMapToMap(pattern string, pointer interface{}, mapping ...map[string]string) error {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetMapToMap(pattern, pointer, mapping...)
|
||||
@ -268,6 +331,9 @@ func (c *Config) GetMapToMap(pattern string, pointer interface{}, mapping ...map
|
||||
return errors.New("configuration not found")
|
||||
}
|
||||
|
||||
// GetMapToMapDeep retrieves the value by specified <pattern> and converts it to specified map
|
||||
// variable recursively.
|
||||
// See gconv.MapToMapDeep.
|
||||
func (c *Config) GetMapToMapDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetMapToMapDeep(pattern, pointer, mapping...)
|
||||
@ -275,6 +341,9 @@ func (c *Config) GetMapToMapDeep(pattern string, pointer interface{}, mapping ..
|
||||
return errors.New("configuration not found")
|
||||
}
|
||||
|
||||
// GetMapToMaps retrieves the value by specified <pattern> and converts it to specified map slice
|
||||
// variable.
|
||||
// See gconv.MapToMaps.
|
||||
func (c *Config) GetMapToMaps(pattern string, pointer interface{}, mapping ...map[string]string) error {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetMapToMaps(pattern, pointer, mapping...)
|
||||
@ -282,6 +351,9 @@ func (c *Config) GetMapToMaps(pattern string, pointer interface{}, mapping ...ma
|
||||
return errors.New("configuration not found")
|
||||
}
|
||||
|
||||
// GetMapToMapsDeep retrieves the value by specified <pattern> and converts it to specified map slice
|
||||
// variable recursively.
|
||||
// See gconv.MapToMapsDeep.
|
||||
func (c *Config) GetMapToMapsDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.GetMapToMapsDeep(pattern, pointer, mapping...)
|
||||
@ -289,6 +361,8 @@ func (c *Config) GetMapToMapsDeep(pattern string, pointer interface{}, mapping .
|
||||
return errors.New("configuration not found")
|
||||
}
|
||||
|
||||
// ToMap converts current Json object to map[string]interface{}.
|
||||
// It returns nil if fails.
|
||||
func (c *Config) ToMap() map[string]interface{} {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.ToMap()
|
||||
@ -296,6 +370,8 @@ func (c *Config) ToMap() map[string]interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ToArray converts current Json object to []interface{}.
|
||||
// It returns nil if fails.
|
||||
func (c *Config) ToArray() []interface{} {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.ToArray()
|
||||
@ -303,6 +379,8 @@ func (c *Config) ToArray() []interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ToStruct converts current Json object to specified object.
|
||||
// The <pointer> should be a pointer type of *struct.
|
||||
func (c *Config) ToStruct(pointer interface{}, mapping ...map[string]string) error {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.ToStruct(pointer, mapping...)
|
||||
@ -310,6 +388,8 @@ func (c *Config) ToStruct(pointer interface{}, mapping ...map[string]string) err
|
||||
return errors.New("configuration not found")
|
||||
}
|
||||
|
||||
// ToStructDeep converts current Json object to specified object recursively.
|
||||
// The <pointer> should be a pointer type of *struct.
|
||||
func (c *Config) ToStructDeep(pointer interface{}, mapping ...map[string]string) error {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.ToStructDeep(pointer, mapping...)
|
||||
@ -317,6 +397,8 @@ func (c *Config) ToStructDeep(pointer interface{}, mapping ...map[string]string)
|
||||
return errors.New("configuration not found")
|
||||
}
|
||||
|
||||
// ToStructs converts current Json object to specified object slice.
|
||||
// The <pointer> should be a pointer type of []struct/*struct.
|
||||
func (c *Config) ToStructs(pointer interface{}, mapping ...map[string]string) error {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.ToStructs(pointer, mapping...)
|
||||
@ -324,6 +406,8 @@ func (c *Config) ToStructs(pointer interface{}, mapping ...map[string]string) er
|
||||
return errors.New("configuration not found")
|
||||
}
|
||||
|
||||
// ToStructsDeep converts current Json object to specified object slice recursively.
|
||||
// The <pointer> should be a pointer type of []struct/*struct.
|
||||
func (c *Config) ToStructsDeep(pointer interface{}, mapping ...map[string]string) error {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.ToStructsDeep(pointer, mapping...)
|
||||
@ -331,6 +415,8 @@ func (c *Config) ToStructsDeep(pointer interface{}, mapping ...map[string]string
|
||||
return errors.New("configuration not found")
|
||||
}
|
||||
|
||||
// ToMapToMap converts current Json object to specified map variable.
|
||||
// The parameter of <pointer> should be type of *map.
|
||||
func (c *Config) ToMapToMap(pointer interface{}, mapping ...map[string]string) error {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.ToMapToMap(pointer, mapping...)
|
||||
@ -338,6 +424,8 @@ func (c *Config) ToMapToMap(pointer interface{}, mapping ...map[string]string) e
|
||||
return errors.New("configuration not found")
|
||||
}
|
||||
|
||||
// ToMapToMapDeep converts current Json object to specified map variable recursively.
|
||||
// The parameter of <pointer> should be type of *map.
|
||||
func (c *Config) ToMapToMapDeep(pointer interface{}, mapping ...map[string]string) error {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.ToMapToMapDeep(pointer, mapping...)
|
||||
@ -345,6 +433,8 @@ func (c *Config) ToMapToMapDeep(pointer interface{}, mapping ...map[string]strin
|
||||
return errors.New("configuration not found")
|
||||
}
|
||||
|
||||
// ToMapToMaps converts current Json object to specified map variable slice.
|
||||
// The parameter of <pointer> should be type of []map/*map.
|
||||
func (c *Config) ToMapToMaps(pointer interface{}, mapping ...map[string]string) error {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.ToMapToMaps(pointer, mapping...)
|
||||
@ -352,6 +442,8 @@ func (c *Config) ToMapToMaps(pointer interface{}, mapping ...map[string]string)
|
||||
return errors.New("configuration not found")
|
||||
}
|
||||
|
||||
// ToMapToMapsDeep converts current Json object to specified map variable slice recursively.
|
||||
// The parameter of <pointer> should be type of []map/*map.
|
||||
func (c *Config) ToMapToMapsDeep(pointer interface{}, mapping ...map[string]string) error {
|
||||
if j := c.getJson(); j != nil {
|
||||
return j.ToMapToMapsDeep(pointer, mapping...)
|
||||
|
||||
@ -82,7 +82,6 @@ array = [1,2,3]
|
||||
"cache": "127.0.0.1:6379,1",
|
||||
})
|
||||
t.AssertEQ(c.FilePath(), gfile.Pwd()+gfile.Separator+path)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -359,6 +358,23 @@ func TestCfg_FilePath(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestCfg_et(t *testing.T) {
|
||||
config := `log-path = "logs"`
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
path := gcfg.DEFAULT_CONFIG_FILE
|
||||
err := gfile.PutContents(path, config)
|
||||
t.Assert(err, nil)
|
||||
defer gfile.Remove(path)
|
||||
|
||||
c := gcfg.New()
|
||||
t.Assert(c.Get("log-path"), "logs")
|
||||
|
||||
err = c.Set("log-path", "custom-logs")
|
||||
t.Assert(err, nil)
|
||||
t.Assert(c.Get("log-path"), "custom-logs")
|
||||
})
|
||||
}
|
||||
|
||||
func TestCfg_Get(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var err error
|
||||
|
||||
Reference in New Issue
Block a user