Skip to content

Commit

Permalink
fix issue in incorrect parameter sequence in package gi18n;improve pa…
Browse files Browse the repository at this point in the history
…ckage gcfg for detailed error printing
  • Loading branch information
jianchenma committed Mar 16, 2021
1 parent 41f2138 commit 150f237
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 151 deletions.
14 changes: 9 additions & 5 deletions frame/gins/gins_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
))
Expand Down
9 changes: 8 additions & 1 deletion frame/gins/gins_redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down
2 changes: 1 addition & 1 deletion i18n/gi18n/gi18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
151 changes: 71 additions & 80 deletions os/gcfg/gcfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 (
Expand Down Expand Up @@ -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)
}
Expand All @@ -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]
Expand Down Expand Up @@ -308,38 +302,32 @@ func (c *Config) FilePath(file ...string) (path 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
// 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 false
return
}

// 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
Expand All @@ -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
}
Expand All @@ -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 {
Expand All @@ -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
Expand Down
Loading

0 comments on commit 150f237

Please sign in to comment.