diff --git a/.example/other/test.go b/.example/other/test.go index bf92aac69..fe4220413 100644 --- a/.example/other/test.go +++ b/.example/other/test.go @@ -1,13 +1,7 @@ package main -import "fmt" - func main() { for i := 0; i < 10; i++ { - fmt.Println(i) - switch i { - case 5: - break - } + } } diff --git a/encoding/gjson/gjson_api.go b/encoding/gjson/gjson_api.go index f50fbda44..5c36c1eec 100644 --- a/encoding/gjson/gjson_api.go +++ b/encoding/gjson/gjson_api.go @@ -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{} diff --git a/encoding/gjson/gjson_z_unit_basic_test.go b/encoding/gjson/gjson_z_unit_basic_test.go index 071fbb3ae..bdcc5c847 100644 --- a/encoding/gjson/gjson_z_unit_basic_test.go +++ b/encoding/gjson/gjson_z_unit_basic_test.go @@ -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) diff --git a/os/gcfg/gcfg.go b/os/gcfg/gcfg.go index a682ff9cc..f59338842 100644 --- a/os/gcfg/gcfg.go +++ b/os/gcfg/gcfg.go @@ -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. diff --git a/os/gcfg/gcfg_api.go b/os/gcfg/gcfg_api.go index 95633dfec..81d61f1c8 100644 --- a/os/gcfg/gcfg_api.go +++ b/os/gcfg/gcfg_api.go @@ -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() + } +} diff --git a/os/gcfg/gcfg_z_unit_test.go b/os/gcfg/gcfg_z_unit_test.go index 01012b3ea..69cf5c1e1 100644 --- a/os/gcfg/gcfg_z_unit_test.go +++ b/os/gcfg/gcfg_z_unit_test.go @@ -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}})