diff --git a/g/container/gvar/gvar.go b/g/container/gvar/gvar.go index 05b3098e8..bbf3543d9 100644 --- a/g/container/gvar/gvar.go +++ b/g/container/gvar/gvar.go @@ -58,33 +58,6 @@ func (v *Var) Interface() interface{} { return v.Val() } -// Time converts and returns as time.Time. -// The parameter specifies the format of the time string using gtime, -// eg: Y-m-d H:i:s. -func (v *Var) Time(format ...string) time.Time { - return gconv.Time(v.Val(), format...) -} - -// Duration converts and returns as time.Duration. -// If value of is string, then it uses time.ParseDuration for conversion. -func (v *Var) Duration() time.Duration { - return gconv.Duration(v.Val()) -} - -// GTime converts and returns as *gtime.Time. -// The parameter specifies the format of the time string using gtime, -// eg: Y-m-d H:i:s. -func (v *Var) GTime(format ...string) *gtime.Time { - return gconv.GTime(v.Val(), format...) -} - -// Struct maps value of to . -// The parameter should be a pointer to a struct instance. -// The parameter is used to specify the key-to-attribute mapping rules. -func (v *Var) Struct(pointer interface{}, mapping ...map[string]string) error { - return gconv.Struct(v.Val(), pointer, mapping...) -} - // IsNil checks whether is nil. func (v *Var) IsNil() bool { return v.Val() == nil @@ -184,3 +157,57 @@ func (v *Var) Strings() []string { func (v *Var) Interfaces() []interface{} { return gconv.Interfaces(v.Val()) } + +// Time converts and returns as time.Time. +// The parameter specifies the format of the time string using gtime, +// eg: Y-m-d H:i:s. +func (v *Var) Time(format ...string) time.Time { + return gconv.Time(v.Val(), format...) +} + +// Duration converts and returns as time.Duration. +// If value of is string, then it uses time.ParseDuration for conversion. +func (v *Var) Duration() time.Duration { + return gconv.Duration(v.Val()) +} + +// GTime converts and returns as *gtime.Time. +// The parameter specifies the format of the time string using gtime, +// eg: Y-m-d H:i:s. +func (v *Var) GTime(format ...string) *gtime.Time { + return gconv.GTime(v.Val(), format...) +} + +// Map converts to map[string]interface{}. +func (v *Var) Map(tags ...string) map[string]interface{} { + return gconv.Map(v.Val(), tags...) +} + +// MapDeep converts to map[string]interface{} recursively. +func (v *Var) MapDeep(tags ...string) map[string]interface{} { + return gconv.MapDeep(v.Val(), tags...) +} + +// Struct maps value of to . +// The parameter should be a pointer to a struct instance. +// The parameter is used to specify the key-to-attribute mapping rules. +func (v *Var) Struct(pointer interface{}, mapping ...map[string]string) error { + return gconv.Struct(v.Val(), pointer, mapping...) +} + +// Struct maps value of to recursively. +// The parameter should be a pointer to a struct instance. +// The parameter is used to specify the key-to-attribute mapping rules. +func (v *Var) StructDeep(pointer interface{}, mapping ...map[string]string) error { + return gconv.StructDeep(v.Val(), pointer, mapping...) +} + +// Structs converts to given struct slice. +func (v *Var) Structs(pointer interface{}, mapping ...map[string]string) (err error) { + return gconv.Structs(v.Val(), pointer, mapping...) +} + +// StructsDeep converts to given struct slice recursively. +func (v *Var) StructsDeep(pointer interface{}, mapping ...map[string]string) (err error) { + return gconv.StructsDeep(v.Val(), pointer, mapping...) +} diff --git a/g/encoding/gjson/gjson_api.go b/g/encoding/gjson/gjson_api.go index b83316d30..a03a50f60 100644 --- a/g/encoding/gjson/gjson_api.go +++ b/g/encoding/gjson/gjson_api.go @@ -266,11 +266,32 @@ func (j *Json) GetToVar(pattern string, pointer interface{}) error { return nil } -// GetToStruct gets the value by specified , -// and converts it to specified object . -// The should be the pointer to an object. -func (j *Json) GetToStruct(pattern string, pointer interface{}) error { - return gconv.Struct(j.Get(pattern), pointer) +// GetStruct gets the value by specified , +// and converts it to specified object . +// The should be the pointer to an object. +func (j *Json) GetStruct(pattern string, pointer interface{}, mapping ...map[string]string) error { + return gconv.Struct(j.Get(pattern), pointer, mapping...) +} + +// GetStructDeep does GetStruct recursively. +func (j *Json) GetStructDeep(pattern string, pointer interface{}, mapping ...map[string]string) error { + return gconv.StructDeep(j.Get(pattern), pointer, mapping...) +} + +// GetStructs converts any slice to given struct slice. +func (j *Json) GetStructs(pattern string, pointer interface{}, mapping ...map[string]string) error { + return gconv.Structs(j.Get(pattern), pointer, mapping...) +} + +// GetStructsDeep converts any slice to given struct slice recursively. +func (j *Json) GetStructsDeep(pattern string, pointer interface{}, mapping ...map[string]string) error { + return gconv.StructsDeep(j.Get(pattern), pointer, mapping...) +} + +// GetToStruct is alias of GetStruct. +// Deprecated. +func (j *Json) GetToStruct(pattern string, pointer interface{}, mapping ...map[string]string) error { + return j.GetStruct(pattern, pointer, mapping...) } // ToMap converts current Json object to map[string]interface{}. @@ -290,7 +311,7 @@ func (j *Json) ToArray() []interface{} { } // ToStruct converts current Json object to specified object. -// The should be a pointer type. +// The should be a pointer type. func (j *Json) ToStruct(pointer interface{}) error { j.mu.RLock() defer j.mu.RUnlock() diff --git a/g/encoding/gparser/gparser_api.go b/g/encoding/gparser/gparser_api.go index 659b2c7dc..2b5261a09 100644 --- a/g/encoding/gparser/gparser_api.go +++ b/g/encoding/gparser/gparser_api.go @@ -145,11 +145,32 @@ func (p *Parser) GetToVar(pattern string, pointer interface{}) error { return p.json.GetToVar(pattern, pointer) } -// GetToStruct gets the value by specified , +// GetStruct gets the value by specified , // and converts it to specified object . -// The should be the pointer to a struct. -func (p *Parser) GetToStruct(pattern string, pointer interface{}) error { - return p.json.GetToStruct(pattern, pointer) +// 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 . diff --git a/g/net/ghttp/ghttp_request_post.go b/g/net/ghttp/ghttp_request_post.go index 6a594fd1b..193544256 100644 --- a/g/net/ghttp/ghttp_request_post.go +++ b/g/net/ghttp/ghttp_request_post.go @@ -154,5 +154,5 @@ func (r *Request) GetPostToStruct(pointer interface{}, mapping ...map[string]str for k, v := range r.GetPostMap() { params[k] = v } - return gconv.Struct(params, pointer, tagMap) + return gconv.StructDeep(params, pointer, tagMap) } diff --git a/g/os/gcfg/gcfg.go b/g/os/gcfg/gcfg.go index 1badfa033..adb3fa570 100644 --- a/g/os/gcfg/gcfg.go +++ b/g/os/gcfg/gcfg.go @@ -489,14 +489,41 @@ func (c *Config) GetGTime(pattern string, format ...string) *gtime.Time { return nil } -func (c *Config) GetToStruct(pattern string, pointer interface{}, def ...interface{}) error { +func (c *Config) GetStruct(pattern string, pointer interface{}, mapping ...map[string]string) error { if j := c.getJson(); j != nil { - return j.GetToStruct(pattern, pointer) + return j.GetStruct(pattern, pointer, mapping...) } return errors.New("config file not found") } -// Deprecated. See Clear. +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() } diff --git a/g/util/gconv/gconv_slice.go b/g/util/gconv/gconv_slice.go index d9f57d500..2b7178bae 100644 --- a/g/util/gconv/gconv_slice.go +++ b/g/util/gconv/gconv_slice.go @@ -14,6 +14,46 @@ import ( "github.com/gogf/gf/g/text/gstr" ) +// SliceInt is alias of Ints. +func SliceInt(i interface{}) []int { + return Ints(i) +} + +// SliceStr is alias of Strings. +func SliceStr(i interface{}) []string { + return Strings(i) +} + +// SliceAny is alias of Interfaces. +func SliceAny(i interface{}) []interface{} { + return Interfaces(i) +} + +// SliceFloat is alias of Floats. +func SliceFloat(i interface{}) []float64 { + return Floats(i) +} + +// SliceMap is alias of Maps. +func SliceMap(i interface{}) []map[string]interface{} { + return Maps(i) +} + +// SliceMapDeep is alias of MapsDeep. +func SliceMapDeep(i interface{}) []map[string]interface{} { + return MapsDeep(i) +} + +// SliceStruct is alias of Structs. +func SliceStruct(params interface{}, pointer interface{}, mapping ...map[string]string) (err error) { + return Structs(params, pointer, mapping...) +} + +// SliceStructDeep is alias of StructsDeep. +func SliceStructDeep(params interface{}, pointer interface{}, mapping ...map[string]string) (err error) { + return StructsDeep(params, pointer, mapping...) +} + // Ints converts to []int. func Ints(i interface{}) []int { if i == nil { @@ -350,6 +390,26 @@ func Maps(i interface{}) []map[string]interface{} { } } +// MapsDeep converts to []map[string]interface{} recursively. +func MapsDeep(i interface{}) []map[string]interface{} { + if i == nil { + return nil + } + if r, ok := i.([]map[string]interface{}); ok { + return r + } else { + array := Interfaces(i) + if len(array) == 0 { + return nil + } + list := make([]map[string]interface{}, len(array)) + for k, v := range array { + list[k] = MapDeep(v) + } + return list + } +} + // Structs converts any slice to given struct slice. func Structs(params interface{}, pointer interface{}, mapping ...map[string]string) (err error) { return doStructs(params, pointer, false, mapping...)