mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
improve performance for gconv.Maps;comment update for package gcache
This commit is contained in:
@ -4,7 +4,8 @@
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
// Package gcache provides high performance and concurrent-safe in-memory cache for process.
|
||||
// Package gcache provides kinds of cache management for process.
|
||||
// It default provides a concurrent-safe in-memory cache adapter for process.
|
||||
package gcache
|
||||
|
||||
import (
|
||||
|
||||
@ -11,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
// Adapter is the adapter for cache features implements.
|
||||
// TODO error returning for each function.
|
||||
// TODO Adding error returning for each function for preciseness.
|
||||
type Adapter interface {
|
||||
// Set sets cache with <key>-<value> pair, which is expired after <duration>.
|
||||
//
|
||||
|
||||
@ -31,6 +31,7 @@ func SliceStructDeep(params interface{}, pointer interface{}, mapping ...map[str
|
||||
}
|
||||
|
||||
// Maps converts <i> to []map[string]interface{}.
|
||||
// Note that it automatically checks and converts json string to []map if <value> is string/[]byte.
|
||||
func Maps(value interface{}, tags ...string) []map[string]interface{} {
|
||||
if value == nil {
|
||||
return nil
|
||||
|
||||
@ -23,7 +23,7 @@ func StructsDeep(params interface{}, pointer interface{}, mapping ...map[string]
|
||||
|
||||
// doStructs converts any slice to given struct slice.
|
||||
//
|
||||
// The parameter <params> should be type of slice.
|
||||
// It automatically checks and converts json string to []map if <params> is string/[]byte.
|
||||
//
|
||||
// The parameter <pointer> should be type of pointer to slice of struct.
|
||||
// Note that if <pointer> is a pointer to another pointer of type of slice of struct,
|
||||
@ -48,57 +48,45 @@ func doStructs(params interface{}, pointer interface{}, deep bool, mapping ...ma
|
||||
return gerror.Newf("pointer should be type of pointer, but got: %v", kind)
|
||||
}
|
||||
}
|
||||
params = Maps(params)
|
||||
var (
|
||||
reflectValue = reflect.ValueOf(params)
|
||||
reflectKind = reflectValue.Kind()
|
||||
)
|
||||
for reflectKind == reflect.Ptr {
|
||||
reflectValue = reflectValue.Elem()
|
||||
reflectKind = reflectValue.Kind()
|
||||
}
|
||||
switch reflectKind {
|
||||
case reflect.Slice, reflect.Array:
|
||||
// If <params> is an empty slice, no conversion.
|
||||
if reflectValue.Len() == 0 {
|
||||
return nil
|
||||
}
|
||||
var (
|
||||
array = reflect.MakeSlice(pointerRv.Type().Elem(), reflectValue.Len(), reflectValue.Len())
|
||||
itemType = array.Index(0).Type()
|
||||
)
|
||||
for i := 0; i < reflectValue.Len(); i++ {
|
||||
if itemType.Kind() == reflect.Ptr {
|
||||
// Slice element is type pointer.
|
||||
e := reflect.New(itemType.Elem()).Elem()
|
||||
if deep {
|
||||
if err = StructDeep(reflectValue.Index(i).Interface(), e, mapping...); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err = Struct(reflectValue.Index(i).Interface(), e, mapping...); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
array.Index(i).Set(e.Addr())
|
||||
} else {
|
||||
// Slice element is not type of pointer.
|
||||
e := reflect.New(itemType).Elem()
|
||||
if deep {
|
||||
if err = StructDeep(reflectValue.Index(i).Interface(), e, mapping...); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err = Struct(reflectValue.Index(i).Interface(), e, mapping...); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
array.Index(i).Set(e)
|
||||
}
|
||||
}
|
||||
pointerRv.Elem().Set(array)
|
||||
paramsMaps := Maps(params)
|
||||
// If <params> is an empty slice, no conversion.
|
||||
if len(paramsMaps) == 0 {
|
||||
return nil
|
||||
default:
|
||||
return gerror.Newf("params should be type of slice, but got: %v", reflectKind)
|
||||
}
|
||||
var (
|
||||
array = reflect.MakeSlice(pointerRv.Type().Elem(), len(paramsMaps), len(paramsMaps))
|
||||
itemType = array.Index(0).Type()
|
||||
)
|
||||
for i := 0; i < len(paramsMaps); i++ {
|
||||
if itemType.Kind() == reflect.Ptr {
|
||||
// Slice element is type pointer.
|
||||
e := reflect.New(itemType.Elem()).Elem()
|
||||
if deep {
|
||||
if err = StructDeep(paramsMaps[i], e, mapping...); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err = Struct(paramsMaps[i], e, mapping...); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
array.Index(i).Set(e.Addr())
|
||||
} else {
|
||||
// Slice element is not type of pointer.
|
||||
e := reflect.New(itemType).Elem()
|
||||
if deep {
|
||||
if err = StructDeep(paramsMaps[i], e, mapping...); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err = Struct(paramsMaps[i], e, mapping...); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
array.Index(i).Set(e)
|
||||
}
|
||||
}
|
||||
pointerRv.Elem().Set(array)
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user