Improve comments on StructToMap()

pull/2/head
Vinícius Garcia 2020-10-26 09:36:33 -03:00
parent 2f386e8541
commit 8c1c483462
1 changed files with 5 additions and 2 deletions

View File

@ -312,16 +312,19 @@ type structInfo struct {
// StructToMap converts any struct type to a map based on // StructToMap converts any struct type to a map based on
// the tag named `gorm`, i.e. `gorm:"map_key_name"` // the tag named `gorm`, i.e. `gorm:"map_key_name"`
// //
// Valid pointers are dereferenced and copied to the map,
// null pointers are ignored.
//
// This function is efficient in the fact that it caches // This function is efficient in the fact that it caches
// the slower steps of the reflection required to do perform // the slower steps of the reflection required to perform
// this task. // this task.
func StructToMap(obj interface{}) (map[string]interface{}, error) { func StructToMap(obj interface{}) (map[string]interface{}, error) {
v := reflect.ValueOf(obj) v := reflect.ValueOf(obj)
t := v.Type() t := v.Type()
if t.Kind() == reflect.Ptr { if t.Kind() == reflect.Ptr {
t = t.Elem()
v = v.Elem() v = v.Elem()
t = t.Elem()
} }
if t.Kind() != reflect.Struct { if t.Kind() != reflect.Struct {
return nil, fmt.Errorf("input must be a struct or struct pointer") return nil, fmt.Errorf("input must be a struct or struct pointer")