github.com/aavshr/aws-sdk-go@v1.41.3/aws/awsutil/copy_test.go (about) 1 package awsutil_test 2 3 import ( 4 "bytes" 5 "fmt" 6 "io" 7 "io/ioutil" 8 "reflect" 9 "testing" 10 "time" 11 12 "github.com/aavshr/aws-sdk-go/aws/awsutil" 13 ) 14 15 func ExampleCopy() { 16 type Foo struct { 17 A int 18 B []*string 19 } 20 21 // Create the initial value 22 str1 := "hello" 23 str2 := "bye bye" 24 f1 := &Foo{A: 1, B: []*string{&str1, &str2}} 25 26 // Do the copy 27 var f2 Foo 28 awsutil.Copy(&f2, f1) 29 30 // Print the result 31 fmt.Println(awsutil.Prettify(f2)) 32 33 // Output: 34 // { 35 // A: 1, 36 // B: ["hello","bye bye"] 37 // } 38 } 39 40 func TestCopy1(t *testing.T) { 41 type Bar struct { 42 a *int 43 B *int 44 c int 45 D int 46 } 47 type Foo struct { 48 A int 49 B []*string 50 C map[string]*int 51 D *time.Time 52 E *Bar 53 } 54 55 // Create the initial value 56 str1 := "hello" 57 str2 := "bye bye" 58 int1 := 1 59 int2 := 2 60 intPtr1 := 1 61 intPtr2 := 2 62 now := time.Now() 63 f1 := &Foo{ 64 A: 1, 65 B: []*string{&str1, &str2}, 66 C: map[string]*int{ 67 "A": &int1, 68 "B": &int2, 69 }, 70 D: &now, 71 E: &Bar{ 72 &intPtr1, 73 &intPtr2, 74 2, 75 3, 76 }, 77 } 78 79 // Do the copy 80 var f2 Foo 81 awsutil.Copy(&f2, f1) 82 83 // Values are equal 84 if v1, v2 := f2.A, f1.A; v1 != v2 { 85 t.Errorf("expected values to be equivalent but received %v and %v", v1, v2) 86 } 87 if v1, v2 := f2.B, f1.B; !reflect.DeepEqual(v1, v2) { 88 t.Errorf("expected values to be equivalent but received %v and %v", v1, v2) 89 } 90 if v1, v2 := f2.C, f1.C; !reflect.DeepEqual(v1, v2) { 91 t.Errorf("expected values to be equivalent but received %v and %v", v1, v2) 92 } 93 if v1, v2 := f2.D, f1.D; !v1.Equal(*v2) { 94 t.Errorf("expected values to be equivalent but received %v and %v", v1, v2) 95 } 96 if v1, v2 := f2.E.B, f1.E.B; !reflect.DeepEqual(v1, v2) { 97 t.Errorf("expected values to be equivalent but received %v and %v", v1, v2) 98 } 99 if v1, v2 := f2.E.D, f1.E.D; v1 != v2 { 100 t.Errorf("expected values to be equivalent but received %v and %v", v1, v2) 101 } 102 103 // But pointers are not! 104 str3 := "nothello" 105 int3 := 57 106 f2.A = 100 107 *f2.B[0] = str3 108 *f2.C["B"] = int3 109 *f2.D = time.Now() 110 f2.E.a = &int3 111 *f2.E.B = int3 112 f2.E.c = 5 113 f2.E.D = 5 114 if v1, v2 := f2.A, f1.A; v1 == v2 { 115 t.Errorf("expected values to be not equivalent, but received %v", v1) 116 } 117 if v1, v2 := f2.B, f1.B; reflect.DeepEqual(v1, v2) { 118 t.Errorf("expected values to be not equivalent, but received %v", v1) 119 } 120 if v1, v2 := f2.C, f1.C; reflect.DeepEqual(v1, v2) { 121 t.Errorf("expected values to be not equivalent, but received %v", v1) 122 } 123 if v1, v2 := f2.D, f1.D; v1 == v2 { 124 t.Errorf("expected values to be not equivalent, but received %v", v1) 125 } 126 if v1, v2 := f2.E.a, f1.E.a; v1 == v2 { 127 t.Errorf("expected values to be not equivalent, but received %v", v1) 128 } 129 if v1, v2 := f2.E.B, f1.E.B; v1 == v2 { 130 t.Errorf("expected values to be not equivalent, but received %v", v1) 131 } 132 if v1, v2 := f2.E.c, f1.E.c; v1 == v2 { 133 t.Errorf("expected values to be not equivalent, but received %v", v1) 134 } 135 if v1, v2 := f2.E.D, f1.E.D; v1 == v2 { 136 t.Errorf("expected values to be not equivalent, but received %v", v1) 137 } 138 } 139 140 func TestCopyNestedWithUnexported(t *testing.T) { 141 type Bar struct { 142 a int 143 B int 144 } 145 type Foo struct { 146 A string 147 B Bar 148 } 149 150 f1 := &Foo{A: "string", B: Bar{a: 1, B: 2}} 151 152 var f2 Foo 153 awsutil.Copy(&f2, f1) 154 155 // Values match 156 if v1, v2 := f2.A, f1.A; v1 != v2 { 157 t.Errorf("expected values to be equivalent but received %v and %v", v1, v2) 158 } 159 if v1, v2 := f2.B, f1.B; v1 == v2 { 160 t.Errorf("expected values to be not equivalent, but received %v", v1) 161 } 162 if v1, v2 := f2.B.a, f1.B.a; v1 == v2 { 163 t.Errorf("expected values to be not equivalent, but received %v", v1) 164 } 165 if v1, v2 := f2.B.B, f2.B.B; v1 != v2 { 166 t.Errorf("expected values to be equivalent but received %v and %v", v1, v2) 167 } 168 } 169 170 func TestCopyIgnoreNilMembers(t *testing.T) { 171 type Foo struct { 172 A *string 173 B []string 174 C map[string]string 175 } 176 177 f := &Foo{} 178 if v1 := f.A; v1 != nil { 179 t.Errorf("expected nil, but received %v", v1) 180 } 181 if v1 := f.B; v1 != nil { 182 t.Errorf("expected nil, but received %v", v1) 183 } 184 if v1 := f.C; v1 != nil { 185 t.Errorf("expected nil, but received %v", v1) 186 } 187 188 var f2 Foo 189 awsutil.Copy(&f2, f) 190 if v1 := f2.A; v1 != nil { 191 t.Errorf("expected nil, but received %v", v1) 192 } 193 if v1 := f2.B; v1 != nil { 194 t.Errorf("expected nil, but received %v", v1) 195 } 196 if v1 := f2.C; v1 != nil { 197 t.Errorf("expected nil, but received %v", v1) 198 } 199 200 fcopy := awsutil.CopyOf(f) 201 f3 := fcopy.(*Foo) 202 if v1 := f3.A; v1 != nil { 203 t.Errorf("expected nil, but received %v", v1) 204 } 205 if v1 := f3.B; v1 != nil { 206 t.Errorf("expected nil, but received %v", v1) 207 } 208 if v1 := f3.C; v1 != nil { 209 t.Errorf("expected nil, but received %v", v1) 210 } 211 } 212 213 func TestCopyPrimitive(t *testing.T) { 214 str := "hello" 215 var s string 216 awsutil.Copy(&s, &str) 217 if v1, v2 := "hello", s; v1 != v2 { 218 t.Errorf("expected values to be equivalent but received %v and %v", v1, v2) 219 } 220 } 221 222 func TestCopyNil(t *testing.T) { 223 var s string 224 awsutil.Copy(&s, nil) 225 if v1, v2 := "", s; v1 != v2 { 226 t.Errorf("expected values to be equivalent but received %v and %v", v1, v2) 227 } 228 } 229 230 func TestCopyReader(t *testing.T) { 231 var buf io.Reader = bytes.NewReader([]byte("hello world")) 232 var r io.Reader 233 awsutil.Copy(&r, buf) 234 b, err := ioutil.ReadAll(r) 235 if err != nil { 236 t.Errorf("expected no error, but received %v", err) 237 } 238 if v1, v2 := []byte("hello world"), b; !bytes.Equal(v1, v2) { 239 t.Errorf("expected values to be equivalent but received %v and %v", v1, v2) 240 } 241 242 // empty bytes because this is not a deep copy 243 b, err = ioutil.ReadAll(buf) 244 if err != nil { 245 t.Errorf("expected no error, but received %v", err) 246 } 247 if v1, v2 := []byte(""), b; !bytes.Equal(v1, v2) { 248 t.Errorf("expected values to be equivalent but received %v and %v", v1, v2) 249 } 250 } 251 252 func TestCopyDifferentStructs(t *testing.T) { 253 type SrcFoo struct { 254 A int 255 B []*string 256 C map[string]*int 257 SrcUnique string 258 SameNameDiffType int 259 unexportedPtr *int 260 ExportedPtr *int 261 } 262 type DstFoo struct { 263 A int 264 B []*string 265 C map[string]*int 266 DstUnique int 267 SameNameDiffType string 268 unexportedPtr *int 269 ExportedPtr *int 270 } 271 272 // Create the initial value 273 str1 := "hello" 274 str2 := "bye bye" 275 int1 := 1 276 int2 := 2 277 f1 := &SrcFoo{ 278 A: 1, 279 B: []*string{&str1, &str2}, 280 C: map[string]*int{ 281 "A": &int1, 282 "B": &int2, 283 }, 284 SrcUnique: "unique", 285 SameNameDiffType: 1, 286 unexportedPtr: &int1, 287 ExportedPtr: &int2, 288 } 289 290 // Do the copy 291 var f2 DstFoo 292 awsutil.Copy(&f2, f1) 293 294 // Values are equal 295 if v1, v2 := f2.A, f1.A; v1 != v2 { 296 t.Errorf("expected values to be equivalent but received %v and %v", v1, v2) 297 } 298 if v1, v2 := f2.B, f1.B; !reflect.DeepEqual(v1, v2) { 299 t.Errorf("expected values to be equivalent but received %v and %v", v1, v2) 300 } 301 if v1, v2 := f2.C, f1.C; !reflect.DeepEqual(v1, v2) { 302 t.Errorf("expected values to be equivalent but received %v and %v", v1, v2) 303 } 304 if v1, v2 := "unique", f1.SrcUnique; v1 != v2 { 305 t.Errorf("expected values to be equivalent but received %v and %v", v1, v2) 306 } 307 if v1, v2 := 1, f1.SameNameDiffType; v1 != v2 { 308 t.Errorf("expected values to be equivalent but received %v and %v", v1, v2) 309 } 310 if v1, v2 := 0, f2.DstUnique; v1 != v2 { 311 t.Errorf("expected values to be equivalent but received %v and %v", v1, v2) 312 } 313 if v1, v2 := "", f2.SameNameDiffType; v1 != v2 { 314 t.Errorf("expected values to be equivalent but received %v and %v", v1, v2) 315 } 316 if v1, v2 := int1, *f1.unexportedPtr; v1 != v2 { 317 t.Errorf("expected values to be equivalent but received %v and %v", v1, v2) 318 } 319 if v1 := f2.unexportedPtr; v1 != nil { 320 t.Errorf("expected nil, but received %v", v1) 321 } 322 if v1, v2 := int2, *f1.ExportedPtr; v1 != v2 { 323 t.Errorf("expected values to be equivalent but received %v and %v", v1, v2) 324 } 325 if v1, v2 := int2, *f2.ExportedPtr; v1 != v2 { 326 t.Errorf("expected values to be equivalent but received %v and %v", v1, v2) 327 } 328 } 329 330 func ExampleCopyOf() { 331 type Foo struct { 332 A int 333 B []*string 334 } 335 336 // Create the initial value 337 str1 := "hello" 338 str2 := "bye bye" 339 f1 := &Foo{A: 1, B: []*string{&str1, &str2}} 340 341 // Do the copy 342 v := awsutil.CopyOf(f1) 343 var f2 *Foo = v.(*Foo) 344 345 // Print the result 346 fmt.Println(awsutil.Prettify(f2)) 347 348 // Output: 349 // { 350 // A: 1, 351 // B: ["hello","bye bye"] 352 // } 353 }