| 1 |
// Copyright 2019 Huan Du. All rights reserved. |
| 2 |
// Licensed under the MIT license that can be found in the LICENSE file. |
| 3 |
|
| 4 |
package clone |
| 5 |
|
| 6 |
import ( |
| 7 |
"encoding/json" |
| 8 |
"fmt" |
| 9 |
"os" |
| 10 |
"reflect" |
| 11 |
) |
| 12 |
|
| 13 |
func ExampleMarkAsScalar() { |
| 14 |
type ScalarType struct { |
| 15 |
stderr *os.File |
| 16 |
} |
| 17 |
|
| 18 |
MarkAsScalar(reflect.TypeOf(new(ScalarType))) |
| 19 |
|
| 20 |
scalar := &ScalarType{ |
| 21 |
stderr: os.Stderr, |
| 22 |
} |
| 23 |
cloned := Clone(scalar).(*ScalarType) |
| 24 |
|
| 25 |
// cloned is a shadow copy of scalar |
| 26 |
// so that the pointer value should be the same. |
| 27 |
fmt.Println(scalar.stderr == cloned.stderr) |
| 28 |
|
| 29 |
// Output: |
| 30 |
// true |
| 31 |
} |
| 32 |
|
| 33 |
func ExampleMarkAsOpaquePointer() { |
| 34 |
type OpaquePointerType struct { |
| 35 |
foo int |
| 36 |
} |
| 37 |
|
| 38 |
MarkAsOpaquePointer(reflect.TypeOf(new(OpaquePointerType))) |
| 39 |
|
| 40 |
opaque := &OpaquePointerType{ |
| 41 |
foo: 123, |
| 42 |
} |
| 43 |
cloned := Clone(opaque).(*OpaquePointerType) |
| 44 |
|
| 45 |
// cloned is a shadow copy of opaque. |
| 46 |
// so that opaque and cloned should be the same. |
| 47 |
fmt.Println(opaque == cloned) |
| 48 |
|
| 49 |
// Output: |
| 50 |
// true |
| 51 |
} |
| 52 |
|
| 53 |
func ExampleSetCustomFunc() { |
| 54 |
type MyStruct struct { |
| 55 |
Data []interface{} |
| 56 |
} |
| 57 |
|
| 58 |
// Filter nil values in Data when cloning old value. |
| 59 |
SetCustomFunc(reflect.TypeOf(MyStruct{}), func(allocator *Allocator, old, new reflect.Value) { |
| 60 |
// The new is a zero value of MyStruct. |
| 61 |
// We can get its address to update it. |
| 62 |
value := new.Addr().Interface().(*MyStruct) |
| 63 |
|
| 64 |
// The old is guaranteed to be a MyStruct. |
| 65 |
// As old.CanAddr() may be false, we'd better to read Data field directly. |
| 66 |
data := old.FieldByName("Data") |
| 67 |
l := data.Len() |
| 68 |
|
| 69 |
for i := 0; i < l; i++ { |
| 70 |
val := data.Index(i) |
| 71 |
|
| 72 |
if val.IsNil() { |
| 73 |
continue |
| 74 |
} |
| 75 |
|
| 76 |
n := allocator.Clone(val).Interface() |
| 77 |
value.Data = append(value.Data, n) |
| 78 |
} |
| 79 |
}) |
| 80 |
|
| 81 |
slice := &MyStruct{ |
| 82 |
Data: []interface{}{ |
| 83 |
"abc", nil, 123, nil, |
| 84 |
}, |
| 85 |
} |
| 86 |
cloned := Clone(slice).(*MyStruct) |
| 87 |
fmt.Println(cloned.Data) |
| 88 |
|
| 89 |
// Output: |
| 90 |
// [abc 123] |
| 91 |
} |
| 92 |
|
| 93 |
func ExampleSetCustomFunc_partiallyClone() { |
| 94 |
type T struct { |
| 95 |
Value int |
| 96 |
} |
| 97 |
|
| 98 |
type MyStruct struct { |
| 99 |
S1 *T |
| 100 |
S2 string |
| 101 |
S3 int |
| 102 |
} |
| 103 |
|
| 104 |
SetCustomFunc(reflect.TypeOf(T{}), func(allocator *Allocator, old, new reflect.Value) { |
| 105 |
oldField := old.FieldByName("Value") |
| 106 |
newField := new.FieldByName("Value") |
| 107 |
newField.SetInt(oldField.Int() + 100) |
| 108 |
}) |
| 109 |
|
| 110 |
SetCustomFunc(reflect.TypeOf(MyStruct{}), func(allocator *Allocator, old, new reflect.Value) { |
| 111 |
// We can call allocator.Clone to clone the old value without worrying about dead loop. |
| 112 |
// This custom func is temporary disabled for the old value in allocator. |
| 113 |
new.Set(allocator.Clone(old)) |
| 114 |
|
| 115 |
oldField := old.FieldByName("S2") |
| 116 |
newField := new.FieldByName("S2") |
| 117 |
newField.SetString(oldField.String() + "_suffix") |
| 118 |
}) |
| 119 |
|
| 120 |
st := &MyStruct{ |
| 121 |
S1: &T{ |
| 122 |
Value: 1, |
| 123 |
}, |
| 124 |
S2: "abc", |
| 125 |
S3: 2, |
| 126 |
} |
| 127 |
cloned := Clone(st).(*MyStruct) |
| 128 |
|
| 129 |
data, _ := json.Marshal(st) |
| 130 |
fmt.Println(string(data)) |
| 131 |
data, _ = json.Marshal(cloned) |
| 132 |
fmt.Println(string(data)) |
| 133 |
|
| 134 |
// Output: |
| 135 |
// {"S1":{"Value":1},"S2":"abc","S3":2} |
| 136 |
// {"S1":{"Value":101},"S2":"abc_suffix","S3":2} |
| 137 |
} |