From 1724a269576f3124f8210b1646213cdbb8b08b88 Mon Sep 17 00:00:00 2001 From: John Guo Date: Tue, 1 Mar 2022 22:34:57 +0800 Subject: [PATCH] improve package gcfg --- frame/gins/gins_z_unit_config_test.go | 15 +++++++------ os/gcfg/gcfg_adapter_file.go | 1 + os/gcfg/gcfg_adapter_file_path.go | 31 ++++++++++++++++++--------- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/frame/gins/gins_z_unit_config_test.go b/frame/gins/gins_z_unit_config_test.go index 409fef420..1395e34d6 100644 --- a/frame/gins/gins_z_unit_config_test.go +++ b/frame/gins/gins_z_unit_config_test.go @@ -65,16 +65,15 @@ func Test_Config2(t *testing.T) { gtest.C(t, func(t *gtest.T) { var err error dirPath := gfile.Temp(gtime.TimestampNanoStr()) - err = gfile.Mkdir(dirPath) - t.Assert(err, nil) + t.AssertNil(gfile.Mkdir(dirPath)) defer gfile.Remove(dirPath) name := "config/config.toml" err = gfile.PutContents(gfile.Join(dirPath, name), configContent) - t.Assert(err, nil) + t.AssertNil(err) err = gins.Config().GetAdapter().(*gcfg.AdapterFile).AddPath(dirPath) - t.Assert(err, nil) + t.AssertNil(err) defer gins.Config().GetAdapter().(*gcfg.AdapterFile).Clear() @@ -200,9 +199,11 @@ func Test_Config4(t *testing.T) { func Test_Basic2(t *testing.T) { config := `log-path = "logs"` gtest.C(t, func(t *gtest.T) { - path := gcfg.DefaultConfigFile - err := gfile.PutContents(path, config) - t.Assert(err, nil) + var ( + path = gcfg.DefaultConfigFileName + err = gfile.PutContents(path, config) + ) + t.AssertNil(err) defer func() { _ = gfile.Remove(path) }() diff --git a/os/gcfg/gcfg_adapter_file.go b/os/gcfg/gcfg_adapter_file.go index 795228432..c7d42ccbe 100644 --- a/os/gcfg/gcfg_adapter_file.go +++ b/os/gcfg/gcfg_adapter_file.go @@ -223,6 +223,7 @@ func (c *AdapterFile) getJson(fileName ...string) (configJson *gjson.Json, err e } else { usedFileName = c.defaultName } + // It uses json map to cache specified configuration file content. result := c.jsonMap.GetOrSetFuncLock(usedFileName, func() interface{} { var ( content string diff --git a/os/gcfg/gcfg_adapter_file_path.go b/os/gcfg/gcfg_adapter_file_path.go index c4149a983..bd83a3fe5 100644 --- a/os/gcfg/gcfg_adapter_file_path.go +++ b/os/gcfg/gcfg_adapter_file_path.go @@ -10,6 +10,7 @@ import ( "bytes" "context" "fmt" + "os" "github.com/gogf/gf/v2/errors/gcode" "github.com/gogf/gf/v2/errors/gerror" @@ -142,23 +143,33 @@ func (c *AdapterFile) AddPath(path string) (err error) { // 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 *AdapterFile) doGetFilePath(fileName string) (path string) { - var tempPath string + var ( + tempPath string + resFile *gres.File + fileInfo os.FileInfo + ) // Searching resource manager. if !gres.IsEmpty() { for _, tryFolder := range resourceTryFolders { tempPath = tryFolder + fileName - if file := gres.Get(tempPath); file != nil { - path = file.Name() - return + if resFile = gres.Get(tempPath); resFile != nil { + fileInfo, _ = resFile.Stat() + if fileInfo != nil && !fileInfo.IsDir() { + path = resFile.Name() + return + } } } c.searchPaths.RLockFunc(func(array []string) { for _, searchPath := range array { for _, tryFolder := range resourceTryFolders { tempPath = searchPath + tryFolder + fileName - if file := gres.Get(tempPath); file != nil { - path = file.Name() - return + if resFile = gres.Get(tempPath); resFile != nil { + fileInfo, _ = resFile.Stat() + if fileInfo != nil && !fileInfo.IsDir() { + path = resFile.Name() + return + } } } } @@ -170,7 +181,7 @@ func (c *AdapterFile) doGetFilePath(fileName string) (path string) { // Searching local file system. if path == "" { // Absolute path. - if path = gfile.RealPath(fileName); path != "" { + if path = gfile.RealPath(fileName); path != "" && !gfile.IsDir(path) { return } c.searchPaths.RLockFunc(func(array []string) { @@ -181,7 +192,7 @@ func (c *AdapterFile) doGetFilePath(fileName string) (path string) { gfile.Join(tryFolder, fileName), `\/`, ) - if path, _ = gspath.Search(searchPath, relativePath); path != "" { + if path, _ = gspath.Search(searchPath, relativePath); path != "" && !gfile.IsDir(path) { return } } @@ -204,7 +215,7 @@ func (c *AdapterFile) GetFilePath(fileName ...string) (path string, err error) { usedFileName = fileName[0] } fileExtName = gfile.ExtName(usedFileName) - if path = c.doGetFilePath(usedFileName); path == "" && !gstr.InArray(supportedFileTypes, fileExtName) { + if path = c.doGetFilePath(usedFileName); (path == "" || gfile.IsDir(path)) && !gstr.InArray(supportedFileTypes, fileExtName) { // If it's not using default configuration or its configuration file is not available, // it searches the possible configuration file according to the name and all supported // file types.