Fix []byte support

This commit is contained in:
Jinzhu 2020-06-18 09:32:31 +08:00
parent 96368eb967
commit 07960fe661
3 changed files with 10 additions and 5 deletions

View File

@ -214,7 +214,7 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
field.DataType = Time
}
case reflect.Array, reflect.Slice:
if fieldValue.Type().Elem() == reflect.TypeOf(uint8(0)) {
if reflect.Indirect(fieldValue).Type().Elem() == reflect.TypeOf(uint8(0)) {
field.DataType = Bytes
}
}

View File

@ -160,6 +160,9 @@ func (stmt *Statement) AddVar(writer clause.Writer, vars ...interface{}) {
case driver.Valuer:
stmt.Vars = append(stmt.Vars, v)
stmt.DB.Dialector.BindVarTo(writer, stmt, v)
case []byte:
stmt.Vars = append(stmt.Vars, v)
stmt.DB.Dialector.BindVarTo(writer, stmt, v)
case []interface{}:
if len(v) > 0 {
writer.WriteByte('(')

View File

@ -17,7 +17,7 @@ import (
func TestScannerValuer(t *testing.T) {
DB.Migrator().DropTable(&ScannerValuerStruct{})
if err := DB.Migrator().AutoMigrate(&ScannerValuerStruct{}); err != nil {
t.Errorf("no error should happen when migrate scanner, valuer struct")
t.Fatalf("no error should happen when migrate scanner, valuer struct, got error %v", err)
}
data := ScannerValuerStruct{
@ -28,6 +28,7 @@ func TestScannerValuer(t *testing.T) {
Height: sql.NullFloat64{Float64: 1.8888, Valid: true},
Birthday: sql.NullTime{Time: time.Now(), Valid: true},
Password: EncryptedData("pass1"),
Bytes: []byte("byte"),
Num: 18,
Strings: StringsSlice{"a", "b", "c"},
Structs: StructsSlice{
@ -38,16 +39,16 @@ func TestScannerValuer(t *testing.T) {
}
if err := DB.Create(&data).Error; err != nil {
t.Errorf("No error should happened when create scanner valuer struct, but got %v", err)
t.Fatalf("No error should happened when create scanner valuer struct, but got %v", err)
}
var result ScannerValuerStruct
if err := DB.Find(&result).Error; err != nil {
t.Errorf("no error should happen when query scanner, valuer struct, but got %v", err)
t.Fatalf("no error should happen when query scanner, valuer struct, but got %v", err)
}
AssertObjEqual(t, data, result, "Name", "Gender", "Age", "Male", "Height", "Birthday", "Password", "Num", "Strings", "Structs")
AssertObjEqual(t, data, result, "Name", "Gender", "Age", "Male", "Height", "Birthday", "Password", "Bytes", "Num", "Strings", "Structs")
}
func TestScannerValuerWithFirstOrCreate(t *testing.T) {
@ -130,6 +131,7 @@ type ScannerValuerStruct struct {
Height sql.NullFloat64
Birthday sql.NullTime
Password EncryptedData
Bytes []byte
Num Num
Strings StringsSlice
Structs StructsSlice