|
1 | 1 | package copier_test
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "database/sql" |
4 | 5 | "testing"
|
| 6 | + "time" |
5 | 7 |
|
6 | 8 | "github.com/jinzhu/copier"
|
7 | 9 | )
|
@@ -47,6 +49,28 @@ func (t *TypeStruct4) Field1(i int) {
|
47 | 49 | t.field1 = i
|
48 | 50 | }
|
49 | 51 |
|
| 52 | +type TypeBaseStruct5 struct { |
| 53 | + A bool |
| 54 | + B byte |
| 55 | + C float64 |
| 56 | + D int16 |
| 57 | + E int32 |
| 58 | + F int64 |
| 59 | + G time.Time |
| 60 | + H string |
| 61 | +} |
| 62 | + |
| 63 | +type TypeSqlNullStruct6 struct { |
| 64 | + A sql.NullBool `json:"a"` |
| 65 | + B sql.NullByte `json:"b"` |
| 66 | + C sql.NullFloat64 `json:"c"` |
| 67 | + D sql.NullInt16 `json:"d"` |
| 68 | + E sql.NullInt32 `json:"e"` |
| 69 | + F sql.NullInt64 `json:"f"` |
| 70 | + G sql.NullTime `json:"g"` |
| 71 | + H sql.NullString `json:"h"` |
| 72 | +} |
| 73 | + |
50 | 74 | func TestCopyDifferentFieldType(t *testing.T) {
|
51 | 75 | ts := &TypeStruct1{
|
52 | 76 | Field1: "str1",
|
@@ -150,3 +174,49 @@ func checkType2WithType4(t2 TypeStruct2, t4 TypeStruct4, t *testing.T, testCase
|
150 | 174 | t.Errorf("%v: type struct 4 and type struct 2 is not equal", testCase)
|
151 | 175 | }
|
152 | 176 | }
|
| 177 | + |
| 178 | +func TestCopyFromBaseToSqlNullWithOptionDeepCopy(t *testing.T) { |
| 179 | + a := TypeBaseStruct5{ |
| 180 | + A: true, |
| 181 | + B: byte(2), |
| 182 | + C: 5.5, |
| 183 | + D: 1, |
| 184 | + E: 2, |
| 185 | + F: 3, |
| 186 | + G: time.Now(), |
| 187 | + H: "deep", |
| 188 | + } |
| 189 | + b := TypeSqlNullStruct6{} |
| 190 | + |
| 191 | + err := copier.CopyWithOption(&b, a, copier.Option{DeepCopy: true}) |
| 192 | + // 检查是否有错误 |
| 193 | + if err != nil { |
| 194 | + t.Errorf("CopyStructWithOption() error = %v", err) |
| 195 | + return |
| 196 | + } |
| 197 | + // 检查 b 结构体的字段是否符合预期 |
| 198 | + if !b.A.Valid || b.A.Bool != true { |
| 199 | + t.Errorf("b.A = %v, want %v", b.A, true) |
| 200 | + } |
| 201 | + if !b.B.Valid || b.B.Byte != byte(2) { |
| 202 | + t.Errorf("b.B = %v, want %v", b.B, byte(2)) |
| 203 | + } |
| 204 | + if !b.C.Valid || b.C.Float64 != 5.5 { |
| 205 | + t.Errorf("b.C = %v, want %v", b.C, 5.5) |
| 206 | + } |
| 207 | + if !b.D.Valid || b.D.Int16 != 1 { |
| 208 | + t.Errorf("b.D = %v, want %v", b.D, 1) |
| 209 | + } |
| 210 | + if !b.E.Valid || b.E.Int32 != 2 { |
| 211 | + t.Errorf("b.E = %v, want %v", b.E, 2) |
| 212 | + } |
| 213 | + if !b.F.Valid || b.F.Int64 != 3 { |
| 214 | + t.Errorf("b.F = %v, want %v", b.F, 3) |
| 215 | + } |
| 216 | + if !b.G.Valid || b.G.Time != a.G { |
| 217 | + t.Errorf("b.G = %v, want %v", b.G, a.G) |
| 218 | + } |
| 219 | + if !b.H.Valid || b.H.String != "deep" { |
| 220 | + t.Errorf("b.H = %v, want %v", b.H, "deep") |
| 221 | + } |
| 222 | +} |
0 commit comments