From d716037caa02f2c99a1dcc4ea9e028e809104ab8 Mon Sep 17 00:00:00 2001 From: John Date: Sun, 15 Mar 2020 19:56:07 +0800 Subject: [PATCH] improve package gcfg adding default configuration file searching for instance --- os/gcfg/gcfg.go | 24 +++++++++++------------- os/gcfg/gcfg_instance.go | 14 +++++++++++--- os/gcfg/gcfg_z_unit_test.go | 9 +++++++++ os/gcfg/testdata/c1.toml | 2 ++ os/gcfg/testdata/folder1/c1.toml | 2 ++ 5 files changed, 35 insertions(+), 16 deletions(-) create mode 100644 os/gcfg/testdata/c1.toml create mode 100644 os/gcfg/testdata/folder1/c1.toml diff --git a/os/gcfg/gcfg.go b/os/gcfg/gcfg.go index d3a16be53..e7aac4378 100644 --- a/os/gcfg/gcfg.go +++ b/os/gcfg/gcfg.go @@ -17,7 +17,6 @@ import ( "github.com/gogf/gf/container/garray" "github.com/gogf/gf/container/gmap" - "github.com/gogf/gf/container/gtype" "github.com/gogf/gf/encoding/gjson" "github.com/gogf/gf/internal/cmdenv" "github.com/gogf/gf/os/gfile" @@ -33,10 +32,10 @@ const ( // Configuration struct. type Config struct { - name *gtype.String // Default configuration file name. + name string // Default configuration file name. paths *garray.StrArray // Searching path array. jsons *gmap.StrAnyMap // The pared JSON objects for configuration files. - vc *gtype.Bool // Whether do violence check in value index searching. It affects the performance when set true(false in default). + vc bool // Whether do violence check in value index searching. It affects the performance when set true(false in default). } var ( @@ -51,10 +50,9 @@ func New(file ...string) *Config { name = file[0] } c := &Config{ - name: gtype.NewString(name), + name: name, paths: garray.NewStrArray(true), jsons: gmap.NewStrAnyMap(true), - vc: gtype.NewBool(), } // Customized dir path from env/cmd. if envPath := cmdenv.Get("gf.gcfg.path").String(); envPath != "" { @@ -82,7 +80,7 @@ func New(file ...string) *Config { // filePath returns the absolute configuration file path for the given filename by . func (c *Config) filePath(file ...string) (path string) { - name := c.name.Val() + name := c.name if len(file) > 0 { name = file[0] } @@ -182,7 +180,7 @@ func (c *Config) SetPath(path string) error { // and it is not recommended to allow separators in the key names. // It is best to avoid this on the application side. func (c *Config) SetViolenceCheck(check bool) { - c.vc.Set(check) + c.vc = check c.Clear() } @@ -250,7 +248,7 @@ func (c *Config) AddPath(path string) error { // If the specified configuration file does not exist, // an empty string is returned. func (c *Config) FilePath(file ...string) (path string) { - name := c.name.Val() + name := c.name if len(file) > 0 { name = file[0] } @@ -290,13 +288,13 @@ func (c *Config) FilePath(file ...string) (path string) { // SetFileName sets the default configuration file name. func (c *Config) SetFileName(name string) *Config { - c.name.Set(name) + c.name = name return c } // GetFileName returns the default configuration file name. func (c *Config) GetFileName() string { - return c.name.Val() + return c.name } // Available checks and returns whether configuration of given is available. @@ -305,7 +303,7 @@ func (c *Config) Available(file ...string) bool { if len(file) > 0 && file[0] != "" { name = file[0] } else { - name = c.name.Val() + name = c.name } if c.FilePath(name) != "" { return true @@ -323,7 +321,7 @@ func (c *Config) getJson(file ...string) *gjson.Json { if len(file) > 0 && file[0] != "" { name = file[0] } else { - name = c.name.Val() + name = c.name } r := c.jsons.GetOrSetFuncLock(name, func() interface{} { content := "" @@ -340,7 +338,7 @@ func (c *Config) getJson(file ...string) *gjson.Json { } } if j, err := gjson.LoadContent(content, true); err == nil { - j.SetViolenceCheck(c.vc.Val()) + j.SetViolenceCheck(c.vc) // Add monitor for this configuration file, // any changes of this file will refresh its cache in Config object. if filePath != "" && !gres.Contains(filePath) { diff --git a/os/gcfg/gcfg_instance.go b/os/gcfg/gcfg_instance.go index 8484e5d12..9ef0fc951 100644 --- a/os/gcfg/gcfg_instance.go +++ b/os/gcfg/gcfg_instance.go @@ -7,6 +7,7 @@ package gcfg import ( + "fmt" "github.com/gogf/gf/container/gmap" ) @@ -16,18 +17,25 @@ const ( ) var ( - // Instances map. + // Instances map containing configuration instances. instances = gmap.NewStrAnyMap(true) ) // Instance returns an instance of Config with default settings. -// The parameter is the name for the instance. +// The parameter 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 { key := DEFAULT_NAME if len(name) > 0 && name[0] != "" { key = name[0] } return instances.GetOrSetFuncLock(key, func() interface{} { - return New() + c := New() + file := fmt.Sprintf(`%s.toml`, key) + if c.Available(file) { + c.SetFileName(file) + } + return c }).(*Config) } diff --git a/os/gcfg/gcfg_z_unit_test.go b/os/gcfg/gcfg_z_unit_test.go index 5df2a0066..924e09959 100644 --- a/os/gcfg/gcfg_z_unit_test.go +++ b/os/gcfg/gcfg_z_unit_test.go @@ -9,6 +9,7 @@ package gcfg_test import ( + "github.com/gogf/gf/debug/gdebug" "io/ioutil" "os" "testing" @@ -420,6 +421,14 @@ func TestCfg_Instance(t *testing.T) { gtest.Case(t, func() { gtest.Assert(gcfg.Instance("gf") != nil, true) }) + gtest.Case(t, func() { + pwd := gfile.Pwd() + gfile.Chdir(gfile.Join(gdebug.CallerDirectory(), "testdata")) + defer gfile.Chdir(pwd) + gtest.Assert(gcfg.Instance("c1") != nil, true) + gtest.Assert(gcfg.Instance("c1").Get("my-config"), "1") + gtest.Assert(gcfg.Instance("folder1/c1").Get("my-config"), "2") + }) } func TestCfg_Config(t *testing.T) { diff --git a/os/gcfg/testdata/c1.toml b/os/gcfg/testdata/c1.toml new file mode 100644 index 000000000..0ebee44aa --- /dev/null +++ b/os/gcfg/testdata/c1.toml @@ -0,0 +1,2 @@ + +my-config = "1" \ No newline at end of file diff --git a/os/gcfg/testdata/folder1/c1.toml b/os/gcfg/testdata/folder1/c1.toml new file mode 100644 index 000000000..eff89f9f5 --- /dev/null +++ b/os/gcfg/testdata/folder1/c1.toml @@ -0,0 +1,2 @@ + +my-config = "2" \ No newline at end of file