From 5f3a525d1186afdf727465b338eb4cc479cbd3b9 Mon Sep 17 00:00:00 2001 From: John Guo Date: Thu, 17 Mar 2022 21:41:10 +0800 Subject: [PATCH] add Set function for AdapterFile for package gcfg --- frame/gins/gins_z_unit_server_test.go | 72 +++++++++++++----------- os/gcfg/gcfg_adapter_file.go | 14 +++++ os/gcfg/gcfg_z_unit_adapter_file_test.go | 20 +++++++ 3 files changed, 72 insertions(+), 34 deletions(-) diff --git a/frame/gins/gins_z_unit_server_test.go b/frame/gins/gins_z_unit_server_test.go index 9cbf1d2c6..8d1d304ac 100644 --- a/frame/gins/gins_z_unit_server_test.go +++ b/frame/gins/gins_z_unit_server_test.go @@ -6,37 +6,41 @@ package gins -//func Test_Server(t *testing.T) { -// gtest.C(t, func(t *gtest.T) { -// var ( -// config = Config().GetAdapter().(*gcfg.AdapterFile) -// searchingPaths = config.GetPaths() -// serverConfigDir = gtest.DataPath("server") -// ) -// t.AssertNE(serverConfigDir, "") -// t.AssertNil(config.SetPath(serverConfigDir)) -// defer func() { -// t.AssertNil(config.SetPath(searchingPaths[0])) -// if len(searchingPaths) > 1 { -// t.AssertNil(config.AddPath(searchingPaths[1:]...)) -// } -// }() -// -// localInstances.Clear() -// defer localInstances.Clear() -// -// config.Clear() -// defer config.Clear() -// -// s := Server("tempByInstanceName") -// s.BindHandler("/", func(r *ghttp.Request) { -// r.Response.Write("hello") -// }) -// s.SetDumpRouterMap(false) -// t.AssertNil(s.Start()) -// defer t.AssertNil(s.Shutdown()) -// -// content := HttpClient().GetContent(gctx.New(), `http://127.0.0.1:8003/`) -// t.Assert(content, `hello`) -// }) -//} +import ( + "testing" + "time" + + "github.com/gogf/gf/v2/net/ghttp" + "github.com/gogf/gf/v2/os/gcfg" + "github.com/gogf/gf/v2/os/gctx" + "github.com/gogf/gf/v2/os/gfile" + "github.com/gogf/gf/v2/test/gtest" +) + +func Test_Server(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + var ( + path = gcfg.DefaultConfigFileName + serverConfigContent = gtest.DataContent("server", "config.yaml") + err = gfile.PutContents(path, serverConfigContent) + ) + t.AssertNil(err) + defer gfile.Remove(path) + + time.Sleep(time.Second) + + localInstances.Clear() + defer localInstances.Clear() + + s := Server("tempByInstanceName") + s.BindHandler("/", func(r *ghttp.Request) { + r.Response.Write("hello") + }) + s.SetDumpRouterMap(false) + t.AssertNil(s.Start()) + defer t.AssertNil(s.Shutdown()) + + content := HttpClient().GetContent(gctx.New(), `http://127.0.0.1:8003/`) + t.Assert(content, `hello`) + }) +} diff --git a/os/gcfg/gcfg_adapter_file.go b/os/gcfg/gcfg_adapter_file.go index c7d42ccbe..a7cc0d0b5 100644 --- a/os/gcfg/gcfg_adapter_file.go +++ b/os/gcfg/gcfg_adapter_file.go @@ -146,6 +146,20 @@ func (c *AdapterFile) Get(ctx context.Context, pattern string) (value interface{ return nil, nil } +// 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 *AdapterFile) Set(pattern string, value interface{}) error { + j, err := c.getJson() + if err != nil { + return err + } + if j != nil { + return j.Set(pattern, value) + } + return nil +} + // Data retrieves and returns all configuration data as map type. func (c *AdapterFile) Data(ctx context.Context) (data map[string]interface{}, err error) { j, err := c.getJson() diff --git a/os/gcfg/gcfg_z_unit_adapter_file_test.go b/os/gcfg/gcfg_z_unit_adapter_file_test.go index c0036a9f0..c1e896b0f 100644 --- a/os/gcfg/gcfg_z_unit_adapter_file_test.go +++ b/os/gcfg/gcfg_z_unit_adapter_file_test.go @@ -12,6 +12,7 @@ import ( "testing" "github.com/gogf/gf/v2/os/gcfg" + "github.com/gogf/gf/v2/os/gfile" "github.com/gogf/gf/v2/test/gtest" ) @@ -99,3 +100,22 @@ func TestAdapterFile_With_UTF8_BOM(t *testing.T) { t.Assert(c.MustGet(ctx, "test.testStr"), "test") }) } + +func TestAdapterFile_Set(t *testing.T) { + config := `log-path = "logs"` + gtest.C(t, func(t *gtest.T) { + var ( + path = gcfg.DefaultConfigFileName + err = gfile.PutContents(path, config) + ) + t.Assert(err, nil) + defer gfile.Remove(path) + + c, err := gcfg.New() + t.Assert(c.MustGet(ctx, "log-path").String(), "logs") + + err = c.GetAdapter().(*gcfg.AdapterFile).Set("log-path", "custom-logs") + t.Assert(err, nil) + t.Assert(c.MustGet(ctx, "log-path").String(), "custom-logs") + }) +}