diff --git a/os/gcache/gcache.go b/os/gcache/gcache.go index 552eba5f0..1ac20f1a2 100644 --- a/os/gcache/gcache.go +++ b/os/gcache/gcache.go @@ -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 ( diff --git a/os/gcache/gcache_adapter.go b/os/gcache/gcache_adapter.go index cb0961b01..37f829026 100644 --- a/os/gcache/gcache_adapter.go +++ b/os/gcache/gcache_adapter.go @@ -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 - pair, which is expired after . // diff --git a/util/gconv/gconv_slice.go b/util/gconv/gconv_slice.go index db06fdad1..220031ce0 100644 --- a/util/gconv/gconv_slice.go +++ b/util/gconv/gconv_slice.go @@ -31,6 +31,7 @@ func SliceStructDeep(params interface{}, pointer interface{}, mapping ...map[str } // Maps converts to []map[string]interface{}. +// Note that it automatically checks and converts json string to []map if is string/[]byte. func Maps(value interface{}, tags ...string) []map[string]interface{} { if value == nil { return nil diff --git a/util/gconv/gconv_structs.go b/util/gconv/gconv_structs.go index 3395680f3..f447fcf6f 100644 --- a/util/gconv/gconv_structs.go +++ b/util/gconv/gconv_structs.go @@ -23,7 +23,7 @@ func StructsDeep(params interface{}, pointer interface{}, mapping ...map[string] // doStructs converts any slice to given struct slice. // -// The parameter should be type of slice. +// It automatically checks and converts json string to []map if is string/[]byte. // // The parameter should be type of pointer to slice of struct. // Note that if 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 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 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 + }