Fix get scanner type

This commit is contained in:
Jinzhu 2015-02-16 17:10:13 +08:00
parent f4e705b749
commit 2d866090e4
1 changed files with 13 additions and 10 deletions

View File

@ -45,7 +45,7 @@ type Relationship struct {
func (scope *Scope) GenerateSqlTag(field *StructField) { func (scope *Scope) GenerateSqlTag(field *StructField) {
var sqlType string var sqlType string
reflectValue := reflect.New(field.Struct.Type) reflectValue := reflect.Indirect(reflect.New(field.Struct.Type))
if value, ok := field.SqlSettings["TYPE"]; ok { if value, ok := field.SqlSettings["TYPE"]; ok {
sqlType = value sqlType = value
@ -58,12 +58,13 @@ func (scope *Scope) GenerateSqlTag(field *StructField) {
if field.IsScanner { if field.IsScanner {
var getScannerValue func(reflect.Value) var getScannerValue func(reflect.Value)
getScannerValue = func(reflectValue reflect.Value) { getScannerValue = func(value reflect.Value) {
if _, isScanner := reflect.New(reflectValue.Type()).Interface().(sql.Scanner); isScanner { reflectValue = value
if _, isScanner := reflect.New(reflectValue.Type()).Interface().(sql.Scanner); isScanner && reflectValue.Kind() == reflect.Struct {
getScannerValue(reflectValue.Field(0)) getScannerValue(reflectValue.Field(0))
} }
} }
getScannerValue(reflectValue.Field(0)) getScannerValue(reflectValue)
} }
if sqlType == "" { if sqlType == "" {
@ -188,7 +189,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
if many2many != "" { if many2many != "" {
kind = "many_to_many" kind = "many_to_many"
} else if !reflect.New(typ).FieldByName(foreignKey).IsValid() { } else if !reflect.New(typ).Elem().FieldByName(foreignKey).IsValid() {
foreignKey = "" foreignKey = ""
} }
@ -239,6 +240,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
} }
for _, field := range modelStruct.StructFields { for _, field := range modelStruct.StructFields {
if field.IsNormal {
if modelStruct.PrimaryKeyField == nil && field.DBName == "id" { if modelStruct.PrimaryKeyField == nil && field.DBName == "id" {
field.IsPrimaryKey = true field.IsPrimaryKey = true
modelStruct.PrimaryKeyField = field modelStruct.PrimaryKeyField = field
@ -246,6 +248,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
scope.GenerateSqlTag(field) scope.GenerateSqlTag(field)
} }
}
return &modelStruct return &modelStruct
} }