oef/reflect/reflect.go

32 lines
689 B
Go

package reflect
import (
"reflect"
"strings"
"github.com/jinzhu/inflection"
)
func ModelName(s interface{}) string {
t := reflect.TypeOf(s)
switch t.Kind() {
case reflect.Ptr:
elem := t.Elem()
if strings.Contains(elem.String(), "[]") {
return strings.Replace(elem.String(), "[]*orm.", "", -1)
}
return elem.Name()
case reflect.Slice:
return strings.Replace(t.Elem().String(), "*orm.", "", -1)
default:
return t.Name()
}
}
func ModelNameLowerPlural(model interface{}) string {
return inflection.Plural(strings.ToLower(ModelName(model)))
}
func ModelMethod(model interface{}, method string) reflect.Value {
return reflect.ValueOf(model).MethodByName(method)
}