diff --git a/model.go b/model.go index ada1d447..9f8b13c3 100644 --- a/model.go +++ b/model.go @@ -1,6 +1,10 @@ package gorm -import "reflect" +import ( + "regexp" + + "reflect" +) type Model struct { Data interface{} @@ -26,6 +30,23 @@ func (m *Model) ColumnsAndValues() (columns []string, values []interface{}) { return } +func (m *Model) TableName() string { + t := reflect.TypeOf(m.Data) + for { + c := false + switch t.Kind() { + case reflect.Array, reflect.Chan, reflect.Map, reflect.Ptr, reflect.Slice: + t = t.Elem() + c = true + } + if !c { + break + } + } + reg, _ := regexp.Compile("s*$") + return reg.ReplaceAllString(toSnake(t.Name()), "s") +} + func (m *Model) Columns() (columns []string) { typ := reflect.TypeOf(m.Data).Elem() diff --git a/orm.go b/orm.go index 8d2ad438..f19511a3 100644 --- a/orm.go +++ b/orm.go @@ -26,7 +26,7 @@ type Orm struct { func (s *Orm) setModel(model interface{}) (err error) { s.Model = toModel(model) - s.TableName = interfaceToTableName(model) + s.TableName = s.Model.TableName() s.PrimaryKey = "id" return } diff --git a/utils.go b/utils.go index 138b8e44..a081554f 100644 --- a/utils.go +++ b/utils.go @@ -3,8 +3,7 @@ package gorm import ( "bytes" "fmt" - "reflect" - "regexp" + "strings" ) @@ -45,23 +44,6 @@ func snakeToUpperCamel(s string) string { return buf.String() } -func interfaceToTableName(f interface{}) string { - t := reflect.TypeOf(f) - for { - c := false - switch t.Kind() { - case reflect.Array, reflect.Chan, reflect.Map, reflect.Ptr, reflect.Slice: - t = t.Elem() - c = true - } - if !c { - break - } - } - reg, _ := regexp.Compile("s*$") - return reg.ReplaceAllString(toSnake(t.Name()), "s") -} - func debug(value interface{}) { fmt.Printf("***************\n") fmt.Printf("%+v\n\n", value)