-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathstructtype_sample_test.go
More file actions
185 lines (150 loc) · 3.81 KB
/
Copy pathstructtype_sample_test.go
File metadata and controls
185 lines (150 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright 2019 Huan Du. All rights reserved.
// Licensed under the MIT license that can be found in the LICENSE file.
package clone
import (
"encoding/json"
"fmt"
"os"
"reflect"
)
func ExampleMarkAsScalar() {
type ScalarType struct {
stderr *os.File
}
MarkAsScalar(reflect.TypeOf(new(ScalarType)))
scalar := &ScalarType{
stderr: os.Stderr,
}
cloned := Clone(scalar).(*ScalarType)
// cloned is a shadow copy of scalar
// so that the pointer value should be the same.
fmt.Println(scalar.stderr == cloned.stderr)
// Output:
// true
}
func ExampleMarkAsOpaquePointer() {
type OpaquePointerType struct {
foo int
}
MarkAsOpaquePointer(reflect.TypeOf(new(OpaquePointerType)))
opaque := &OpaquePointerType{
foo: 123,
}
cloned := Clone(opaque).(*OpaquePointerType)
// cloned is a shadow copy of opaque.
// so that opaque and cloned should be the same.
fmt.Println(opaque == cloned)
// Output:
// true
}
func ExampleSetCustomFunc() {
type MyStruct struct {
Data []interface{}
}
// Filter nil values in Data when cloning old value.
SetCustomFunc(reflect.TypeOf(MyStruct{}), func(allocator *Allocator, old, new reflect.Value) {
// The new is a zero value of MyStruct.
// We can get its address to update it.
value := new.Addr().Interface().(*MyStruct)
// The old is guaranteed to be a MyStruct.
// As old.CanAddr() may be false, we'd better to read Data field directly.
data := old.FieldByName("Data")
l := data.Len()
for i := 0; i < l; i++ {
val := data.Index(i)
if val.IsNil() {
continue
}
n := allocator.Clone(val).Interface()
value.Data = append(value.Data, n)
}
})
slice := &MyStruct{
Data: []interface{}{
"abc", nil, 123, nil,
},
}
cloned := Clone(slice).(*MyStruct)
fmt.Println(cloned.Data)
// Output:
// [abc 123]
}
func ExampleSetCustomFunc_partiallyClone() {
type T struct {
Value int
}
type MyStruct struct {
S1 *T
S2 string
S3 int
}
SetCustomFunc(reflect.TypeOf(T{}), func(allocator *Allocator, old, new reflect.Value) {
oldField := old.FieldByName("Value")
newField := new.FieldByName("Value")
newField.SetInt(oldField.Int() + 100)
})
SetCustomFunc(reflect.TypeOf(MyStruct{}), func(allocator *Allocator, old, new reflect.Value) {
// We can call allocator.Clone to clone the old value without worrying about dead loop.
// This custom func is temporary disabled for the old value in allocator.
new.Set(allocator.Clone(old))
oldField := old.FieldByName("S2")
newField := new.FieldByName("S2")
newField.SetString(oldField.String() + "_suffix")
})
st := &MyStruct{
S1: &T{
Value: 1,
},
S2: "abc",
S3: 2,
}
cloned := Clone(st).(*MyStruct)
data, _ := json.Marshal(st)
fmt.Println(string(data))
data, _ = json.Marshal(cloned)
fmt.Println(string(data))
// Output:
// {"S1":{"Value":1},"S2":"abc","S3":2}
// {"S1":{"Value":101},"S2":"abc_suffix","S3":2}
}
func ExampleSetCustomFunc_conditionalClonePointer() {
type T struct {
shouldClone bool
data []string
}
type Pointer struct {
*T
}
values := map[string]Pointer{
"shouldClone": {
T: &T{
shouldClone: true,
data: []string{"a", "b", "c"},
},
},
"shouldNotClone": {
T: &T{
shouldClone: false,
data: []string{"a", "b", "c"},
},
},
}
SetCustomFunc(reflect.TypeOf(Pointer{}), func(allocator *Allocator, old, new reflect.Value) {
p := old.Interface().(Pointer)
if p.shouldClone {
np := allocator.Clone(old).Interface().(Pointer)
// Update the cloned value to make the change very obvious.
np.shouldClone = false
np.data = append(np.data, "cloned")
new.Set(reflect.ValueOf(np))
} else {
new.Set(old)
}
})
cloned := Clone(values).(map[string]Pointer)
fmt.Println(cloned["shouldClone"].data)
fmt.Println(cloned["shouldNotClone"].data)
// Output:
// [a b c cloned]
// [a b c]
}