From a98ad9577bcfb77f276eb6cdf1a8bf36a9c4a2cb Mon Sep 17 00:00:00 2001 From: John Date: Mon, 29 Jul 2019 20:37:49 +0800 Subject: [PATCH] improve gjson/gparser/gcfg/gvar --- g/container/gvar/gvar.go | 27 +- g/encoding/gjson/gjson_api.go | 4 + g/encoding/gjson/gjson_func.go | 63 ---- g/encoding/gparser/gparser.go | 5 +- g/encoding/gparser/gparser_api.go | 240 -------------- g/encoding/gparser/gparser_api_config.go | 17 - g/encoding/gparser/gparser_api_encoding.go | 41 --- g/encoding/gparser/gparser_api_new_load.go | 30 +- g/encoding/gparser/gparser_unit_basic_test.go | 7 +- g/os/gcfg/gcfg.go | 230 -------------- g/os/gcfg/gcfg_api.go | 300 ++++++++++++++++++ 11 files changed, 353 insertions(+), 611 deletions(-) delete mode 100644 g/encoding/gjson/gjson_func.go delete mode 100644 g/encoding/gparser/gparser_api.go delete mode 100644 g/encoding/gparser/gparser_api_config.go create mode 100644 g/os/gcfg/gcfg_api.go diff --git a/g/container/gvar/gvar.go b/g/container/gvar/gvar.go index 1b2dfac9d..68101eeb4 100644 --- a/g/container/gvar/gvar.go +++ b/g/container/gvar/gvar.go @@ -9,9 +9,10 @@ package gvar import ( "encoding/json" - "github.com/gogf/gf/g/internal/empty" "time" + "github.com/gogf/gf/g/internal/empty" + "github.com/gogf/gf/g/container/gtype" "github.com/gogf/gf/g/os/gtime" "github.com/gogf/gf/g/util/gconv" @@ -224,3 +225,27 @@ func (v *Var) Structs(pointer interface{}, mapping ...map[string]string) (err er func (v *Var) StructsDeep(pointer interface{}, mapping ...map[string]string) (err error) { return gconv.StructsDeep(v.Val(), pointer, mapping...) } + +// MapStruct converts map type variable to another map type variable . +// The elements of should be type of struct/*struct. +func (v *Var) MapStruct(pointer interface{}, mapping ...map[string]string) (err error) { + return gconv.MapStruct(v.Val(), pointer, mapping...) +} + +// MapStructDeep recursively converts map type variable to another map type variable . +// The elements of should be type of struct/*struct. +func (v *Var) MapStructDeep(pointer interface{}, mapping ...map[string]string) (err error) { + return gconv.MapStructDeep(v.Val(), pointer, mapping...) +} + +// MapStructs converts map type variable to another map type variable . +// The elements of should be type of []struct/[]*struct. +func (v *Var) MapStructs(pointer interface{}, mapping ...map[string]string) (err error) { + return gconv.MapStructs(v.Val(), pointer, mapping...) +} + +// MapStructsDeep recursively converts map type variable to another map type variable . +// The elements of should be type of []struct/[]*struct. +func (v *Var) MapStructsDeep(pointer interface{}, mapping ...map[string]string) (err error) { + return gconv.MapStructsDeep(v.Val(), pointer, mapping...) +} diff --git a/g/encoding/gjson/gjson_api.go b/g/encoding/gjson/gjson_api.go index 13b6a2b3f..7d57a00c9 100644 --- a/g/encoding/gjson/gjson_api.go +++ b/g/encoding/gjson/gjson_api.go @@ -43,6 +43,10 @@ func (j *Json) Get(pattern string, def ...interface{}) interface{} { j.mu.RLock() defer j.mu.RUnlock() + if pattern == "." { + pattern = "" + } + var result *interface{} if j.vc { result = j.getPointerByPattern(pattern) diff --git a/g/encoding/gjson/gjson_func.go b/g/encoding/gjson/gjson_func.go deleted file mode 100644 index 669e13ab0..000000000 --- a/g/encoding/gjson/gjson_func.go +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved. -// -// This Source Code Form is subject to the terms of the MIT License. -// If a copy of the MIT was not distributed with this file, -// You can obtain one at https://github.com/gogf/gf. - -package gjson - -//func MarshalOrdered(value interface{}) ([]byte, error) { -// buffer := bytes.NewBuffer(nil) -// rv := reflect.ValueOf(value) -// kind := rv.Kind() -// if kind == reflect.Ptr { -// rv = rv.Elem() -// kind = rv.Kind() -// } -// switch kind { -// case reflect.Slice: fallthrough -// case reflect.Array: -// buffer.WriteByte('[') -// length := rv.Len() -// for i := 0; i < length; i++ { -// if p, err := MarshalOrdered(rv.Index(i).Interface()); err != nil { -// return nil, err -// } else { -// buffer.Write(p) -// if i < length - 1 { -// buffer.WriteByte(',') -// } -// } -// } -// buffer.WriteByte(']') -// case reflect.Map: fallthrough -// case reflect.Struct: -// m := gconv.Map(value, "json") -// keys := make([]string, len(m)) -// index := 0 -// for key := range m { -// keys[index] = key -// index++ -// } -// sort.Strings(keys) -// buffer.WriteByte('{') -// for i, key := range keys { -// if p, err := MarshalOrdered(m[key]); err != nil { -// return nil, err -// } else { -// buffer.WriteString(fmt.Sprintf(`"%s":%s`, key, string(p))) -// if i < index - 1 { -// buffer.WriteByte(',') -// } -// } -// } -// buffer.WriteByte('}') -// default: -// if p, err := json.Marshal(value); err != nil { -// return nil, err -// } else { -// buffer.Write(p) -// } -// } -// return buffer.Bytes(), nil -//} diff --git a/g/encoding/gparser/gparser.go b/g/encoding/gparser/gparser.go index b015f7728..3a33e5860 100644 --- a/g/encoding/gparser/gparser.go +++ b/g/encoding/gparser/gparser.go @@ -11,6 +11,5 @@ import ( "github.com/gogf/gf/g/encoding/gjson" ) -type Parser struct { - json *gjson.Json -} +// Parser is actually alias of gjson.Json. +type Parser = gjson.Json diff --git a/g/encoding/gparser/gparser_api.go b/g/encoding/gparser/gparser_api.go deleted file mode 100644 index 9be9f3c9b..000000000 --- a/g/encoding/gparser/gparser_api.go +++ /dev/null @@ -1,240 +0,0 @@ -// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved. -// -// This Source Code Form is subject to the terms of the MIT License. -// If a copy of the MIT was not distributed with this file, -// You can obtain one at https://gitee.com/johng/gp. - -package gparser - -import ( - "time" - - "github.com/gogf/gf/g/container/gvar" - "github.com/gogf/gf/g/os/gtime" -) - -// Val returns the value. -func (p *Parser) Value() interface{} { - return p.json.Value() -} - -// Get returns value by specified . -// It returns all values of current Json object, if is empty or not specified. -// It returns nil if no value found by . -// -// We can also access slice item by its index number in , -// eg: "items.name.first", "list.10". -// -// It returns a default value specified by if value for is not found. -func (p *Parser) Get(pattern string, def ...interface{}) interface{} { - return p.json.Get(pattern, def...) -} - -// GetVar returns a *gvar.Var with value by given . -func (p *Parser) GetVar(pattern string, def ...interface{}) *gvar.Var { - return p.json.GetVar(pattern, def...) -} - -// GetMap gets the value by specified , -// and converts it to map[string]interface{}. -func (p *Parser) GetMap(pattern string, def ...interface{}) map[string]interface{} { - return p.json.GetMap(pattern, def...) -} - -// GetArray gets the value by specified , -// and converts it to a slice of []interface{}. -func (p *Parser) GetArray(pattern string, def ...interface{}) []interface{} { - return p.json.GetArray(pattern, def...) -} - -// GetString gets the value by specified , -// and converts it to string. -func (p *Parser) GetString(pattern string, def ...interface{}) string { - return p.json.GetString(pattern, def...) -} - -// GetBool gets the value by specified , -// and converts it to bool. -// It returns false when value is: "", 0, false, off, nil; -// or returns true instead. -func (p *Parser) GetBool(pattern string, def ...interface{}) bool { - return p.json.GetBool(pattern, def...) -} - -func (p *Parser) GetInt(pattern string, def ...interface{}) int { - return p.json.GetInt(pattern, def...) -} - -func (p *Parser) GetInt8(pattern string, def ...interface{}) int8 { - return p.json.GetInt8(pattern, def...) -} - -func (p *Parser) GetInt16(pattern string, def ...interface{}) int16 { - return p.json.GetInt16(pattern, def...) -} - -func (p *Parser) GetInt32(pattern string, def ...interface{}) int32 { - return p.json.GetInt32(pattern, def...) -} - -func (p *Parser) GetInt64(pattern string, def ...interface{}) int64 { - return p.json.GetInt64(pattern, def...) -} - -func (p *Parser) GetInts(pattern string, def ...interface{}) []int { - return p.json.GetInts(pattern, def...) -} - -func (p *Parser) GetUint(pattern string, def ...interface{}) uint { - return p.json.GetUint(pattern, def...) -} - -func (p *Parser) GetUint8(pattern string, def ...interface{}) uint8 { - return p.json.GetUint8(pattern, def...) -} - -func (p *Parser) GetUint16(pattern string, def ...interface{}) uint16 { - return p.json.GetUint16(pattern, def...) -} - -func (p *Parser) GetUint32(pattern string, def ...interface{}) uint32 { - return p.json.GetUint32(pattern, def...) -} - -func (p *Parser) GetUint64(pattern string, def ...interface{}) uint64 { - return p.json.GetUint64(pattern, def...) -} - -func (p *Parser) GetFloat32(pattern string, def ...interface{}) float32 { - return p.json.GetFloat32(pattern, def...) -} - -func (p *Parser) GetFloat64(pattern string, def ...interface{}) float64 { - return p.json.GetFloat64(pattern, def...) -} - -func (p *Parser) GetFloats(pattern string, def ...interface{}) []float64 { - return p.json.GetFloats(pattern, def...) -} - -// GetStrings gets the value by specified , -// and converts it to a slice of []string. -func (p *Parser) GetStrings(pattern string, def ...interface{}) []string { - return p.json.GetStrings(pattern, def...) -} - -func (p *Parser) GetInterfaces(pattern string, def ...interface{}) []interface{} { - return p.json.GetInterfaces(pattern, def...) -} - -func (p *Parser) GetTime(pattern string, format ...string) time.Time { - return p.json.GetTime(pattern, format...) -} - -func (p *Parser) GetDuration(pattern string, def ...interface{}) time.Duration { - return p.json.GetDuration(pattern, def...) -} - -func (p *Parser) GetGTime(pattern string, format ...string) *gtime.Time { - return p.json.GetGTime(pattern, format...) -} - -// GetToVar gets the value by specified , -// and converts it to specified golang variable . -// The should be a pointer type. -// Deprecated. -func (p *Parser) GetToVar(pattern string, pointer interface{}) error { - return p.json.GetToVar(pattern, pointer) -} - -// GetStruct gets the value by specified , -// and converts it to specified object . -// The should be the pointer to an object. -func (p *Parser) GetStruct(pattern string, pointer interface{}, mapping ...map[string]string) error { - return p.json.GetStruct(pattern, pointer, mapping...) -} - -// GetStructDeep does GetStruct recursively. -func (p *Parser) GetStructDeep(pattern string, pointer interface{}, mapping ...map[string]string) error { - return p.json.GetStructDeep(pattern, pointer, mapping...) -} - -// GetStructs converts any slice to given struct slice. -func (p *Parser) GetStructs(pattern string, pointer interface{}, mapping ...map[string]string) error { - return p.json.GetStructs(pattern, pointer, mapping...) -} - -// GetStructsDeep converts any slice to given struct slice recursively. -func (p *Parser) GetStructsDeep(pattern string, pointer interface{}, mapping ...map[string]string) error { - return p.json.GetStructsDeep(pattern, pointer, mapping...) -} - -// GetToStruct is alias of GetStruct. -// Deprecated. -func (p *Parser) GetToStruct(pattern string, pointer interface{}, mapping ...map[string]string) error { - return p.json.GetStruct(pattern, pointer, mapping...) -} - -// Set sets value with specified . -// It supports hierarchical data access by char separator, which is '.' in default. -func (p *Parser) Set(pattern string, value interface{}) error { - return p.json.Set(pattern, value) -} - -// Len returns the length/size of the value by specified . -// The target value by should be type of slice or map. -// It returns -1 if the target value is not found, or its type is invalid. -func (p *Parser) Len(pattern string) int { - return p.json.Len(pattern) -} - -// Append appends value to the value by specified . -// The target value by should be type of slice. -func (p *Parser) Append(pattern string, value interface{}) error { - return p.json.Append(pattern, value) -} - -// Remove deletes value with specified . -// It supports hierarchical data access by char separator, which is '.' in default. -func (p *Parser) Remove(pattern string) error { - return p.json.Remove(pattern) -} - -// ToMap converts current object values to map[string]interface{}. -// It returns nil if fails. -func (p *Parser) ToMap() map[string]interface{} { - return p.json.ToMap() -} - -// ToArray converts current object values to []interface{}. -// It returns nil if fails. -func (p *Parser) ToArray() []interface{} { - return p.json.ToArray() -} - -// ToStruct converts current Json object to specified object. -// The should be a pointer type. -func (p *Parser) ToStruct(pointer interface{}) error { - return p.json.ToStruct(pointer) -} - -func (p *Parser) ToStructDeep(pointer interface{}) error { - return p.json.ToStructDeep(pointer) -} - -func (p *Parser) ToStructs(pointer interface{}) error { - return p.json.ToStructs(pointer) -} - -func (p *Parser) ToStructsDeep(pointer interface{}) error { - return p.json.ToStructsDeep(pointer) -} - -// Dump prints current Json object with more manually readable. -func (p *Parser) Dump() { - p.json.Dump() -} - -func (p *Parser) Export() string { - return p.json.Export() -} diff --git a/g/encoding/gparser/gparser_api_config.go b/g/encoding/gparser/gparser_api_config.go deleted file mode 100644 index ba8ec9767..000000000 --- a/g/encoding/gparser/gparser_api_config.go +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved. -// -// This Source Code Form is subject to the terms of the MIT License. -// If a copy of the MIT was not distributed with this file, -// You can obtain one at https://gitee.com/johng/gp. - -package gparser - -// SetSplitChar sets the separator char for hierarchical data access. -func (p *Parser) SetSplitChar(char byte) { - p.json.SetSplitChar(char) -} - -// SetViolenceCheck enables/disables violence check for hierarchical data access. -func (p *Parser) SetViolenceCheck(check bool) { - p.json.SetViolenceCheck(check) -} diff --git a/g/encoding/gparser/gparser_api_encoding.go b/g/encoding/gparser/gparser_api_encoding.go index efc342731..04a8f53c4 100644 --- a/g/encoding/gparser/gparser_api_encoding.go +++ b/g/encoding/gparser/gparser_api_encoding.go @@ -6,43 +6,6 @@ package gparser -// MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (p *Parser) MarshalJSON() ([]byte, error) { - return p.json.MarshalJSON() -} - -func (p *Parser) ToXml(rootTag ...string) ([]byte, error) { - return p.json.ToXml(rootTag...) -} - -func (p *Parser) ToXmlIndent(rootTag ...string) ([]byte, error) { - return p.json.ToXmlIndent(rootTag...) -} - -func (p *Parser) ToJson() ([]byte, error) { - return p.json.ToJson() -} - -func (p *Parser) ToJsonString() (string, error) { - return p.json.ToJsonString() -} - -func (p *Parser) ToJsonIndent() ([]byte, error) { - return p.json.ToJsonIndent() -} - -func (p *Parser) ToJsonIndentString() (string, error) { - return p.json.ToJsonIndentString() -} - -func (p *Parser) ToYaml() ([]byte, error) { - return p.json.ToYaml() -} - -func (p *Parser) ToToml() ([]byte, error) { - return p.json.ToToml() -} - func VarToXml(value interface{}, rootTag ...string) ([]byte, error) { return New(value).ToXml(rootTag...) } @@ -74,7 +37,3 @@ func VarToYaml(value interface{}) ([]byte, error) { func VarToToml(value interface{}) ([]byte, error) { return New(value).ToToml() } - -func VarToStruct(value interface{}, obj interface{}) error { - return New(value).ToStruct(obj) -} diff --git a/g/encoding/gparser/gparser_api_new_load.go b/g/encoding/gparser/gparser_api_new_load.go index 91548f686..1b78d679b 100644 --- a/g/encoding/gparser/gparser_api_new_load.go +++ b/g/encoding/gparser/gparser_api_new_load.go @@ -16,26 +16,34 @@ import ( // The param specifies whether using this Parser object // in un-concurrent-safe context, which is false in default. func New(value interface{}, safe ...bool) *Parser { - return &Parser{gjson.New(value, safe...)} + return gjson.New(value, safe...) } // Load loads content from specified file , // and creates a Parser object from its content. func Load(path string, safe ...bool) (*Parser, error) { - if j, e := gjson.Load(path, safe...); e == nil { - return &Parser{j}, nil - } else { - return nil, e - } + return gjson.Load(path, safe...) } // LoadContent creates a Parser object from given content, // it checks the data type of automatically, // supporting JSON, XML, YAML and TOML types of data. func LoadContent(data interface{}, safe ...bool) (*Parser, error) { - if j, e := gjson.LoadContent(data, safe...); e == nil { - return &Parser{j}, nil - } else { - return nil, e - } + return gjson.LoadContent(data, safe...) +} + +func LoadJson(data interface{}, safe ...bool) (*Parser, error) { + return gjson.LoadJson(data, safe...) +} + +func LoadXml(data interface{}, safe ...bool) (*Parser, error) { + return gjson.LoadXml(data, safe...) +} + +func LoadYaml(data interface{}, safe ...bool) (*Parser, error) { + return gjson.LoadYaml(data, safe...) +} + +func LoadToml(data interface{}, safe ...bool) (*Parser, error) { + return gjson.LoadToml(data, safe...) } diff --git a/g/encoding/gparser/gparser_unit_basic_test.go b/g/encoding/gparser/gparser_unit_basic_test.go index fc2d2d9b3..e8384c1f0 100644 --- a/g/encoding/gparser/gparser_unit_basic_test.go +++ b/g/encoding/gparser/gparser_unit_basic_test.go @@ -7,10 +7,11 @@ package gparser_test import ( + "testing" + "github.com/gogf/gf/g" "github.com/gogf/gf/g/encoding/gparser" "github.com/gogf/gf/g/test/gtest" - "testing" ) func Test_New(t *testing.T) { @@ -243,10 +244,6 @@ func Test_Convert(t *testing.T) { p = gparser.New(`[0,1,2]`) gtest.Assert(p.ToArray()[0], 0) - - err = gparser.VarToStruct(`{"name":"gf"}`, &name) - gtest.Assert(err, nil) - gtest.Assert(name.Name, "gf") }) } diff --git a/g/os/gcfg/gcfg.go b/g/os/gcfg/gcfg.go index c1828fabd..6ca67a54c 100644 --- a/g/os/gcfg/gcfg.go +++ b/g/os/gcfg/gcfg.go @@ -11,19 +11,16 @@ import ( "bytes" "errors" "fmt" - "time" "github.com/gogf/gf/g/container/garray" "github.com/gogf/gf/g/container/gmap" "github.com/gogf/gf/g/container/gtype" - "github.com/gogf/gf/g/container/gvar" "github.com/gogf/gf/g/encoding/gjson" "github.com/gogf/gf/g/internal/cmdenv" "github.com/gogf/gf/g/os/gfile" "github.com/gogf/gf/g/os/gfsnotify" "github.com/gogf/gf/g/os/glog" "github.com/gogf/gf/g/os/gspath" - "github.com/gogf/gf/g/os/gtime" ) const ( @@ -305,230 +302,3 @@ func (c *Config) getJson(file ...string) *gjson.Json { } return nil } - -func (c *Config) Get(pattern string, def ...interface{}) interface{} { - if j := c.getJson(); j != nil { - return j.Get(pattern, def...) - } - return nil -} - -func (c *Config) GetVar(pattern string, def ...interface{}) *gvar.Var { - if j := c.getJson(); j != nil { - return gvar.New(j.Get(pattern, def...)) - } - return gvar.New(nil) -} - -func (c *Config) Contains(pattern string) bool { - if j := c.getJson(); j != nil { - return j.Contains(pattern) - } - return false -} - -func (c *Config) GetMap(pattern string, def ...interface{}) map[string]interface{} { - if j := c.getJson(); j != nil { - return j.GetMap(pattern, def...) - } - return nil -} - -func (c *Config) GetArray(pattern string, def ...interface{}) []interface{} { - if j := c.getJson(); j != nil { - return j.GetArray(pattern, def...) - } - return nil -} - -func (c *Config) GetString(pattern string, def ...interface{}) string { - if j := c.getJson(); j != nil { - return j.GetString(pattern, def...) - } - return "" -} - -func (c *Config) GetStrings(pattern string, def ...interface{}) []string { - if j := c.getJson(); j != nil { - return j.GetStrings(pattern, def...) - } - return nil -} - -func (c *Config) GetInterfaces(pattern string, def ...interface{}) []interface{} { - if j := c.getJson(); j != nil { - return j.GetInterfaces(pattern, def...) - } - return nil -} - -func (c *Config) GetBool(pattern string, def ...interface{}) bool { - if j := c.getJson(); j != nil { - return j.GetBool(pattern, def...) - } - return false -} - -func (c *Config) GetFloat32(pattern string, def ...interface{}) float32 { - if j := c.getJson(); j != nil { - return j.GetFloat32(pattern, def...) - } - return 0 -} - -func (c *Config) GetFloat64(pattern string, def ...interface{}) float64 { - if j := c.getJson(); j != nil { - return j.GetFloat64(pattern, def...) - } - return 0 -} - -func (c *Config) GetFloats(pattern string, def ...interface{}) []float64 { - if j := c.getJson(); j != nil { - return j.GetFloats(pattern, def...) - } - return nil -} - -func (c *Config) GetInt(pattern string, def ...interface{}) int { - if j := c.getJson(); j != nil { - return j.GetInt(pattern, def...) - } - return 0 -} - -func (c *Config) GetInt8(pattern string, def ...interface{}) int8 { - if j := c.getJson(); j != nil { - return j.GetInt8(pattern, def...) - } - return 0 -} - -func (c *Config) GetInt16(pattern string, def ...interface{}) int16 { - if j := c.getJson(); j != nil { - return j.GetInt16(pattern, def...) - } - return 0 -} - -func (c *Config) GetInt32(pattern string, def ...interface{}) int32 { - if j := c.getJson(); j != nil { - return j.GetInt32(pattern, def...) - } - return 0 -} - -func (c *Config) GetInt64(pattern string, def ...interface{}) int64 { - if j := c.getJson(); j != nil { - return j.GetInt64(pattern, def...) - } - return 0 -} - -func (c *Config) GetInts(pattern string, def ...interface{}) []int { - if j := c.getJson(); j != nil { - return j.GetInts(pattern, def...) - } - return nil -} - -func (c *Config) GetUint(pattern string, def ...interface{}) uint { - if j := c.getJson(); j != nil { - return j.GetUint(pattern, def...) - } - return 0 -} - -func (c *Config) GetUint8(pattern string, def ...interface{}) uint8 { - if j := c.getJson(); j != nil { - return j.GetUint8(pattern, def...) - } - return 0 -} - -func (c *Config) GetUint16(pattern string, def ...interface{}) uint16 { - if j := c.getJson(); j != nil { - return j.GetUint16(pattern, def...) - } - return 0 -} - -func (c *Config) GetUint32(pattern string, def ...interface{}) uint32 { - if j := c.getJson(); j != nil { - return j.GetUint32(pattern, def...) - } - return 0 -} - -func (c *Config) GetUint64(pattern string, def ...interface{}) uint64 { - if j := c.getJson(); j != nil { - return j.GetUint64(pattern, def...) - } - return 0 -} - -func (c *Config) GetTime(pattern string, format ...string) time.Time { - if j := c.getJson(); j != nil { - return j.GetTime(pattern, format...) - } - return time.Time{} -} - -func (c *Config) GetDuration(pattern string, def ...interface{}) time.Duration { - if j := c.getJson(); j != nil { - return j.GetDuration(pattern, def...) - } - return 0 -} - -func (c *Config) GetGTime(pattern string, format ...string) *gtime.Time { - if j := c.getJson(); j != nil { - return j.GetGTime(pattern, format...) - } - return nil -} - -func (c *Config) GetStruct(pattern string, pointer interface{}, mapping ...map[string]string) error { - if j := c.getJson(); j != nil { - return j.GetStruct(pattern, pointer, mapping...) - } - return errors.New("config file 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") -} - -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") -} - -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") -} - -// Deprecated. -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() -} - -// Clear removes all parsed configuration files content cache, -// which will force reload configuration content from file. -func (c *Config) Clear() { - c.jsons.Clear() -} diff --git a/g/os/gcfg/gcfg_api.go b/g/os/gcfg/gcfg_api.go new file mode 100644 index 000000000..640ab5400 --- /dev/null +++ b/g/os/gcfg/gcfg_api.go @@ -0,0 +1,300 @@ +// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package gcfg + +import ( + "errors" + "time" + + "github.com/gogf/gf/g/encoding/gjson" + + "github.com/gogf/gf/g/container/gvar" + "github.com/gogf/gf/g/os/gtime" +) + +func (c *Config) Get(pattern string, def ...interface{}) interface{} { + if j := c.getJson(); j != nil { + return j.Get(pattern, def...) + } + return nil +} + +func (c *Config) GetVar(pattern string, def ...interface{}) *gvar.Var { + if j := c.getJson(); j != nil { + return gvar.New(j.Get(pattern, def...)) + } + return gvar.New(nil) +} + +func (c *Config) Contains(pattern string) bool { + if j := c.getJson(); j != nil { + return j.Contains(pattern) + } + return false +} + +func (c *Config) GetMap(pattern string, def ...interface{}) map[string]interface{} { + if j := c.getJson(); j != nil { + return j.GetMap(pattern, def...) + } + return nil +} + +func (c *Config) GetArray(pattern string, def ...interface{}) []interface{} { + if j := c.getJson(); j != nil { + return j.GetArray(pattern, def...) + } + return nil +} + +func (c *Config) GetBytes(pattern string, def ...interface{}) []byte { + if j := c.getJson(); j != nil { + return j.GetBytes(pattern, def...) + } + return nil +} + +func (c *Config) GetString(pattern string, def ...interface{}) string { + if j := c.getJson(); j != nil { + return j.GetString(pattern, def...) + } + return "" +} + +func (c *Config) GetStrings(pattern string, def ...interface{}) []string { + if j := c.getJson(); j != nil { + return j.GetStrings(pattern, def...) + } + return nil +} + +func (c *Config) GetInterfaces(pattern string, def ...interface{}) []interface{} { + if j := c.getJson(); j != nil { + return j.GetInterfaces(pattern, def...) + } + return nil +} + +func (c *Config) GetBool(pattern string, def ...interface{}) bool { + if j := c.getJson(); j != nil { + return j.GetBool(pattern, def...) + } + return false +} + +func (c *Config) GetFloat32(pattern string, def ...interface{}) float32 { + if j := c.getJson(); j != nil { + return j.GetFloat32(pattern, def...) + } + return 0 +} + +func (c *Config) GetFloat64(pattern string, def ...interface{}) float64 { + if j := c.getJson(); j != nil { + return j.GetFloat64(pattern, def...) + } + return 0 +} + +func (c *Config) GetFloats(pattern string, def ...interface{}) []float64 { + if j := c.getJson(); j != nil { + return j.GetFloats(pattern, def...) + } + return nil +} + +func (c *Config) GetInt(pattern string, def ...interface{}) int { + if j := c.getJson(); j != nil { + return j.GetInt(pattern, def...) + } + return 0 +} + +func (c *Config) GetInt8(pattern string, def ...interface{}) int8 { + if j := c.getJson(); j != nil { + return j.GetInt8(pattern, def...) + } + return 0 +} + +func (c *Config) GetInt16(pattern string, def ...interface{}) int16 { + if j := c.getJson(); j != nil { + return j.GetInt16(pattern, def...) + } + return 0 +} + +func (c *Config) GetInt32(pattern string, def ...interface{}) int32 { + if j := c.getJson(); j != nil { + return j.GetInt32(pattern, def...) + } + return 0 +} + +func (c *Config) GetInt64(pattern string, def ...interface{}) int64 { + if j := c.getJson(); j != nil { + return j.GetInt64(pattern, def...) + } + return 0 +} + +func (c *Config) GetInts(pattern string, def ...interface{}) []int { + if j := c.getJson(); j != nil { + return j.GetInts(pattern, def...) + } + return nil +} + +func (c *Config) GetUint(pattern string, def ...interface{}) uint { + if j := c.getJson(); j != nil { + return j.GetUint(pattern, def...) + } + return 0 +} + +func (c *Config) GetUint8(pattern string, def ...interface{}) uint8 { + if j := c.getJson(); j != nil { + return j.GetUint8(pattern, def...) + } + return 0 +} + +func (c *Config) GetUint16(pattern string, def ...interface{}) uint16 { + if j := c.getJson(); j != nil { + return j.GetUint16(pattern, def...) + } + return 0 +} + +func (c *Config) GetUint32(pattern string, def ...interface{}) uint32 { + if j := c.getJson(); j != nil { + return j.GetUint32(pattern, def...) + } + return 0 +} + +func (c *Config) GetUint64(pattern string, def ...interface{}) uint64 { + if j := c.getJson(); j != nil { + return j.GetUint64(pattern, def...) + } + return 0 +} + +func (c *Config) GetTime(pattern string, format ...string) time.Time { + if j := c.getJson(); j != nil { + return j.GetTime(pattern, format...) + } + return time.Time{} +} + +func (c *Config) GetDuration(pattern string, def ...interface{}) time.Duration { + if j := c.getJson(); j != nil { + return j.GetDuration(pattern, def...) + } + return 0 +} + +func (c *Config) GetGTime(pattern string, format ...string) *gtime.Time { + if j := c.getJson(); j != nil { + return j.GetGTime(pattern, format...) + } + return nil +} + +func (c *Config) GetJson(pattern string, def ...interface{}) *gjson.Json { + if j := c.getJson(); j != nil { + return j.GetJson(pattern, def...) + } + return nil +} + +func (c *Config) GetJsons(pattern string, def ...interface{}) []*gjson.Json { + if j := c.getJson(); j != nil { + return j.GetJsons(pattern, def...) + } + return nil +} + +func (c *Config) GetJsonMap(pattern string, def ...interface{}) map[string]*gjson.Json { + if j := c.getJson(); j != nil { + return j.GetJsonMap(pattern, def...) + } + return nil +} + +func (c *Config) GetStruct(pattern string, pointer interface{}, mapping ...map[string]string) error { + if j := c.getJson(); j != nil { + return j.GetStruct(pattern, pointer, mapping...) + } + return errors.New("config file 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") +} + +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") +} + +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") +} + +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") +} + +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") +} + +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") +} + +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") +} + +// Deprecated. +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() +} + +// Clear removes all parsed configuration files content cache, +// which will force reload configuration content from file. +func (c *Config) Clear() { + c.jsons.Clear() +}