From be8a0558f8bd4d5f00b64c3910b90703b851b872 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Mon, 20 Feb 2023 12:00:49 +0800 Subject: [PATCH] test: add examples for CopyProperties function --- convertor/convertor_example_test.go | 42 +++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/convertor/convertor_example_test.go b/convertor/convertor_example_test.go index 62413f5..8720e48 100644 --- a/convertor/convertor_example_test.go +++ b/convertor/convertor_example_test.go @@ -295,3 +295,45 @@ func ExampleDeepClone() { // map[a:1 b:2] false // &{test 1 0.1 true } false } + +func ExampleCopyProperties() { + type Address struct { + Country string + ZipCode string + } + + type User struct { + Name string + Age int + Role string + Addr Address + Hobbys []string + salary int + } + + type Employee struct { + Name string + Age int + Role string + Addr Address + Hobbys []string + salary int + } + + user := User{Name: "user001", Age: 10, Role: "Admin", Addr: Address{Country: "CN", ZipCode: "001"}, Hobbys: []string{"a", "b"}, salary: 1000} + + employee1 := Employee{} + CopyProperties(&employee1, &user) + + employee2 := Employee{Name: "employee001", Age: 20, Role: "User", + Addr: Address{Country: "UK", ZipCode: "002"}, salary: 500} + + CopyProperties(&employee2, &user) + + fmt.Println(employee1) + fmt.Println(employee2) + + // Output: + // {user001 10 Admin {CN 001} [a b] 0} + // {user001 10 Admin {CN 001} [a b] 500} +}