mirror of
https://gitee.com/johng/gf.git
synced 2024-12-02 04:07:47 +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
|
package gins
|
||||||
|
|
||||||
//func Test_Server(t *testing.T) {
|
import (
|
||||||
// gtest.C(t, func(t *gtest.T) {
|
"testing"
|
||||||
// var (
|
"time"
|
||||||
// config = Config().GetAdapter().(*gcfg.AdapterFile)
|
|
||||||
// searchingPaths = config.GetPaths()
|
"github.com/gogf/gf/v2/net/ghttp"
|
||||||
// serverConfigDir = gtest.DataPath("server")
|
"github.com/gogf/gf/v2/os/gcfg"
|
||||||
// )
|
"github.com/gogf/gf/v2/os/gctx"
|
||||||
// t.AssertNE(serverConfigDir, "")
|
"github.com/gogf/gf/v2/os/gfile"
|
||||||
// t.AssertNil(config.SetPath(serverConfigDir))
|
"github.com/gogf/gf/v2/test/gtest"
|
||||||
// defer func() {
|
)
|
||||||
// t.AssertNil(config.SetPath(searchingPaths[0]))
|
|
||||||
// if len(searchingPaths) > 1 {
|
func Test_Server(t *testing.T) {
|
||||||
// t.AssertNil(config.AddPath(searchingPaths[1:]...))
|
gtest.C(t, func(t *gtest.T) {
|
||||||
// }
|
var (
|
||||||
// }()
|
path = gcfg.DefaultConfigFileName
|
||||||
//
|
serverConfigContent = gtest.DataContent("server", "config.yaml")
|
||||||
// localInstances.Clear()
|
err = gfile.PutContents(path, serverConfigContent)
|
||||||
// defer localInstances.Clear()
|
)
|
||||||
//
|
t.AssertNil(err)
|
||||||
// config.Clear()
|
defer gfile.Remove(path)
|
||||||
// defer config.Clear()
|
|
||||||
//
|
time.Sleep(time.Second)
|
||||||
// s := Server("tempByInstanceName")
|
|
||||||
// s.BindHandler("/", func(r *ghttp.Request) {
|
localInstances.Clear()
|
||||||
// r.Response.Write("hello")
|
defer localInstances.Clear()
|
||||||
// })
|
|
||||||
// s.SetDumpRouterMap(false)
|
s := Server("tempByInstanceName")
|
||||||
// t.AssertNil(s.Start())
|
s.BindHandler("/", func(r *ghttp.Request) {
|
||||||
// defer t.AssertNil(s.Shutdown())
|
r.Response.Write("hello")
|
||||||
//
|
})
|
||||||
// content := HttpClient().GetContent(gctx.New(), `http://127.0.0.1:8003/`)
|
s.SetDumpRouterMap(false)
|
||||||
// t.Assert(content, `hello`)
|
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
|
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.
|
// Data retrieves and returns all configuration data as map type.
|
||||||
func (c *AdapterFile) Data(ctx context.Context) (data map[string]interface{}, err error) {
|
func (c *AdapterFile) Data(ctx context.Context) (data map[string]interface{}, err error) {
|
||||||
j, err := c.getJson()
|
j, err := c.getJson()
|
||||||
|
@ -12,6 +12,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/gogf/gf/v2/os/gcfg"
|
"github.com/gogf/gf/v2/os/gcfg"
|
||||||
|
"github.com/gogf/gf/v2/os/gfile"
|
||||||
"github.com/gogf/gf/v2/test/gtest"
|
"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")
|
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