Fix set Scanner's data type

This commit is contained in:
Jinzhu 2017-02-06 08:43:49 +08:00
parent 6633f325b8
commit b870f86fba
2 changed files with 55 additions and 1 deletions

View File

@ -194,10 +194,12 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
if indirectType.Kind() == reflect.Struct {
for i := 0; i < indirectType.NumField(); i++ {
for key, value := range parseTagSetting(indirectType.Field(i).Tag) {
if _, ok := field.TagSettings[key]; !ok {
field.TagSettings[key] = value
}
}
}
}
} else if _, isTime := fieldValue.(*time.Time); isTime {
// is time
field.IsNormal = true

View File

@ -5,6 +5,8 @@ import (
"encoding/json"
"errors"
"testing"
"github.com/jinzhu/gorm"
)
func TestScannableSlices(t *testing.T) {
@ -83,3 +85,53 @@ func (l *ExampleStructSlice) Scan(input interface{}) error {
return errors.New("not supported")
}
}
type ScannerDataType struct {
Street string `sql:"TYPE:varchar(24)"`
}
func (ScannerDataType) Value() (driver.Value, error) {
return nil, nil
}
func (*ScannerDataType) Scan(input interface{}) error {
return nil
}
type ScannerDataTypeTestStruct struct {
Field1 int
ScannerDataType *ScannerDataType `sql:"TYPE:json"`
}
type ScannerDataType2 struct {
Street string `sql:"TYPE:varchar(24)"`
}
func (ScannerDataType2) Value() (driver.Value, error) {
return nil, nil
}
func (*ScannerDataType2) Scan(input interface{}) error {
return nil
}
type ScannerDataTypeTestStruct2 struct {
Field1 int
ScannerDataType *ScannerDataType2
}
func TestScannerDataType(t *testing.T) {
scope := gorm.Scope{Value: &ScannerDataTypeTestStruct{}}
if field, ok := scope.FieldByName("ScannerDataType"); ok {
if DB.Dialect().DataTypeOf(field.StructField) != "json" {
t.Errorf("data type for scanner is wrong")
}
}
scope = gorm.Scope{Value: &ScannerDataTypeTestStruct2{}}
if field, ok := scope.FieldByName("ScannerDataType"); ok {
if DB.Dialect().DataTypeOf(field.StructField) != "varchar(24)" {
t.Errorf("data type for scanner is wrong")
}
}
}