blob: 9e985f1095114081e5f616631113495e434a0e6c [file] [log] [blame]
Colin Cross81695002015-10-30 13:19:14 -07001// Copyright 2014 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package proptools
16
17import (
18 "fmt"
19 "reflect"
Colin Cross01ee36e2016-05-17 13:57:12 -070020 "sync"
Colin Cross81695002015-10-30 13:19:14 -070021)
22
Colin Cross5d57b2d2020-01-27 16:14:31 -080023// CloneProperties takes a reflect.Value of a pointer to a struct and returns a reflect.Value
24// of a pointer to a new struct that copies of the values for its fields. It recursively clones
25// struct pointers and interfaces that contain struct pointers.
Colin Cross81695002015-10-30 13:19:14 -070026func CloneProperties(structValue reflect.Value) reflect.Value {
Colin Cross5d57b2d2020-01-27 16:14:31 -080027 if !isStructPtr(structValue.Type()) {
28 panic(fmt.Errorf("CloneProperties expected *struct, got %s", structValue.Type()))
29 }
30 result := reflect.New(structValue.Type().Elem())
31 copyProperties(result.Elem(), structValue.Elem())
Colin Cross81695002015-10-30 13:19:14 -070032 return result
33}
34
Colin Cross5d57b2d2020-01-27 16:14:31 -080035// CopyProperties takes destination and source reflect.Values of a pointer to structs and returns
36// copies each field from the source into the destination. It recursively copies struct pointers
37// and interfaces that contain struct pointers.
Colin Cross81695002015-10-30 13:19:14 -070038func CopyProperties(dstValue, srcValue reflect.Value) {
Colin Cross5d57b2d2020-01-27 16:14:31 -080039 if !isStructPtr(dstValue.Type()) {
40 panic(fmt.Errorf("CopyProperties expected dstValue *struct, got %s", dstValue.Type()))
41 }
42 if !isStructPtr(srcValue.Type()) {
43 panic(fmt.Errorf("CopyProperties expected srcValue *struct, got %s", srcValue.Type()))
44 }
45 copyProperties(dstValue.Elem(), srcValue.Elem())
46}
47
48func copyProperties(dstValue, srcValue reflect.Value) {
Colin Cross81695002015-10-30 13:19:14 -070049 typ := dstValue.Type()
50 if srcValue.Type() != typ {
51 panic(fmt.Errorf("can't copy mismatching types (%s <- %s)",
52 dstValue.Kind(), srcValue.Kind()))
53 }
54
Colin Cross01ee36e2016-05-17 13:57:12 -070055 for i, field := range typeFields(typ) {
Colin Cross81695002015-10-30 13:19:14 -070056 if field.PkgPath != "" {
Jaewoong Jung7d699c32019-03-07 14:01:22 -080057 panic(fmt.Errorf("can't copy a private field %q", field.Name))
Colin Cross81695002015-10-30 13:19:14 -070058 }
59
60 srcFieldValue := srcValue.Field(i)
61 dstFieldValue := dstValue.Field(i)
Colin Crossf72ef502015-10-30 11:42:57 -070062 dstFieldInterfaceValue := reflect.Value{}
Colin Crossc3d73122016-08-05 17:19:36 -070063 origDstFieldValue := dstFieldValue
Colin Cross81695002015-10-30 13:19:14 -070064
65 switch srcFieldValue.Kind() {
66 case reflect.Bool, reflect.String, reflect.Int, reflect.Uint:
67 dstFieldValue.Set(srcFieldValue)
68 case reflect.Struct:
Colin Cross5d57b2d2020-01-27 16:14:31 -080069 copyProperties(dstFieldValue, srcFieldValue)
Colin Cross81695002015-10-30 13:19:14 -070070 case reflect.Slice:
71 if !srcFieldValue.IsNil() {
Colin Cross81695002015-10-30 13:19:14 -070072 if srcFieldValue != dstFieldValue {
73 newSlice := reflect.MakeSlice(field.Type, srcFieldValue.Len(),
74 srcFieldValue.Len())
75 reflect.Copy(newSlice, srcFieldValue)
76 dstFieldValue.Set(newSlice)
77 }
78 } else {
79 dstFieldValue.Set(srcFieldValue)
80 }
Colin Crossf72ef502015-10-30 11:42:57 -070081 case reflect.Interface:
82 if srcFieldValue.IsNil() {
Colin Cross81695002015-10-30 13:19:14 -070083 dstFieldValue.Set(srcFieldValue)
Colin Crossf72ef502015-10-30 11:42:57 -070084 break
85 }
86
87 srcFieldValue = srcFieldValue.Elem()
88
Colin Cross6898d262020-01-27 16:48:30 -080089 if !isStructPtr(srcFieldValue.Type()) {
90 panic(fmt.Errorf("can't clone field %q: expected interface to contain *struct, found %s",
91 field.Name, srcFieldValue.Type()))
Colin Crossf72ef502015-10-30 11:42:57 -070092 }
93
94 if dstFieldValue.IsNil() || dstFieldValue.Elem().Type() != srcFieldValue.Type() {
95 // We can't use the existing destination allocation, so
96 // clone a new one.
97 newValue := reflect.New(srcFieldValue.Type()).Elem()
98 dstFieldValue.Set(newValue)
99 dstFieldInterfaceValue = dstFieldValue
100 dstFieldValue = newValue
101 } else {
102 dstFieldValue = dstFieldValue.Elem()
103 }
104 fallthrough
105 case reflect.Ptr:
Colin Crossf72ef502015-10-30 11:42:57 -0700106 if srcFieldValue.IsNil() {
Colin Crossc3d73122016-08-05 17:19:36 -0700107 origDstFieldValue.Set(srcFieldValue)
Colin Crossf72ef502015-10-30 11:42:57 -0700108 break
109 }
110
Colin Cross5d57b2d2020-01-27 16:14:31 -0800111 switch srcFieldValue.Elem().Kind() {
Colin Cross80117682015-10-30 15:53:55 -0700112 case reflect.Struct:
113 if !dstFieldValue.IsNil() {
114 // Re-use the existing allocation.
Colin Cross5d57b2d2020-01-27 16:14:31 -0800115 copyProperties(dstFieldValue.Elem(), srcFieldValue.Elem())
Colin Cross80117682015-10-30 15:53:55 -0700116 break
Colin Crossf72ef502015-10-30 11:42:57 -0700117 } else {
Colin Cross80117682015-10-30 15:53:55 -0700118 newValue := CloneProperties(srcFieldValue)
119 if dstFieldInterfaceValue.IsValid() {
120 dstFieldInterfaceValue.Set(newValue)
121 } else {
Colin Crossc3d73122016-08-05 17:19:36 -0700122 origDstFieldValue.Set(newValue)
Colin Cross80117682015-10-30 15:53:55 -0700123 }
Colin Crossf72ef502015-10-30 11:42:57 -0700124 }
Nan Zhangf5865442017-11-01 14:03:28 -0700125 case reflect.Bool, reflect.Int64, reflect.String:
Colin Cross5d57b2d2020-01-27 16:14:31 -0800126 newValue := reflect.New(srcFieldValue.Elem().Type())
127 newValue.Elem().Set(srcFieldValue.Elem())
Colin Crossc3d73122016-08-05 17:19:36 -0700128 origDstFieldValue.Set(newValue)
Colin Cross80117682015-10-30 15:53:55 -0700129 default:
Colin Cross5d57b2d2020-01-27 16:14:31 -0800130 panic(fmt.Errorf("can't clone pointer field %q type %s",
131 field.Name, srcFieldValue.Type()))
Colin Cross81695002015-10-30 13:19:14 -0700132 }
133 default:
Colin Cross5d57b2d2020-01-27 16:14:31 -0800134 panic(fmt.Errorf("unexpected type for property struct field %q: %s",
135 field.Name, srcFieldValue.Type()))
Colin Cross81695002015-10-30 13:19:14 -0700136 }
137 }
138}
139
Colin Cross5d57b2d2020-01-27 16:14:31 -0800140// ZeroProperties takes a reflect.Value of a pointer to a struct and replaces all of its fields
141// with zero values, recursing into struct, pointer to struct and interface fields.
Colin Cross81695002015-10-30 13:19:14 -0700142func ZeroProperties(structValue reflect.Value) {
Colin Cross5d57b2d2020-01-27 16:14:31 -0800143 if !isStructPtr(structValue.Type()) {
144 panic(fmt.Errorf("ZeroProperties expected *struct, got %s", structValue.Type()))
145 }
146 zeroProperties(structValue.Elem())
147}
148
149func zeroProperties(structValue reflect.Value) {
Colin Cross81695002015-10-30 13:19:14 -0700150 typ := structValue.Type()
151
Colin Cross01ee36e2016-05-17 13:57:12 -0700152 for i, field := range typeFields(typ) {
Colin Cross81695002015-10-30 13:19:14 -0700153 if field.PkgPath != "" {
154 // The field is not exported so just skip it.
155 continue
156 }
157
158 fieldValue := structValue.Field(i)
159
160 switch fieldValue.Kind() {
Colin Cross83cedbe2015-11-20 17:01:25 -0800161 case reflect.Bool, reflect.String, reflect.Slice, reflect.Int, reflect.Uint:
Colin Cross81695002015-10-30 13:19:14 -0700162 fieldValue.Set(reflect.Zero(fieldValue.Type()))
Colin Crossf72ef502015-10-30 11:42:57 -0700163 case reflect.Interface:
164 if fieldValue.IsNil() {
165 break
Colin Cross81695002015-10-30 13:19:14 -0700166 }
Colin Crossf72ef502015-10-30 11:42:57 -0700167
168 // We leave the pointer intact and zero out the struct that's
169 // pointed to.
170 fieldValue = fieldValue.Elem()
Colin Cross6898d262020-01-27 16:48:30 -0800171 if !isStructPtr(fieldValue.Type()) {
172 panic(fmt.Errorf("can't zero field %q: expected interface to contain *struct, found %s",
173 field.Name, fieldValue.Type()))
Colin Crossf72ef502015-10-30 11:42:57 -0700174 }
175 fallthrough
176 case reflect.Ptr:
Colin Cross80117682015-10-30 15:53:55 -0700177 switch fieldValue.Type().Elem().Kind() {
178 case reflect.Struct:
179 if fieldValue.IsNil() {
180 break
181 }
Colin Cross5d57b2d2020-01-27 16:14:31 -0800182 zeroProperties(fieldValue.Elem())
Nan Zhangf5865442017-11-01 14:03:28 -0700183 case reflect.Bool, reflect.Int64, reflect.String:
Colin Cross80117682015-10-30 15:53:55 -0700184 fieldValue.Set(reflect.Zero(fieldValue.Type()))
185 default:
186 panic(fmt.Errorf("can't zero field %q: points to a %s",
187 field.Name, fieldValue.Elem().Kind()))
Colin Crossf72ef502015-10-30 11:42:57 -0700188 }
Colin Cross83cedbe2015-11-20 17:01:25 -0800189 case reflect.Struct:
Colin Cross5d57b2d2020-01-27 16:14:31 -0800190 zeroProperties(fieldValue)
Colin Cross81695002015-10-30 13:19:14 -0700191 default:
192 panic(fmt.Errorf("unexpected kind for property struct field %q: %s",
193 field.Name, fieldValue.Kind()))
194 }
195 }
196}
197
Colin Cross5d57b2d2020-01-27 16:14:31 -0800198// CloneEmptyProperties takes a reflect.Value of a pointer to a struct and returns a reflect.Value
199// of a pointer to a new struct that has the zero values for its fields. It recursively clones
200// struct pointers and interfaces that contain struct pointers.
Colin Cross81695002015-10-30 13:19:14 -0700201func CloneEmptyProperties(structValue reflect.Value) reflect.Value {
Colin Cross5d57b2d2020-01-27 16:14:31 -0800202 if !isStructPtr(structValue.Type()) {
203 panic(fmt.Errorf("CloneEmptyProperties expected *struct, got %s", structValue.Type()))
204 }
205 result := reflect.New(structValue.Type().Elem())
206 cloneEmptyProperties(result.Elem(), structValue.Elem())
Colin Cross81695002015-10-30 13:19:14 -0700207 return result
208}
209
210func cloneEmptyProperties(dstValue, srcValue reflect.Value) {
211 typ := srcValue.Type()
Colin Cross01ee36e2016-05-17 13:57:12 -0700212 for i, field := range typeFields(typ) {
Colin Cross81695002015-10-30 13:19:14 -0700213 if field.PkgPath != "" {
214 // The field is not exported so just skip it.
215 continue
216 }
217
218 srcFieldValue := srcValue.Field(i)
219 dstFieldValue := dstValue.Field(i)
Colin Crossf72ef502015-10-30 11:42:57 -0700220 dstFieldInterfaceValue := reflect.Value{}
Colin Cross81695002015-10-30 13:19:14 -0700221
222 switch srcFieldValue.Kind() {
223 case reflect.Bool, reflect.String, reflect.Slice, reflect.Int, reflect.Uint:
224 // Nothing
225 case reflect.Struct:
226 cloneEmptyProperties(dstFieldValue, srcFieldValue)
Colin Crossf72ef502015-10-30 11:42:57 -0700227 case reflect.Interface:
228 if srcFieldValue.IsNil() {
229 break
230 }
231
232 srcFieldValue = srcFieldValue.Elem()
Colin Cross6898d262020-01-27 16:48:30 -0800233 if !isStructPtr(srcFieldValue.Type()) {
234 panic(fmt.Errorf("can't clone empty field %q: expected interface to contain *struct, found %s",
235 field.Name, srcFieldValue.Type()))
Colin Crossf72ef502015-10-30 11:42:57 -0700236 }
237
238 newValue := reflect.New(srcFieldValue.Type()).Elem()
239 dstFieldValue.Set(newValue)
240 dstFieldInterfaceValue = dstFieldValue
241 dstFieldValue = newValue
242 fallthrough
243 case reflect.Ptr:
Colin Cross80117682015-10-30 15:53:55 -0700244 switch srcFieldValue.Type().Elem().Kind() {
245 case reflect.Struct:
246 if srcFieldValue.IsNil() {
247 break
248 }
Colin Cross5d57b2d2020-01-27 16:14:31 -0800249 newValue := CloneEmptyProperties(srcFieldValue)
Colin Cross80117682015-10-30 15:53:55 -0700250 if dstFieldInterfaceValue.IsValid() {
251 dstFieldInterfaceValue.Set(newValue)
252 } else {
253 dstFieldValue.Set(newValue)
254 }
Nan Zhangf5865442017-11-01 14:03:28 -0700255 case reflect.Bool, reflect.Int64, reflect.String:
Colin Cross80117682015-10-30 15:53:55 -0700256 // Nothing
257 default:
258 panic(fmt.Errorf("can't clone empty field %q: points to a %s",
259 field.Name, srcFieldValue.Elem().Kind()))
Colin Crossf72ef502015-10-30 11:42:57 -0700260 }
261
Colin Cross81695002015-10-30 13:19:14 -0700262 default:
263 panic(fmt.Errorf("unexpected kind for property struct field %q: %s",
264 field.Name, srcFieldValue.Kind()))
265 }
266 }
267}
Colin Cross01ee36e2016-05-17 13:57:12 -0700268
Colin Cross937efab2019-01-23 13:24:26 -0800269var typeFieldCache sync.Map
Colin Cross01ee36e2016-05-17 13:57:12 -0700270
271func typeFields(typ reflect.Type) []reflect.StructField {
Colin Cross937efab2019-01-23 13:24:26 -0800272 // reflect.Type.Field allocates a []int{} to hold the index every time it is called, which ends up
273 // being a significant portion of the GC pressure. It can't reuse the same one in case a caller
274 // modifies the backing array through the slice. Since we don't modify it, cache the result
275 // locally to reduce allocations.
276
Colin Cross01ee36e2016-05-17 13:57:12 -0700277 // Fast path
Colin Cross937efab2019-01-23 13:24:26 -0800278 if typeFields, ok := typeFieldCache.Load(typ); ok {
279 return typeFields.([]reflect.StructField)
Colin Cross01ee36e2016-05-17 13:57:12 -0700280 }
281
282 // Slow path
283 typeFields := make([]reflect.StructField, typ.NumField())
284
285 for i := range typeFields {
286 typeFields[i] = typ.Field(i)
287 }
288
Colin Cross937efab2019-01-23 13:24:26 -0800289 typeFieldCache.Store(typ, typeFields)
Colin Cross01ee36e2016-05-17 13:57:12 -0700290
291 return typeFields
292}