From ba4d99e178c4b1c463207f67a37f032a56fee9f7 Mon Sep 17 00:00:00 2001 From: jiang4869 <1121429190@qq.com> Date: Thu, 6 Apr 2023 00:45:12 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=BD=93=E9=80=89?= =?UTF-8?q?=E6=8B=A9deepCopy=E6=97=B6=EF=BC=8C=E4=BB=8E=E5=9F=BA=E7=A1=80?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E8=BD=AC=E5=88=B0sql.NullXXX=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E6=97=B6=EF=BC=8C=E8=BD=AC=E6=8D=A2=E5=A4=B1=E8=B4=A5?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- copier.go | 2 +- copier_different_type_test.go | 73 ++++++++++++++++++++++++++++++++++- 2 files changed, 72 insertions(+), 3 deletions(-) diff --git a/copier.go b/copier.go index 0d21d14..42c4ff5 100644 --- a/copier.go +++ b/copier.go @@ -529,7 +529,7 @@ func set(to, from reflect.Value, deepCopy bool, converters map[converterPair]Typ if from.Kind() == reflect.Ptr && from.IsNil() { return true, nil } - if toKind == reflect.Struct || toKind == reflect.Map || toKind == reflect.Slice { + if _, ok := to.Addr().Interface().(sql.Scanner); !ok && (toKind == reflect.Struct || toKind == reflect.Map || toKind == reflect.Slice) { return false, nil } } diff --git a/copier_different_type_test.go b/copier_different_type_test.go index aaa5fbc..08f1711 100644 --- a/copier_different_type_test.go +++ b/copier_different_type_test.go @@ -1,9 +1,10 @@ package copier_test import ( - "testing" - + "database/sql" "github.com/jinzhu/copier" + "testing" + "time" ) type TypeStruct1 struct { @@ -47,6 +48,28 @@ func (t *TypeStruct4) Field1(i int) { t.field1 = i } +type TypeBaseStruct5 struct { + A bool + B byte + C float64 + D int16 + E int32 + F int64 + G time.Time + H string +} + +type TypeSqlNullStruct6 struct { + A sql.NullBool `json:"a"` + B sql.NullByte `json:"b"` + C sql.NullFloat64 `json:"c"` + D sql.NullInt16 `json:"d"` + E sql.NullInt32 `json:"e"` + F sql.NullInt64 `json:"f"` + G sql.NullTime `json:"g"` + H sql.NullString `json:"h"` +} + func TestCopyDifferentFieldType(t *testing.T) { ts := &TypeStruct1{ Field1: "str1", @@ -150,3 +173,49 @@ func checkType2WithType4(t2 TypeStruct2, t4 TypeStruct4, t *testing.T, testCase t.Errorf("%v: type struct 4 and type struct 2 is not equal", testCase) } } + +func TestCopyFromBaseToSqlNullWithOptionDeepCopy(t *testing.T) { + a := TypeBaseStruct5{ + A: true, + B: byte(2), + C: 5.5, + D: 1, + E: 2, + F: 3, + G: time.Now(), + H: "deep", + } + b := TypeSqlNullStruct6{} + + err := copier.CopyWithOption(&b, a, copier.Option{DeepCopy: true}) + // 检查是否有错误 + if err != nil { + t.Errorf("CopyStructWithOption() error = %v", err) + return + } + // 检查 b 结构体的字段是否符合预期 + if !b.A.Valid || b.A.Bool != true { + t.Errorf("b.A = %v, want %v", b.A, true) + } + if !b.B.Valid || b.B.Byte != byte(2) { + t.Errorf("b.B = %v, want %v", b.B, byte(2)) + } + if !b.C.Valid || b.C.Float64 != 5.5 { + t.Errorf("b.C = %v, want %v", b.C, 5.5) + } + if !b.D.Valid || b.D.Int16 != 1 { + t.Errorf("b.D = %v, want %v", b.D, 1) + } + if !b.E.Valid || b.E.Int32 != 2 { + t.Errorf("b.E = %v, want %v", b.E, 2) + } + if !b.F.Valid || b.F.Int64 != 3 { + t.Errorf("b.F = %v, want %v", b.F, 3) + } + if !b.G.Valid || b.G.Time != a.G { + t.Errorf("b.G = %v, want %v", b.G, a.G) + } + if !b.H.Valid || b.H.String != "deep" { + t.Errorf("b.H = %v, want %v", b.H, "deep") + } +} From 4e0e2df777985a6f3bd974cd4c83b8666cba70b0 Mon Sep 17 00:00:00 2001 From: jiang4869 <1121429190@qq.com> Date: Thu, 6 Apr 2023 00:47:05 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8C=85=E6=8E=92?= =?UTF-8?q?=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- copier_different_type_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/copier_different_type_test.go b/copier_different_type_test.go index 08f1711..1a1e732 100644 --- a/copier_different_type_test.go +++ b/copier_different_type_test.go @@ -2,9 +2,10 @@ package copier_test import ( "database/sql" - "github.com/jinzhu/copier" "testing" "time" + + "github.com/jinzhu/copier" ) type TypeStruct1 struct {