diamond-orm/util.go

71 lines
1.5 KiB
Go
Raw Normal View History

2024-09-01 16:17:48 -04:00
package orm
import "reflect"
func panik(err error) {
if err != nil {
panic(err)
}
}
func nameOf(i interface{}) string {
v := reflect.ValueOf(i)
var n string
switch v.Kind() {
case reflect.Slice, reflect.Map:
if v.Type().Elem().Kind() == reflect.Pointer {
n = v.Type().Elem().Elem().Name()
}
case reflect.Pointer:
n = nameOf(reflect.Indirect(v).Interface())
default:
n = v.Type().Name()
}
return n
}
func valueOf(i interface{}) reflect.Value {
v := reflect.ValueOf(i)
if v.Type().Kind() == reflect.Slice || v.Type().Kind() == reflect.Map {
in := v.Type().Elem()
switch in.Kind() {
case reflect.Pointer:
v = reflect.New(in.Elem()).Elem()
default:
v = reflect.New(in).Elem()
}
} else if v.Type().Kind() == reflect.Pointer {
v = valueOf(reflect.Indirect(v).Interface())
}
return v
}
func iFace(input interface{}) interface{} {
return reflect.ValueOf(input).Interface()
}
func iFaceSlice(input interface{}) []interface{} {
ret := make([]interface{}, 0)
fv := reflect.ValueOf(input)
if fv.Type().Kind() != reflect.Slice {
return ret
}
for i := 0; i < fv.Len(); i++ {
ret = append(ret, fv.Index(i).Interface())
}
return ret
}
func coerceInt(input reflect.Value, dst reflect.Value) interface{} {
if input.Type().Kind() == reflect.Pointer {
input = input.Elem()
}
if dst.Type().Kind() == reflect.Pointer {
dst = dst.Elem()
}
if input.Type().ConvertibleTo(dst.Type()) {
return input.Convert(dst.Type()).Interface()
}
return nil
}