Example #1
0
func RegModels(models ...interface{}) {
	for _, model := range models {
		pkg := mcore.GetPkgPath(model)
		typ := mcore.GetTypeName(model)
		key := fmt.Sprintf("%s-%s", pkg, typ)
		Models[key] = model
	}
}
Example #2
0
// GetModelTableName
// Model Should has TableName() method.
// If TableName() function not present, cast model name
// to table name.
func GetModelTableName(model interface{}) (result string) {
	// reflect model method TableName() error, try TypeName
	defer func() {
		err := recover()
		if err != nil {
			//typ := reflect.TypeOf(model)
			//typeName := fmt.Sprintf("%v", typ)
			typeName := mcore.GetTypeName(model)
			//type name to table name
			result = mcore.ToTableName(typeName)
			logger.Debugf("Model not implements TableName() function. Cast model name to table name. Model:%s, Table: %s", typeName, result)
		}
	}()

	in := make([]reflect.Value, 0)
	method := reflect.ValueOf(model).MethodByName("TableName")
	r := method.Call(in)
	result = fmt.Sprintf("%v", r[0])
	return
}