From 07960fe661b5ced50c9ca30e010aa26513eaf851 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Thu, 18 Jun 2020 09:32:31 +0800 Subject: [PATCH] Fix []byte support --- schema/field.go | 2 +- statement.go | 3 +++ tests/scanner_valuer_test.go | 10 ++++++---- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/schema/field.go b/schema/field.go index 8bfa3b22..f8ecef60 100644 --- a/schema/field.go +++ b/schema/field.go @@ -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 } } diff --git a/statement.go b/statement.go index 720ef283..2a092966 100644 --- a/statement.go +++ b/statement.go @@ -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('(') diff --git a/tests/scanner_valuer_test.go b/tests/scanner_valuer_test.go index ec228f00..632bd74a 100644 --- a/tests/scanner_valuer_test.go +++ b/tests/scanner_valuer_test.go @@ -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