- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 500
 
Open
Description
代码
type TestUser struct {
	Name     string   `json:"name"`
	Age      int      `json:"age"`
	Sex      string   `json:"sex"`
	Hobbies  []string `json:"hobbies"`
	IsSigned bool     `json:"is_signed"`
	Family   string   `json:"family"`
	Addr     []*Addr  `json:"addr"`
}
type TestUser2 struct {
	Name     string   `json:"name"`
	Age      int      `json:"age"`
	Sex      string   `json:"sex"`
	Hobbies  []string `json:"hobbies"`
	IsSigned bool     `json:"is_signed"`
	Score    float64  `json:"score"`
	Addr     []*Addr2 `json:"addr"`
}
type Addr struct {
	State   string
	City    string
	ZipCode int
}
type Addr2 struct {
	State   string
	City    string
	ZipCode int
	No      string
}
func TestTrans2(t *testing.T) {
	user := TestUser{
		Name:     "张三1",
		Age:      18,
		Sex:      "male",
		Hobbies:  []string{"socker"},
		IsSigned: false,
		Family:   "张三2",
		Addr: []*Addr{
			{
				State:   "北京",
				City:    "北京",
				ZipCode: 100000,
			},
			// {
			// 	State:   "北京2",
			// 	City:    "北京2",
			// 	ZipCode: 100002,
			// },
			// {
			// 	State:   "北京3",
			// 	City:    "北京3",
			// 	ZipCode: 100003,
			// },
		},
	}
	user3 := TestUser2{
		Name:     "张三2",
		Hobbies:  []string{"football", "basketball"},
		IsSigned: true,
		Score:    6.6,
		Addr: []*Addr2{
			{
				State:   "南京",
				City:    "南京",
				ZipCode: 888,
				No:      "9",
			},
			{
				State:   "南京2",
				City:    "南京2",
				ZipCode: 8882,
				No:      "92",
			},
		},
	}
	err := copier.CopyWithOption(&user3, &user, copier.Option{
		IgnoreEmpty:   false,
		CaseSensitive: true,
		DeepCopy:      false,
	})
	if err != nil {
		t.Fatalf("Trans failed, err:%v\n", err)
	}
	contents, _ := json.Marshal(user3)
	t.Logf("user3=>%s\n", contents)
}描述
当DeepCopy为false时,输出如下:
{
"name":"张三1",
"age":18,
"sex":"male",
"hobbies":["socker"],
"is_signed":false,
"score":6.6,
"addr":[
  {"State":"北京","City":"北京","ZipCode":100000,"No":""},
  {"State":"南京2","City":"南京2","ZipCode":8882,"No":"92"}
]
}当DeepCopy为true时,输出如下:
{
"name":"张三1",
"age":18,
"sex":"male",
"hobbies":["socker","basketball"],
"is_signed":false,
"score":6.6,
"addr":[
 {"State":"北京","City":"北京","ZipCode":100000,"No":""},
 {"State":"南京2","City":"南京2","ZipCode":8882,"No":"92"}
]
}期望
Addr的转换逻辑与Hobbies保持一致
Metadata
Metadata
Assignees
Labels
No labels