Commit 7d6c45d4 authored by astaxie's avatar astaxie

add RegisterModelWithSuffix #2140

parent c697b980
...@@ -22,8 +22,9 @@ import ( ...@@ -22,8 +22,9 @@ import (
) )
// register models. // register models.
// prefix means table name prefix. // PrefixOrSuffix means table name prefix or suffix.
func registerModel(prefix string, model interface{}) { // isPrefix whether the prefix is prefix or suffix
func registerModel(PrefixOrSuffix string, model interface{}, isPrefix bool) {
val := reflect.ValueOf(model) val := reflect.ValueOf(model)
typ := reflect.Indirect(val).Type() typ := reflect.Indirect(val).Type()
...@@ -39,8 +40,12 @@ func registerModel(prefix string, model interface{}) { ...@@ -39,8 +40,12 @@ func registerModel(prefix string, model interface{}) {
table := getTableName(val) table := getTableName(val)
if prefix != "" { if PrefixOrSuffix != "" {
table = prefix + table if isPrefix {
table = PrefixOrSuffix + table
} else {
table = table + PrefixOrSuffix
}
} }
// models's fullname is pkgpath + struct name // models's fullname is pkgpath + struct name
name := getFullName(typ) name := getFullName(typ)
...@@ -213,7 +218,6 @@ func bootStrap() { ...@@ -213,7 +218,6 @@ func bootStrap() {
} }
} }
} }
if fi.reverseFieldInfoTwo == nil { if fi.reverseFieldInfoTwo == nil {
err = fmt.Errorf("can not find m2m field for m2m model `%s`, ensure your m2m model defined correct", err = fmt.Errorf("can not find m2m field for m2m model `%s`, ensure your m2m model defined correct",
fi.relThroughModelInfo.fullName) fi.relThroughModelInfo.fullName)
...@@ -297,17 +301,31 @@ end: ...@@ -297,17 +301,31 @@ end:
// RegisterModel register models // RegisterModel register models
func RegisterModel(models ...interface{}) { func RegisterModel(models ...interface{}) {
if modelCache.done {
panic(fmt.Errorf("RegisterModel must be run before BootStrap"))
}
RegisterModelWithPrefix("", models...) RegisterModelWithPrefix("", models...)
} }
// RegisterModelWithPrefix register models with a prefix // RegisterModelWithPrefix register models with a prefix
func RegisterModelWithPrefix(prefix string, models ...interface{}) { func RegisterModelWithPrefix(prefix string, models ...interface{}) {
if modelCache.done { if modelCache.done {
panic(fmt.Errorf("RegisterModel must be run before BootStrap")) panic(fmt.Errorf("RegisterModelWithPrefix must be run before BootStrap"))
}
for _, model := range models {
registerModel(prefix, model, true)
}
}
// RegisterModelWithSuffix register models with a suffix
func RegisterModelWithSuffix(suffix string, models ...interface{}) {
if modelCache.done {
panic(fmt.Errorf("RegisterModelWithSuffix must be run before BootStrap"))
} }
for _, model := range models { for _, model := range models {
registerModel(prefix, model) registerModel(suffix, model, false)
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment