improve gcfg: it returns all values if pattern is '.'; add more functions for gcfg

This commit is contained in:
John 2019-08-07 16:58:01 +08:00
parent 4d5814fc43
commit 352e4c088a
6 changed files with 97 additions and 25 deletions

View File

@ -1,13 +1,7 @@
package main
import "fmt"
func main() {
for i := 0; i < 10; i++ {
fmt.Println(i)
switch i {
case 5:
break
}
}
}

View File

@ -43,8 +43,14 @@ func (j *Json) Get(pattern string, def ...interface{}) interface{} {
j.mu.RLock()
defer j.mu.RUnlock()
// It returns nil if pattern is empty.
if pattern == "" {
return nil
}
// It returns all if pattern is ".".
if pattern == "." {
pattern = ""
return *j.p
}
var result *interface{}

View File

@ -371,10 +371,11 @@ func Test_Basic(t *testing.T) {
gtest.Case(t, func() {
j := gjson.New(`{"name":"gf","time":"2019-06-12"}`)
j.SetViolenceCheck(true)
gtest.Assert(j.Get("").(g.Map)["name"], "gf")
gtest.Assert(j.Get("").(g.Map)["name1"], nil)
gtest.Assert(j.Get(""), nil)
gtest.Assert(j.Get(".").(g.Map)["name"], "gf")
gtest.Assert(j.Get(".").(g.Map)["name1"], nil)
j.SetViolenceCheck(false)
gtest.Assert(j.Get("").(g.Map)["name"], "gf")
gtest.Assert(j.Get(".").(g.Map)["name"], "gf")
err := j.Set("name", "gf1")
gtest.Assert(err, nil)

View File

@ -246,8 +246,9 @@ func (c *Config) FilePath(file ...string) (path string) {
}
// SetFileName sets the default configuration file name.
func (c *Config) SetFileName(name string) {
func (c *Config) SetFileName(name string) *Config {
c.name.Set(name)
return c
}
// GetFileName returns the default configuration file name.

View File

@ -230,56 +230,56 @@ func (c *Config) GetStruct(pattern string, pointer interface{}, mapping ...map[s
if j := c.getJson(); j != nil {
return j.GetStruct(pattern, pointer, mapping...)
}
return errors.New("config file not found")
return errors.New("configuration not found")
}
func (c *Config) GetStructDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.GetStructDeep(pattern, pointer, mapping...)
}
return errors.New("config file not found")
return errors.New("configuration not found")
}
func (c *Config) GetStructs(pattern string, pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.GetStructs(pattern, pointer, mapping...)
}
return errors.New("config file not found")
return errors.New("configuration not found")
}
func (c *Config) GetStructsDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.GetStructsDeep(pattern, pointer, mapping...)
}
return errors.New("config file not found")
return errors.New("configuration not found")
}
func (c *Config) GetMapStruct(pattern string, pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.GetMapStruct(pattern, pointer, mapping...)
}
return errors.New("config file not found")
return errors.New("configuration not found")
}
func (c *Config) GetMapStructDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.GetMapStructDeep(pattern, pointer, mapping...)
}
return errors.New("config file not found")
return errors.New("configuration not found")
}
func (c *Config) GetMapStructs(pattern string, pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.GetMapStructs(pattern, pointer, mapping...)
}
return errors.New("config file not found")
return errors.New("configuration not found")
}
func (c *Config) GetMapStructsDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.GetMapStructsDeep(pattern, pointer, mapping...)
}
return errors.New("config file not found")
return errors.New("configuration not found")
}
// Deprecated.
@ -287,10 +287,74 @@ func (c *Config) GetToStruct(pattern string, pointer interface{}) error {
return c.GetStruct(pattern, pointer)
}
// Reload is alias of Clear.
// Deprecated.
func (c *Config) Reload() {
c.jsons.Clear()
func (c *Config) ToMap() map[string]interface{} {
if j := c.getJson(); j != nil {
return j.ToMap()
}
return nil
}
func (c *Config) ToArray() []interface{} {
if j := c.getJson(); j != nil {
return j.ToArray()
}
return nil
}
func (c *Config) ToStruct(pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.ToStruct(pointer, mapping...)
}
return errors.New("configuration not found")
}
func (c *Config) ToStructDeep(pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.ToStructDeep(pointer, mapping...)
}
return errors.New("configuration not found")
}
func (c *Config) ToStructs(pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.ToStructs(pointer, mapping...)
}
return errors.New("configuration not found")
}
func (c *Config) ToStructsDeep(pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.ToStructsDeep(pointer, mapping...)
}
return errors.New("configuration not found")
}
func (c *Config) ToMapStruct(pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.ToMapStruct(pointer, mapping...)
}
return errors.New("configuration not found")
}
func (c *Config) ToMapStructDeep(pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.ToMapStructDeep(pointer, mapping...)
}
return errors.New("configuration not found")
}
func (c *Config) ToMapStructs(pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.ToMapStructs(pointer, mapping...)
}
return errors.New("configuration not found")
}
func (c *Config) ToMapStructsDeep(pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil {
return j.ToMapStructsDeep(pointer, mapping...)
}
return errors.New("configuration not found")
}
// Clear removes all parsed configuration files content cache,
@ -298,3 +362,10 @@ func (c *Config) Reload() {
func (c *Config) Clear() {
c.jsons.Clear()
}
// Dump prints current Json object with more manually readable.
func (c *Config) Dump() {
if j := c.getJson(); j != nil {
j.Dump()
}
}

View File

@ -401,7 +401,6 @@ func TestCfg_Get(t *testing.T) {
}{}
gtest.Assert(c.GetToStruct("name", &name) == nil, false)
c.Reload()
c.Clear()
arr, _ := gjson.Encode(g.Map{"name": "gf", "time": "2019-06-12", "person": g.Map{"name": "gf"}, "floats": g.Slice{1, 2, 3}})