mirror of https://github.com/go-gorm/gorm.git
Change plural engine to github.com/qor/inflection
This commit is contained in:
parent
6f30170fec
commit
05b3f036f8
|
@ -5,10 +5,11 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/ast"
|
"go/ast"
|
||||||
"reflect"
|
"reflect"
|
||||||
"regexp"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/qor/inflection"
|
||||||
)
|
)
|
||||||
|
|
||||||
var modelStructs = map[reflect.Type]*ModelStruct{}
|
var modelStructs = map[reflect.Type]*ModelStruct{}
|
||||||
|
@ -71,9 +72,6 @@ type Relationship struct {
|
||||||
JoinTableHandler JoinTableHandlerInterface
|
JoinTableHandler JoinTableHandlerInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
var pluralMapKeys = []*regexp.Regexp{regexp.MustCompile("ch$"), regexp.MustCompile("ss$"), regexp.MustCompile("sh$"), regexp.MustCompile("day$"), regexp.MustCompile("y$"), regexp.MustCompile("x$"), regexp.MustCompile("([^s])s?$")}
|
|
||||||
var pluralMapValues = []string{"ches", "sses", "shes", "days", "ies", "xes", "${1}s"}
|
|
||||||
|
|
||||||
func (scope *Scope) GetModelStruct() *ModelStruct {
|
func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
var modelStruct ModelStruct
|
var modelStruct ModelStruct
|
||||||
|
|
||||||
|
@ -113,11 +111,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
} else {
|
} else {
|
||||||
name := ToDBName(scopeType.Name())
|
name := ToDBName(scopeType.Name())
|
||||||
if scope.db == nil || !scope.db.parent.singularTable {
|
if scope.db == nil || !scope.db.parent.singularTable {
|
||||||
for index, reg := range pluralMapKeys {
|
name = inflection.Plural(name)
|
||||||
if reg.MatchString(name) {
|
|
||||||
name = reg.ReplaceAllString(name, pluralMapValues[index])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
modelStruct.defaultTableName = name
|
modelStruct.defaultTableName = name
|
||||||
|
|
Loading…
Reference in New Issue