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 package main
import "fmt"
func main() { func main() {
for i := 0; i < 10; i++ { 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() j.mu.RLock()
defer j.mu.RUnlock() defer j.mu.RUnlock()
// It returns nil if pattern is empty.
if pattern == "" {
return nil
}
// It returns all if pattern is ".".
if pattern == "." { if pattern == "." {
pattern = "" return *j.p
} }
var result *interface{} var result *interface{}

View File

@ -371,10 +371,11 @@ func Test_Basic(t *testing.T) {
gtest.Case(t, func() { gtest.Case(t, func() {
j := gjson.New(`{"name":"gf","time":"2019-06-12"}`) j := gjson.New(`{"name":"gf","time":"2019-06-12"}`)
j.SetViolenceCheck(true) j.SetViolenceCheck(true)
gtest.Assert(j.Get("").(g.Map)["name"], "gf") gtest.Assert(j.Get(""), nil)
gtest.Assert(j.Get("").(g.Map)["name1"], nil) gtest.Assert(j.Get(".").(g.Map)["name"], "gf")
gtest.Assert(j.Get(".").(g.Map)["name1"], nil)
j.SetViolenceCheck(false) j.SetViolenceCheck(false)
gtest.Assert(j.Get("").(g.Map)["name"], "gf") gtest.Assert(j.Get(".").(g.Map)["name"], "gf")
err := j.Set("name", "gf1") err := j.Set("name", "gf1")
gtest.Assert(err, nil) 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. // SetFileName sets the default configuration file name.
func (c *Config) SetFileName(name string) { func (c *Config) SetFileName(name string) *Config {
c.name.Set(name) c.name.Set(name)
return c
} }
// GetFileName returns the default configuration file name. // 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 { if j := c.getJson(); j != nil {
return j.GetStruct(pattern, pointer, mapping...) 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 { func (c *Config) GetStructDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil { if j := c.getJson(); j != nil {
return j.GetStructDeep(pattern, pointer, mapping...) 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 { func (c *Config) GetStructs(pattern string, pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil { if j := c.getJson(); j != nil {
return j.GetStructs(pattern, pointer, mapping...) 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 { func (c *Config) GetStructsDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil { if j := c.getJson(); j != nil {
return j.GetStructsDeep(pattern, pointer, mapping...) 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 { func (c *Config) GetMapStruct(pattern string, pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil { if j := c.getJson(); j != nil {
return j.GetMapStruct(pattern, pointer, mapping...) 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 { func (c *Config) GetMapStructDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil { if j := c.getJson(); j != nil {
return j.GetMapStructDeep(pattern, pointer, mapping...) 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 { func (c *Config) GetMapStructs(pattern string, pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil { if j := c.getJson(); j != nil {
return j.GetMapStructs(pattern, pointer, mapping...) 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 { func (c *Config) GetMapStructsDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
if j := c.getJson(); j != nil { if j := c.getJson(); j != nil {
return j.GetMapStructsDeep(pattern, pointer, mapping...) return j.GetMapStructsDeep(pattern, pointer, mapping...)
} }
return errors.New("config file not found") return errors.New("configuration not found")
} }
// Deprecated. // Deprecated.
@ -287,10 +287,74 @@ func (c *Config) GetToStruct(pattern string, pointer interface{}) error {
return c.GetStruct(pattern, pointer) return c.GetStruct(pattern, pointer)
} }
// Reload is alias of Clear. func (c *Config) ToMap() map[string]interface{} {
// Deprecated. if j := c.getJson(); j != nil {
func (c *Config) Reload() { return j.ToMap()
c.jsons.Clear() }
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, // Clear removes all parsed configuration files content cache,
@ -298,3 +362,10 @@ func (c *Config) Reload() {
func (c *Config) Clear() { func (c *Config) Clear() {
c.jsons.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) gtest.Assert(c.GetToStruct("name", &name) == nil, false)
c.Reload()
c.Clear() c.Clear()
arr, _ := gjson.Encode(g.Map{"name": "gf", "time": "2019-06-12", "person": g.Map{"name": "gf"}, "floats": g.Slice{1, 2, 3}}) arr, _ := gjson.Encode(g.Map{"name": "gf", "time": "2019-06-12", "person": g.Map{"name": "gf"}, "floats": g.Slice{1, 2, 3}})