copy_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package awsutil_test
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "testing"
  8. "github.com/aws/aws-sdk-go/aws/awsutil"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func ExampleCopy() {
  12. type Foo struct {
  13. A int
  14. B []*string
  15. }
  16. // Create the initial value
  17. str1 := "hello"
  18. str2 := "bye bye"
  19. f1 := &Foo{A: 1, B: []*string{&str1, &str2}}
  20. // Do the copy
  21. var f2 Foo
  22. awsutil.Copy(&f2, f1)
  23. // Print the result
  24. fmt.Println(awsutil.Prettify(f2))
  25. // Output:
  26. // {
  27. // A: 1,
  28. // B: ["hello","bye bye"]
  29. // }
  30. }
  31. func TestCopy(t *testing.T) {
  32. type Foo struct {
  33. A int
  34. B []*string
  35. C map[string]*int
  36. }
  37. // Create the initial value
  38. str1 := "hello"
  39. str2 := "bye bye"
  40. int1 := 1
  41. int2 := 2
  42. f1 := &Foo{
  43. A: 1,
  44. B: []*string{&str1, &str2},
  45. C: map[string]*int{
  46. "A": &int1,
  47. "B": &int2,
  48. },
  49. }
  50. // Do the copy
  51. var f2 Foo
  52. awsutil.Copy(&f2, f1)
  53. // Values are equal
  54. assert.Equal(t, f2.A, f1.A)
  55. assert.Equal(t, f2.B, f1.B)
  56. assert.Equal(t, f2.C, f1.C)
  57. // But pointers are not!
  58. str3 := "nothello"
  59. int3 := 57
  60. f2.A = 100
  61. f2.B[0] = &str3
  62. f2.C["B"] = &int3
  63. assert.NotEqual(t, f2.A, f1.A)
  64. assert.NotEqual(t, f2.B, f1.B)
  65. assert.NotEqual(t, f2.C, f1.C)
  66. }
  67. func TestCopyNestedWithUnexported(t *testing.T) {
  68. type Bar struct {
  69. a int
  70. B int
  71. }
  72. type Foo struct {
  73. A string
  74. B Bar
  75. }
  76. f1 := &Foo{A: "string", B: Bar{a: 1, B: 2}}
  77. var f2 Foo
  78. awsutil.Copy(&f2, f1)
  79. // Values match
  80. assert.Equal(t, f2.A, f1.A)
  81. assert.NotEqual(t, f2.B, f1.B)
  82. assert.NotEqual(t, f2.B.a, f1.B.a)
  83. assert.Equal(t, f2.B.B, f2.B.B)
  84. }
  85. func TestCopyIgnoreNilMembers(t *testing.T) {
  86. type Foo struct {
  87. A *string
  88. B []string
  89. C map[string]string
  90. }
  91. f := &Foo{}
  92. assert.Nil(t, f.A)
  93. assert.Nil(t, f.B)
  94. assert.Nil(t, f.C)
  95. var f2 Foo
  96. awsutil.Copy(&f2, f)
  97. assert.Nil(t, f2.A)
  98. assert.Nil(t, f2.B)
  99. assert.Nil(t, f2.C)
  100. fcopy := awsutil.CopyOf(f)
  101. f3 := fcopy.(*Foo)
  102. assert.Nil(t, f3.A)
  103. assert.Nil(t, f3.B)
  104. assert.Nil(t, f3.C)
  105. }
  106. func TestCopyPrimitive(t *testing.T) {
  107. str := "hello"
  108. var s string
  109. awsutil.Copy(&s, &str)
  110. assert.Equal(t, "hello", s)
  111. }
  112. func TestCopyNil(t *testing.T) {
  113. var s string
  114. awsutil.Copy(&s, nil)
  115. assert.Equal(t, "", s)
  116. }
  117. func TestCopyReader(t *testing.T) {
  118. var buf io.Reader = bytes.NewReader([]byte("hello world"))
  119. var r io.Reader
  120. awsutil.Copy(&r, buf)
  121. b, err := ioutil.ReadAll(r)
  122. assert.NoError(t, err)
  123. assert.Equal(t, []byte("hello world"), b)
  124. // empty bytes because this is not a deep copy
  125. b, err = ioutil.ReadAll(buf)
  126. assert.NoError(t, err)
  127. assert.Equal(t, []byte(""), b)
  128. }
  129. func TestCopyDifferentStructs(t *testing.T) {
  130. type SrcFoo struct {
  131. A int
  132. B []*string
  133. C map[string]*int
  134. SrcUnique string
  135. SameNameDiffType int
  136. unexportedPtr *int
  137. ExportedPtr *int
  138. }
  139. type DstFoo struct {
  140. A int
  141. B []*string
  142. C map[string]*int
  143. DstUnique int
  144. SameNameDiffType string
  145. unexportedPtr *int
  146. ExportedPtr *int
  147. }
  148. // Create the initial value
  149. str1 := "hello"
  150. str2 := "bye bye"
  151. int1 := 1
  152. int2 := 2
  153. f1 := &SrcFoo{
  154. A: 1,
  155. B: []*string{&str1, &str2},
  156. C: map[string]*int{
  157. "A": &int1,
  158. "B": &int2,
  159. },
  160. SrcUnique: "unique",
  161. SameNameDiffType: 1,
  162. unexportedPtr: &int1,
  163. ExportedPtr: &int2,
  164. }
  165. // Do the copy
  166. var f2 DstFoo
  167. awsutil.Copy(&f2, f1)
  168. // Values are equal
  169. assert.Equal(t, f2.A, f1.A)
  170. assert.Equal(t, f2.B, f1.B)
  171. assert.Equal(t, f2.C, f1.C)
  172. assert.Equal(t, "unique", f1.SrcUnique)
  173. assert.Equal(t, 1, f1.SameNameDiffType)
  174. assert.Equal(t, 0, f2.DstUnique)
  175. assert.Equal(t, "", f2.SameNameDiffType)
  176. assert.Equal(t, int1, *f1.unexportedPtr)
  177. assert.Nil(t, f2.unexportedPtr)
  178. assert.Equal(t, int2, *f1.ExportedPtr)
  179. assert.Equal(t, int2, *f2.ExportedPtr)
  180. }
  181. func ExampleCopyOf() {
  182. type Foo struct {
  183. A int
  184. B []*string
  185. }
  186. // Create the initial value
  187. str1 := "hello"
  188. str2 := "bye bye"
  189. f1 := &Foo{A: 1, B: []*string{&str1, &str2}}
  190. // Do the copy
  191. v := awsutil.CopyOf(f1)
  192. var f2 *Foo = v.(*Foo)
  193. // Print the result
  194. fmt.Println(awsutil.Prettify(f2))
  195. // Output:
  196. // {
  197. // A: 1,
  198. // B: ["hello","bye bye"]
  199. // }
  200. }