From 7c92c2f7e85b738a48f26ebd12ac1302d5ac95f7 Mon Sep 17 00:00:00 2001 From: John Guo Date: Tue, 17 Oct 2023 20:12:07 +0800 Subject: [PATCH] improve comments and variable names for `gcfg.AdapterFile` of package `gcfg` (#3046) --- os/gcfg/gcfg_adapter_file_path.go | 95 +++++++++++++++++++------------ 1 file changed, 58 insertions(+), 37 deletions(-) diff --git a/os/gcfg/gcfg_adapter_file_path.go b/os/gcfg/gcfg_adapter_file_path.go index 53cb2765e..bbe11904d 100644 --- a/os/gcfg/gcfg_adapter_file_path.go +++ b/os/gcfg/gcfg_adapter_file_path.go @@ -21,25 +21,27 @@ import ( "github.com/gogf/gf/v2/text/gstr" ) -// 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 (a *AdapterFile) SetPath(path string) (err error) { +// SetPath sets the configuration `directory` path for file search. +// The parameter `path` can be absolute or relative `directory` path, +// but absolute `directory` path is strongly recommended. +// +// Note that this parameter is a path to a directory not a file. +func (a *AdapterFile) SetPath(directoryPath string) (err error) { var ( isDir = false realPath = "" ) - if file := gres.Get(path); file != nil { - realPath = path + if file := gres.Get(directoryPath); file != nil { + realPath = directoryPath isDir = file.FileInfo().IsDir() } else { // Absolute path. - realPath = gfile.RealPath(path) + realPath = gfile.RealPath(directoryPath) if realPath == "" { // Relative path. a.searchPaths.RLockFunc(func(array []string) { for _, v := range array { - if searchedPath, _ := gspath.Search(v, path); searchedPath != "" { + if searchedPath, _ := gspath.Search(v, directoryPath); searchedPath != "" { realPath = searchedPath break } @@ -54,14 +56,20 @@ func (a *AdapterFile) SetPath(path string) (err error) { if realPath == "" { buffer := bytes.NewBuffer(nil) if a.searchPaths.Len() > 0 { - buffer.WriteString(fmt.Sprintf(`SetPath failed: cannot find directory "%s" in following paths:`, path)) + buffer.WriteString(fmt.Sprintf( + `SetPath failed: cannot find directory "%s" in following paths:`, + directoryPath, + )) a.searchPaths.RLockFunc(func(array []string) { for k, v := range array { buffer.WriteString(fmt.Sprintf("\n%d. %s", k+1, v)) } }) } else { - buffer.WriteString(fmt.Sprintf(`SetPath failed: path "%s" does not exist`, path)) + buffer.WriteString(fmt.Sprintf( + `SetPath failed: path "%s" does not exist`, + directoryPath, + )) } return gerror.New(buffer.String()) } @@ -70,7 +78,7 @@ func (a *AdapterFile) SetPath(path string) (err error) { return gerror.NewCodef( gcode.CodeInvalidParameter, `SetPath failed: path "%s" should be directory type`, - path, + directoryPath, ) } // Repeated path check. @@ -84,35 +92,37 @@ func (a *AdapterFile) SetPath(path string) (err error) { return nil } -// AddPath adds an absolute or relative path to the search paths. -func (a *AdapterFile) AddPath(paths ...string) (err error) { - for _, path := range paths { - if err = a.doAddPath(path); err != nil { +// AddPath adds an absolute or relative `directory` path to the search paths. +// +// Note that this parameter is paths to a directories not files. +func (a *AdapterFile) AddPath(directoryPaths ...string) (err error) { + for _, directoryPath := range directoryPaths { + if err = a.doAddPath(directoryPath); err != nil { return err } } return nil } -// doAddPath adds an absolute or relative path to the search paths. -func (a *AdapterFile) doAddPath(path string) (err error) { +// doAddPath adds an absolute or relative `directory` path to the search paths. +func (a *AdapterFile) doAddPath(directoryPath string) (err error) { var ( isDir = false realPath = "" ) // It firstly checks the resource manager, // and then checks the filesystem for the path. - if file := gres.Get(path); file != nil { - realPath = path + if file := gres.Get(directoryPath); file != nil { + realPath = directoryPath isDir = file.FileInfo().IsDir() } else { // Absolute path. - realPath = gfile.RealPath(path) + realPath = gfile.RealPath(directoryPath) if realPath == "" { // Relative path. a.searchPaths.RLockFunc(func(array []string) { for _, v := range array { - if searchedPath, _ := gspath.Search(v, path); searchedPath != "" { + if searchedPath, _ := gspath.Search(v, directoryPath); searchedPath != "" { realPath = searchedPath break } @@ -126,19 +136,29 @@ func (a *AdapterFile) doAddPath(path string) (err error) { if realPath == "" { buffer := bytes.NewBuffer(nil) if a.searchPaths.Len() > 0 { - buffer.WriteString(fmt.Sprintf(`AddPath failed: cannot find directory "%s" in following paths:`, path)) + buffer.WriteString(fmt.Sprintf( + `AddPath failed: cannot find directory "%s" in following paths:`, + directoryPath, + )) a.searchPaths.RLockFunc(func(array []string) { for k, v := range array { buffer.WriteString(fmt.Sprintf("\n%d. %s", k+1, v)) } }) } else { - buffer.WriteString(fmt.Sprintf(`AddPath failed: path "%s" does not exist`, path)) + buffer.WriteString(fmt.Sprintf( + `AddPath failed: path "%s" does not exist`, + directoryPath, + )) } return gerror.New(buffer.String()) } if !isDir { - return gerror.NewCodef(gcode.CodeInvalidParameter, `AddPath failed: path "%s" should be directory type`, path) + return gerror.NewCodef( + gcode.CodeInvalidParameter, + `AddPath failed: path "%s" should be directory type`, + directoryPath, + ) } // Repeated path check. if a.searchPaths.Search(realPath) != -1 { @@ -149,7 +169,7 @@ func (a *AdapterFile) doAddPath(path string) (err error) { return nil } -// GetPaths returns the searching path array of current configuration manager. +// GetPaths returns the searching directory path array of current configuration manager. func (a *AdapterFile) GetPaths() []string { return a.searchPaths.Slice() } @@ -157,7 +177,7 @@ func (a *AdapterFile) GetPaths() []string { // doGetFilePath 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 (a *AdapterFile) doGetFilePath(fileName string) (path string) { +func (a *AdapterFile) doGetFilePath(fileName string) (filePath string) { var ( tempPath string resFile *gres.File @@ -170,7 +190,7 @@ func (a *AdapterFile) doGetFilePath(fileName string) (path string) { if resFile = gres.Get(tempPath); resFile != nil { fileInfo, _ = resFile.Stat() if fileInfo != nil && !fileInfo.IsDir() { - path = resFile.Name() + filePath = resFile.Name() return } } @@ -182,7 +202,7 @@ func (a *AdapterFile) doGetFilePath(fileName string) (path string) { if resFile = gres.Get(tempPath); resFile != nil { fileInfo, _ = resFile.Stat() if fileInfo != nil && !fileInfo.IsDir() { - path = resFile.Name() + filePath = resFile.Name() return } } @@ -194,9 +214,9 @@ func (a *AdapterFile) doGetFilePath(fileName string) (path string) { a.autoCheckAndAddMainPkgPathToSearchPaths() // Searching local file system. - if path == "" { + if filePath == "" { // Absolute path. - if path = gfile.RealPath(fileName); path != "" && !gfile.IsDir(path) { + if filePath = gfile.RealPath(fileName); filePath != "" && !gfile.IsDir(filePath) { return } a.searchPaths.RLockFunc(func(array []string) { @@ -207,7 +227,8 @@ func (a *AdapterFile) doGetFilePath(fileName string) (path string) { gfile.Join(tryFolder, fileName), `\/`, ) - if path, _ = gspath.Search(searchPath, relativePath); path != "" && !gfile.IsDir(path) { + if filePath, _ = gspath.Search(searchPath, relativePath); filePath != "" && + !gfile.IsDir(filePath) { return } } @@ -220,7 +241,7 @@ func (a *AdapterFile) doGetFilePath(fileName string) (path string) { // 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 (a *AdapterFile) GetFilePath(fileName ...string) (path string, err error) { +func (a *AdapterFile) GetFilePath(fileName ...string) (filePath string, err error) { var ( fileExtName string tempFileName string @@ -230,19 +251,19 @@ func (a *AdapterFile) GetFilePath(fileName ...string) (path string, err error) { usedFileName = fileName[0] } fileExtName = gfile.ExtName(usedFileName) - if path = a.doGetFilePath(usedFileName); (path == "" || gfile.IsDir(path)) && !gstr.InArray(supportedFileTypes, fileExtName) { + if filePath = a.doGetFilePath(usedFileName); (filePath == "" || gfile.IsDir(filePath)) && !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. for _, fileType := range supportedFileTypes { tempFileName = fmt.Sprintf(`%s.%s`, usedFileName, fileType) - if path = a.doGetFilePath(tempFileName); path != "" { + if filePath = a.doGetFilePath(tempFileName); filePath != "" { break } } } - // If it cannot find the path of `file`, it formats and returns a detailed error. - if path == "" { + // If it cannot find the filePath of `file`, it formats and returns a detailed error. + if filePath == "" { var buffer = bytes.NewBuffer(nil) if a.searchPaths.Len() > 0 { if !gstr.InArray(supportedFileTypes, fileExtName) { @@ -270,7 +291,7 @@ func (a *AdapterFile) GetFilePath(fileName ...string) (path string, err error) { } }) } else { - buffer.WriteString(fmt.Sprintf(`cannot find config file "%s" with no path configured`, usedFileName)) + buffer.WriteString(fmt.Sprintf(`cannot find config file "%s" with no filePath configured`, usedFileName)) } err = gerror.NewCode(gcode.CodeNotFound, buffer.String()) }