fix issue in incorrect parameter sequence in package gi18n;improve package gcfg for detailed error printing

This commit is contained in:
jianchenma
2021-03-16 14:39:01 +08:00
parent 41f2138b39
commit 150f237f13
9 changed files with 155 additions and 151 deletions

View File

@ -48,22 +48,26 @@ func Database(name ...string) gdb.DB {
configMap = Config().GetMap(configNodeKey)
}
if len(configMap) == 0 && !gdb.IsConfigured() {
if !Config().Available() {
configFilePath, err := Config().GetFilePath()
if configFilePath == "" {
exampleFileName := "config.example.toml"
if Config().Available(exampleFileName) {
panic(gerror.Newf(
if exampleConfigFilePath, _ := Config().GetFilePath(exampleFileName); exampleConfigFilePath != "" {
panic(gerror.Wrapf(
err,
`configuration file "%s" not found, but found "%s", did you miss renaming the configuration example file?`,
Config().GetFileName(),
exampleFileName,
))
} else {
panic(gerror.Newf(
panic(gerror.Wrapf(
err,
`configuration file "%s" not found, did you miss the configuration file or the file name setting?`,
Config().GetFileName(),
))
}
}
panic(gerror.Newf(
panic(gerror.Wrapf(
err,
`database initialization failed: "%s" node not found, is configuration file or configuration node missing?`,
configNodeNameDatabase,
))

View File

@ -47,7 +47,14 @@ func Redis(name ...string) *gredis.Redis {
panic(fmt.Sprintf(`configuration for redis not found for group "%s"`, group))
}
} else {
panic(fmt.Sprintf(`incomplete configuration for redis: "redis" node not found in config file "%s"`, config.FilePath()))
filepath, err := config.GetFilePath()
if err != nil {
panic(err)
}
panic(fmt.Sprintf(
`incomplete configuration for redis: "redis" node not found in config file "%s"`,
filepath,
))
}
return nil
})

View File

@ -53,7 +53,7 @@ func TranslateFormat(format string, values ...interface{}) string {
// configured language. If <language> is given empty string, it uses the default configured
// language for the translation.
func TranslateFormatLang(language string, format string, values ...interface{}) string {
return defaultManager.TranslateFormatLang(format, language, values...)
return defaultManager.TranslateFormatLang(language, format, values...)
}
// Translate translates <content> with configured language and returns the translated content.

View File

@ -11,6 +11,7 @@ import (
"bytes"
"errors"
"fmt"
"github.com/gogf/gf/errors/gerror"
"github.com/gogf/gf/internal/intlog"
"github.com/gogf/gf/os/gcmd"
"github.com/gogf/gf/text/gstr"
@ -45,7 +46,7 @@ var (
)
// New returns a new configuration management object.
// The parameter <file> specifies the default configuration file name for reading.
// The parameter `file` specifies the default configuration file name for reading.
func New(file ...string) *Config {
name := DefaultConfigFile
if len(file) > 0 {
@ -67,7 +68,7 @@ func New(file ...string) *Config {
_ = c.SetPath(customPath)
} else {
if errorPrint() {
glog.Errorf("Configuration directory path does not exist: %s", customPath)
glog.Errorf("[gcfg] Configuration directory path does not exist: %s", customPath)
}
}
} else {
@ -93,42 +94,8 @@ func New(file ...string) *Config {
return c
}
// filePath returns the absolute configuration file path for the given filename by <file>.
func (c *Config) filePath(file ...string) (path string) {
name := c.defaultName
if len(file) > 0 {
name = file[0]
}
path = c.FilePath(name)
if path == "" {
var (
buffer = bytes.NewBuffer(nil)
)
if c.searchPaths.Len() > 0 {
buffer.WriteString(fmt.Sprintf("[gcfg] cannot find config file \"%s\" in following paths:", name))
c.searchPaths.RLockFunc(func(array []string) {
index := 1
for _, v := range array {
v = gstr.TrimRight(v, `\/`)
buffer.WriteString(fmt.Sprintf("\n%d. %s", index, v))
index++
buffer.WriteString(fmt.Sprintf("\n%d. %s", index, v+gfile.Separator+"config"))
index++
}
})
} else {
buffer.WriteString(fmt.Sprintf("[gcfg] cannot find config file \"%s\" with no path set/add", name))
}
if errorPrint() {
glog.Error(buffer.String())
}
}
return path
}
// SetPath sets the configuration directory path for file search.
// The parameter <path> can be absolute or relative path,
// The parameter `path` can be absolute or relative path,
// but absolute path is strongly recommended.
func (c *Config) SetPath(path string) error {
var (
@ -246,14 +213,14 @@ func (c *Config) AddPath(path string) error {
} else {
buffer.WriteString(fmt.Sprintf(`[gcfg] AddPath failed: path "%s" does not exist`, path))
}
err := errors.New(buffer.String())
err := gerror.New(buffer.String())
if errorPrint() {
glog.Error(err)
}
return err
}
if !isDir {
err := fmt.Errorf(`[gcfg] AddPath failed: path "%s" should be directory type`, path)
err := gerror.Newf(`[gcfg] AddPath failed: path "%s" should be directory type`, path)
if errorPrint() {
glog.Error(err)
}
@ -268,11 +235,38 @@ func (c *Config) AddPath(path string) error {
return nil
}
// 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 (c *Config) FilePath(file ...string) (path string) {
// SetFileName sets the default configuration file name.
func (c *Config) SetFileName(name string) *Config {
c.defaultName = name
return c
}
// GetFileName returns the default configuration file name.
func (c *Config) GetFileName() string {
return c.defaultName
}
// Available checks and returns whether configuration of given `file` is available.
func (c *Config) Available(file ...string) bool {
var name string
if len(file) > 0 && file[0] != "" {
name = file[0]
} else {
name = c.defaultName
}
if path, _ := c.GetFilePath(name); path != "" {
return true
}
if GetContent(name) != "" {
return true
}
return false
}
// GetFilePath returns the absolute configuration file path for the given filename by `file`.
// If `file` is not passed, it returns the configuration file path of the default name.
// It returns an empty `path` string and an error if the given `file` does not exist.
func (c *Config) GetFilePath(file ...string) (path string, err error) {
name := c.defaultName
if len(file) > 0 {
name = file[0]
@ -308,38 +302,32 @@ func (c *Config) FilePath(file ...string) (path string) {
}
}
})
// If it cannot find the path of `file`, it formats and returns a detailed error.
if path == "" {
var (
buffer = bytes.NewBuffer(nil)
)
if c.searchPaths.Len() > 0 {
buffer.WriteString(fmt.Sprintf(`[gcfg] cannot find config file "%s" in resource manager or the following paths:`, name))
c.searchPaths.RLockFunc(func(array []string) {
index := 1
for _, v := range array {
v = gstr.TrimRight(v, `\/`)
buffer.WriteString(fmt.Sprintf("\n%d. %s", index, v))
index++
buffer.WriteString(fmt.Sprintf("\n%d. %s", index, v+gfile.Separator+"config"))
index++
}
})
} else {
buffer.WriteString(fmt.Sprintf("[gcfg] cannot find config file \"%s\" with no path configured", name))
}
err = gerror.New(buffer.String())
}
return
}
// SetFileName sets the default configuration file name.
func (c *Config) SetFileName(name string) *Config {
c.defaultName = name
return c
}
// GetFileName returns the default configuration file name.
func (c *Config) GetFileName() string {
return c.defaultName
}
// Available checks and returns whether configuration of given <file> is available.
func (c *Config) Available(file ...string) bool {
var name string
if len(file) > 0 && file[0] != "" {
name = file[0]
} else {
name = c.defaultName
}
if c.FilePath(name) != "" {
return true
}
if GetContent(name) != "" {
return true
}
return false
}
// getJson returns a *gjson.Json object for the specified <file> content.
// getJson returns a *gjson.Json object for the specified `file` content.
// It would print error if file reading fails. It return nil if any error occurs.
func (c *Config) getJson(file ...string) *gjson.Json {
var name string
@ -350,14 +338,18 @@ func (c *Config) getJson(file ...string) *gjson.Json {
}
r := c.jsonMap.GetOrSetFuncLock(name, func() interface{} {
var (
content = ""
filePath = ""
err error
content string
filePath string
)
// The configured content can be any kind of data type different from its file type.
isFromConfigContent := true
if content = GetContent(name); content == "" {
isFromConfigContent = false
filePath = c.filePath(name)
filePath, err = c.GetFilePath(name)
if err != nil && errorPrint() {
glog.Error(err)
}
if filePath == "" {
return nil
}
@ -369,8 +361,7 @@ func (c *Config) getJson(file ...string) *gjson.Json {
}
// Note that the underlying configuration json object operations are concurrent safe.
var (
j *gjson.Json
err error
j *gjson.Json
)
dataType := gfile.ExtName(name)
if gjson.IsValidDataType(dataType) && !isFromConfigContent {
@ -394,9 +385,9 @@ func (c *Config) getJson(file ...string) *gjson.Json {
}
if errorPrint() {
if filePath != "" {
glog.Criticalf(`[gcfg] Load config file "%s" failed: %s`, filePath, err.Error())
glog.Criticalf(`[gcfg] load config file "%s" failed: %s`, filePath, err.Error())
} else {
glog.Criticalf(`[gcfg] Load configuration failed: %s`, err.Error())
glog.Criticalf(`[gcfg] load configuration failed: %s`, err.Error())
}
}
return nil

View File

@ -16,7 +16,7 @@ import (
"github.com/gogf/gf/os/gtime"
)
// Set sets value with specified <pattern>.
// 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 {
@ -26,14 +26,14 @@ func (c *Config) Set(pattern string, value interface{}) error {
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>.
// 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:
// 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.
// 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...)
@ -41,7 +41,7 @@ func (c *Config) Get(pattern string, def ...interface{}) interface{} {
return nil
}
// GetVar returns a gvar.Var with value by given <pattern>.
// 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 j.GetVar(pattern, def...)
@ -49,7 +49,7 @@ func (c *Config) GetVar(pattern string, def ...interface{}) *gvar.Var {
return gvar.New(nil)
}
// Contains checks whether the value by specified <pattern> exist.
// 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)
@ -57,7 +57,7 @@ func (c *Config) Contains(pattern string) bool {
return false
}
// GetMap retrieves and returns the value by specified <pattern> as map[string]interface{}.
// 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...)
@ -65,7 +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.
// 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...)
@ -73,7 +73,7 @@ func (c *Config) GetMapStrStr(pattern string, def ...interface{}) map[string]str
return nil
}
// GetArray retrieves the value by specified <pattern>,
// 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 {
@ -82,7 +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.
// 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...)
@ -90,7 +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.
// 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...)
@ -98,7 +98,7 @@ func (c *Config) GetString(pattern string, def ...interface{}) string {
return ""
}
// GetStrings retrieves the value by specified <pattern> and converts it to []string.
// 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...)
@ -115,7 +115,7 @@ func (c *Config) GetInterfaces(pattern string, def ...interface{}) []interface{}
return nil
}
// GetBool retrieves the value by specified <pattern>,
// 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.
@ -126,7 +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.
// 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...)
@ -134,7 +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.
// 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...)
@ -142,7 +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.
// 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...)
@ -150,7 +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.
// 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...)
@ -158,7 +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.
// 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...)
@ -166,7 +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.
// 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...)
@ -174,7 +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.
// 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...)
@ -182,7 +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.
// 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...)
@ -190,7 +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.
// 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...)
@ -198,7 +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.
// 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...)
@ -206,7 +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.
// 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...)
@ -214,7 +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.
// 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...)
@ -222,7 +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.
// 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...)
@ -230,7 +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.
// 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...)
@ -238,7 +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.
// 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...)
@ -246,7 +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.
// 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...)
@ -254,7 +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.
// 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...)
@ -262,7 +262,7 @@ func (c *Config) GetGTime(pattern string, format ...string) *gtime.Time {
return nil
}
// GetJson gets the value by specified <pattern>,
// 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 {
@ -271,7 +271,7 @@ func (c *Config) GetJson(pattern string, def ...interface{}) *gjson.Json {
return nil
}
// GetJsons gets the value by specified <pattern>,
// 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 {
@ -280,7 +280,7 @@ func (c *Config) GetJsons(pattern string, def ...interface{}) []*gjson.Json {
return nil
}
// GetJsonMap gets the value by specified <pattern>,
// 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 {
@ -289,8 +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.
// 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...)
@ -324,7 +324,7 @@ 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.
// 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 {
@ -333,7 +333,7 @@ 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
// 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 {
@ -343,7 +343,7 @@ 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
// 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 {
@ -353,7 +353,7 @@ 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
// 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 {
@ -382,7 +382,7 @@ func (c *Config) ToArray() []interface{} {
}
// ToStruct converts current Json object to specified object.
// The <pointer> should be a pointer type of *struct.
// 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...)
@ -391,7 +391,7 @@ func (c *Config) ToStruct(pointer interface{}, mapping ...map[string]string) err
}
// ToStructDeep converts current Json object to specified object recursively.
// The <pointer> should be a pointer type of *struct.
// 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...)
@ -400,7 +400,7 @@ func (c *Config) ToStructDeep(pointer interface{}, mapping ...map[string]string)
}
// ToStructs converts current Json object to specified object slice.
// The <pointer> should be a pointer type of []struct/*struct.
// 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...)
@ -409,7 +409,7 @@ func (c *Config) ToStructs(pointer interface{}, mapping ...map[string]string) er
}
// ToStructsDeep converts current Json object to specified object slice recursively.
// The <pointer> should be a pointer type of []struct/*struct.
// 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...)
@ -418,7 +418,7 @@ func (c *Config) ToStructsDeep(pointer interface{}, mapping ...map[string]string
}
// ToMapToMap converts current Json object to specified map variable.
// The parameter of <pointer> should be type of *map.
// 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...)
@ -427,7 +427,7 @@ func (c *Config) ToMapToMap(pointer interface{}, mapping ...map[string]string) e
}
// ToMapToMapDeep converts current Json object to specified map variable recursively.
// The parameter of <pointer> should be type of *map.
// 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...)
@ -436,7 +436,7 @@ func (c *Config) ToMapToMapDeep(pointer interface{}, mapping ...map[string]strin
}
// ToMapToMaps converts current Json object to specified map variable slice.
// The parameter of <pointer> should be type of []map/*map.
// 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...)
@ -445,7 +445,7 @@ func (c *Config) ToMapToMaps(pointer interface{}, mapping ...map[string]string)
}
// ToMapToMapsDeep converts current Json object to specified map variable slice recursively.
// The parameter of <pointer> should be type of []map/*map.
// 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...)

View File

@ -16,14 +16,14 @@ var (
configs = gmap.NewStrStrMap(true)
)
// SetContent sets customized configuration content for specified <file>.
// The <file> is unnecessary param, default is DefaultConfigFile.
// SetContent sets customized configuration content for specified `file`.
// The `file` is unnecessary param, default is DefaultConfigFile.
func SetContent(content string, file ...string) {
name := DefaultConfigFile
if len(file) > 0 {
name = file[0]
}
// Clear file cache for instances which cached <name>.
// Clear file cache for instances which cached `name`.
instances.LockFunc(func(m map[string]interface{}) {
if configs.Contains(name) {
for _, v := range m {
@ -34,8 +34,8 @@ func SetContent(content string, file ...string) {
})
}
// GetContent returns customized configuration content for specified <file>.
// The <file> is unnecessary param, default is DefaultConfigFile.
// GetContent returns customized configuration content for specified `file`.
// The `file` is unnecessary param, default is DefaultConfigFile.
func GetContent(file ...string) string {
name := DefaultConfigFile
if len(file) > 0 {
@ -44,14 +44,14 @@ func GetContent(file ...string) string {
return configs.Get(name)
}
// RemoveContent removes the global configuration with specified <file>.
// If <name> is not passed, it removes configuration of the default group name.
// RemoveContent removes the global configuration with specified `file`.
// If `name` is not passed, it removes configuration of the default group name.
func RemoveContent(file ...string) {
name := DefaultConfigFile
if len(file) > 0 {
name = file[0]
}
// Clear file cache for instances which cached <name>.
// Clear file cache for instances which cached `name`.
instances.LockFunc(func(m map[string]interface{}) {
if configs.Contains(name) {
for _, v := range m {

View File

@ -22,7 +22,7 @@ var (
)
// 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"
// 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 Instance(name ...string) *Config {

View File

@ -81,7 +81,8 @@ array = [1,2,3]
"disk": "127.0.0.1:6379,0",
"cache": "127.0.0.1:6379,1",
})
t.AssertEQ(c.FilePath(), gfile.Pwd()+gfile.Separator+path)
filepath, _ := c.GetFilePath()
t.AssertEQ(filepath, gfile.Pwd()+gfile.Separator+path)
})
}
@ -223,8 +224,8 @@ func Test_SetFileName(t *testing.T) {
"disk": "127.0.0.1:6379,0",
"cache": "127.0.0.1:6379,1",
})
t.AssertEQ(c.FilePath(), gfile.Pwd()+gfile.Separator+path)
filepath, _ := c.GetFilePath()
t.AssertEQ(filepath, gfile.Pwd()+gfile.Separator+path)
})
}
@ -281,9 +282,9 @@ func TestCfg_AddPath(t *testing.T) {
func TestCfg_FilePath(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
c := gcfg.New("config.yml")
path := c.FilePath("tmp")
path, _ := c.GetFilePath("tmp")
t.Assert(path, "")
path = c.FilePath("tmp")
path, _ = c.GetFilePath("tmp")
t.Assert(path, "")
})
}

View File

@ -77,7 +77,8 @@ v4 = "1.234"
"disk": "127.0.0.1:6379,0",
"cache": "127.0.0.1:6379,1",
})
t.AssertEQ(c.FilePath(), gfile.Pwd()+gfile.Separator+path)
filepath, _ := c.GetFilePath()
t.AssertEQ(filepath, gfile.Pwd()+gfile.Separator+path)
})
}