2022-04-05 11:40:53 +02:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Values is a shortcut for map[string]interface{}
|
|
|
|
type Values map[string]interface{}
|
|
|
|
|
|
|
|
// ModelValues extracts Values from an entity model.
|
|
|
|
func ModelValues(m interface{}, keyNames ...string) (result Values, keys []interface{}, err error) {
|
|
|
|
isKey := func(name string) bool {
|
|
|
|
for _, s := range keyNames {
|
|
|
|
if name == s {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
r := reflect.ValueOf(m)
|
|
|
|
|
|
|
|
if r.Kind() != reflect.Pointer {
|
|
|
|
return result, keys, fmt.Errorf("model interface expected")
|
|
|
|
}
|
|
|
|
|
|
|
|
values := r.Elem()
|
|
|
|
|
|
|
|
if kind := values.Kind(); kind != reflect.Struct {
|
|
|
|
return result, keys, fmt.Errorf("model expected")
|
|
|
|
}
|
|
|
|
|
|
|
|
t := values.Type()
|
|
|
|
num := t.NumField()
|
|
|
|
|
|
|
|
keys = make([]interface{}, 0, len(keyNames))
|
|
|
|
result = make(map[string]interface{}, num)
|
|
|
|
|
|
|
|
// Add exported fields to result.
|
|
|
|
for i := 0; i < num; i++ {
|
|
|
|
field := t.Field(i)
|
|
|
|
|
|
|
|
// Skip non-exported fields.
|
|
|
|
if !field.IsExported() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-04-16 12:39:47 +02:00
|
|
|
fieldName := field.Name
|
2022-04-05 11:40:53 +02:00
|
|
|
|
|
|
|
// Skip timestamps.
|
2022-04-16 12:39:47 +02:00
|
|
|
if fieldName == "" || fieldName == "UpdatedAt" || fieldName == "CreatedAt" {
|
2022-04-05 11:40:53 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
v := values.Field(i)
|
|
|
|
|
2022-04-16 12:39:47 +02:00
|
|
|
switch v.Kind() {
|
|
|
|
case reflect.Slice, reflect.Chan, reflect.Func, reflect.Map, reflect.UnsafePointer:
|
|
|
|
continue
|
|
|
|
case reflect.Struct:
|
|
|
|
if v.IsZero() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-05 11:40:53 +02:00
|
|
|
// Skip read-only fields.
|
|
|
|
if !v.CanSet() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip keys.
|
2022-04-16 12:39:47 +02:00
|
|
|
if isKey(fieldName) {
|
2022-04-05 11:40:53 +02:00
|
|
|
if !v.IsZero() {
|
|
|
|
keys = append(keys, v.Interface())
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add value to result.
|
2022-04-16 12:39:47 +02:00
|
|
|
result[fieldName] = v.Interface()
|
2022-04-05 11:40:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(result) == 0 {
|
|
|
|
return result, keys, fmt.Errorf("no values")
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, keys, nil
|
|
|
|
}
|