mirror of
https://gitee.com/johng/gf.git
synced 2024-11-30 03:07:45 +08:00
add Set function for AdapterFile for package gcfg
This commit is contained in:
parent
c5d80a2192
commit
5f3a525d11
@ -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`)
|
||||
})
|
||||
}
|
||||
|
@ -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()
|
||||
|
@ -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")
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user