remove function Set for package gcfg

This commit is contained in:
John Guo
2022-02-14 17:20:27 +08:00
parent 887cad3b96
commit 01ba56c38b
2 changed files with 9 additions and 53 deletions

View File

@ -11,7 +11,6 @@ import (
"context"
"fmt"
"github.com/gogf/gf/v2/container/gmap"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
@ -24,7 +23,6 @@ import (
// Config is the configuration management object.
type Config struct {
adapter Adapter
dataMap *gmap.StrAnyMap
}
const (
@ -39,7 +37,6 @@ func New() (*Config, error) {
}
return &Config{
adapter: adapterFile,
dataMap: gmap.NewStrAnyMap(true),
}, nil
}
@ -47,7 +44,6 @@ func New() (*Config, error) {
func NewWithAdapter(adapter Adapter) *Config {
return &Config{
adapter: adapter,
dataMap: gmap.NewStrAnyMap(true),
}
}
@ -103,13 +99,6 @@ func (c *Config) Available(ctx context.Context, resource ...string) (ok bool) {
return c.adapter.Available(ctx, resource...)
}
// 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 *Config) Set(ctx context.Context, pattern string, value interface{}) {
c.dataMap.Set(pattern, value)
}
// Get retrieves and returns value by specified `pattern`.
// It returns all values of current Json object if `pattern` is given empty or string ".".
// It returns nil if no value found by `pattern`.
@ -120,17 +109,15 @@ func (c *Config) Get(ctx context.Context, pattern string, def ...interface{}) (*
err error
value interface{}
)
if value = c.dataMap.Get(pattern); value == nil {
value, err = c.adapter.Get(ctx, pattern)
if err != nil {
return nil, err
}
if value == nil {
if len(def) > 0 {
return gvar.New(def[0]), nil
}
return nil, nil
value, err = c.adapter.Get(ctx, pattern)
if err != nil {
return nil, err
}
if value == nil {
if len(def) > 0 {
return gvar.New(def[0]), nil
}
return nil, nil
}
return gvar.New(value), nil
}
@ -181,19 +168,7 @@ func (c *Config) GetWithCmd(ctx context.Context, pattern string, def ...interfac
// Data retrieves and returns all configuration data as map type.
func (c *Config) Data(ctx context.Context) (data map[string]interface{}, err error) {
adapterData, err := c.adapter.Data(ctx)
if err != nil {
return nil, err
}
data = make(map[string]interface{})
for k, v := range adapterData {
data[k] = v
}
c.dataMap.Iterator(func(k string, v interface{}) bool {
data[k] = v
return true
})
return
return c.adapter.Data(ctx)
}
// MustGet acts as function Get, but it panics if error occurs.

View File

@ -146,25 +146,6 @@ func Test_SetFileName(t *testing.T) {
})
}
func TestCfg_Set(t *testing.T) {
config := `log-path = "logs"`
gtest.C(t, func(t *gtest.T) {
path := gcfg.DefaultConfigFile
err := gfile.PutContents(path, config)
t.Assert(err, nil)
defer gfile.Remove(path)
adapterFile, err := gcfg.NewAdapterFile()
t.AssertNil(err)
t.Assert(adapterFile.MustGet(ctx, "log-path"), "logs")
c := gcfg.NewWithAdapter(adapterFile)
c.Set(ctx, "log-path", "custom-logs")
t.Assert(err, nil)
t.Assert(c.MustGet(ctx, "log-path"), "custom-logs")
})
}
func TestCfg_Get_WrongConfigFile(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var err error