File: copier_issue84_test.go

package info (click to toggle)
golang-github-jinzhu-copier 0.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 156 kB
  • sloc: makefile: 2
file content (86 lines) | stat: -rw-r--r-- 1,755 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package copier_test

import (
	"testing"
	"time"

	"github.com/jinzhu/copier"
)

type Embedded struct {
	Field1 string
	Field2 string
}

type Embedder struct {
	Embedded
	PtrField *string
}

type Timestamps struct {
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
}

type NotWork struct {
	ID      string  `json:"id"`
	UserID  *string `json:"user_id"`
	Name    string  `json:"name"`
	Website *string `json:"website"`
	Timestamps
}

type Work struct {
	ID      string  `json:"id"`
	Name    string  `json:"name"`
	UserID  *string `json:"user_id"`
	Website *string `json:"website"`
	Timestamps
}

func TestIssue84(t *testing.T) {
	t.Run("test1", func(t *testing.T) {
		var embedder Embedder
		embedded := Embedded{
			Field1: "1",
			Field2: "2",
		}
		err := copier.Copy(&embedder, &embedded)
		if err != nil {
			t.Errorf("unable to copy: %s", err)
		}
		if embedder.Field1 != embedded.Field1 {
			t.Errorf("field1 value is %s instead of %s", embedder.Field1, embedded.Field1)
		}
		if embedder.Field2 != embedded.Field2 {
			t.Errorf("field2 value is %s instead of %s", embedder.Field2, embedded.Field2)
		}
	})
	t.Run("from issue", func(t *testing.T) {
		notWorkObj := NotWork{
			ID:      "123",
			Name:    "name",
			Website: nil,
			UserID:  nil,
			Timestamps: Timestamps{
				UpdatedAt: time.Now(),
			},
		}
		workObj := Work{
			ID:      "123",
			Name:    "name",
			Website: nil,
			UserID:  nil,
			Timestamps: Timestamps{
				UpdatedAt: time.Now(),
			},
		}

		destObj1 := Work{}
		destObj2 := NotWork{}

		copier.CopyWithOption(&destObj1, &workObj, copier.Option{IgnoreEmpty: true, DeepCopy: false})

		copier.CopyWithOption(&destObj2, &notWorkObj, copier.Option{IgnoreEmpty: true, DeepCopy: false})
	})
}