Colin Cross | 8169500 | 2015-10-30 13:19:14 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package proptools |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "reflect" |
Colin Cross | 01ee36e | 2016-05-17 13:57:12 -0700 | [diff] [blame] | 20 | "sync" |
Colin Cross | 8169500 | 2015-10-30 13:19:14 -0700 | [diff] [blame] | 21 | ) |
| 22 | |
Colin Cross | 5d57b2d | 2020-01-27 16:14:31 -0800 | [diff] [blame] | 23 | // 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 Cross | 8169500 | 2015-10-30 13:19:14 -0700 | [diff] [blame] | 26 | func CloneProperties(structValue reflect.Value) reflect.Value { |
Colin Cross | 5d57b2d | 2020-01-27 16:14:31 -0800 | [diff] [blame] | 27 | 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 Cross | 8169500 | 2015-10-30 13:19:14 -0700 | [diff] [blame] | 32 | return result |
| 33 | } |
| 34 | |
Colin Cross | 5d57b2d | 2020-01-27 16:14:31 -0800 | [diff] [blame] | 35 | // 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 Cross | 8169500 | 2015-10-30 13:19:14 -0700 | [diff] [blame] | 38 | func CopyProperties(dstValue, srcValue reflect.Value) { |
Colin Cross | 5d57b2d | 2020-01-27 16:14:31 -0800 | [diff] [blame] | 39 | 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 | |
| 48 | func copyProperties(dstValue, srcValue reflect.Value) { |
Colin Cross | 8169500 | 2015-10-30 13:19:14 -0700 | [diff] [blame] | 49 | 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 Cross | 01ee36e | 2016-05-17 13:57:12 -0700 | [diff] [blame] | 55 | for i, field := range typeFields(typ) { |
Colin Cross | 8169500 | 2015-10-30 13:19:14 -0700 | [diff] [blame] | 56 | if field.PkgPath != "" { |
Jaewoong Jung | 7d699c3 | 2019-03-07 14:01:22 -0800 | [diff] [blame] | 57 | panic(fmt.Errorf("can't copy a private field %q", field.Name)) |
Colin Cross | 8169500 | 2015-10-30 13:19:14 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | srcFieldValue := srcValue.Field(i) |
| 61 | dstFieldValue := dstValue.Field(i) |
Colin Cross | f72ef50 | 2015-10-30 11:42:57 -0700 | [diff] [blame] | 62 | dstFieldInterfaceValue := reflect.Value{} |
Colin Cross | c3d7312 | 2016-08-05 17:19:36 -0700 | [diff] [blame] | 63 | origDstFieldValue := dstFieldValue |
Colin Cross | 8169500 | 2015-10-30 13:19:14 -0700 | [diff] [blame] | 64 | |
| 65 | switch srcFieldValue.Kind() { |
| 66 | case reflect.Bool, reflect.String, reflect.Int, reflect.Uint: |
| 67 | dstFieldValue.Set(srcFieldValue) |
| 68 | case reflect.Struct: |
Colin Cross | 5d57b2d | 2020-01-27 16:14:31 -0800 | [diff] [blame] | 69 | copyProperties(dstFieldValue, srcFieldValue) |
Colin Cross | 8169500 | 2015-10-30 13:19:14 -0700 | [diff] [blame] | 70 | case reflect.Slice: |
| 71 | if !srcFieldValue.IsNil() { |
Colin Cross | 8169500 | 2015-10-30 13:19:14 -0700 | [diff] [blame] | 72 | 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 Cross | f72ef50 | 2015-10-30 11:42:57 -0700 | [diff] [blame] | 81 | case reflect.Interface: |
| 82 | if srcFieldValue.IsNil() { |
Colin Cross | 8169500 | 2015-10-30 13:19:14 -0700 | [diff] [blame] | 83 | dstFieldValue.Set(srcFieldValue) |
Colin Cross | f72ef50 | 2015-10-30 11:42:57 -0700 | [diff] [blame] | 84 | break |
| 85 | } |
| 86 | |
| 87 | srcFieldValue = srcFieldValue.Elem() |
| 88 | |
Colin Cross | 6898d26 | 2020-01-27 16:48:30 -0800 | [diff] [blame] | 89 | 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 Cross | f72ef50 | 2015-10-30 11:42:57 -0700 | [diff] [blame] | 92 | } |
| 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 Cross | f72ef50 | 2015-10-30 11:42:57 -0700 | [diff] [blame] | 106 | if srcFieldValue.IsNil() { |
Colin Cross | c3d7312 | 2016-08-05 17:19:36 -0700 | [diff] [blame] | 107 | origDstFieldValue.Set(srcFieldValue) |
Colin Cross | f72ef50 | 2015-10-30 11:42:57 -0700 | [diff] [blame] | 108 | break |
| 109 | } |
| 110 | |
Colin Cross | 5d57b2d | 2020-01-27 16:14:31 -0800 | [diff] [blame] | 111 | switch srcFieldValue.Elem().Kind() { |
Colin Cross | 8011768 | 2015-10-30 15:53:55 -0700 | [diff] [blame] | 112 | case reflect.Struct: |
| 113 | if !dstFieldValue.IsNil() { |
| 114 | // Re-use the existing allocation. |
Colin Cross | 5d57b2d | 2020-01-27 16:14:31 -0800 | [diff] [blame] | 115 | copyProperties(dstFieldValue.Elem(), srcFieldValue.Elem()) |
Colin Cross | 8011768 | 2015-10-30 15:53:55 -0700 | [diff] [blame] | 116 | break |
Colin Cross | f72ef50 | 2015-10-30 11:42:57 -0700 | [diff] [blame] | 117 | } else { |
Colin Cross | 8011768 | 2015-10-30 15:53:55 -0700 | [diff] [blame] | 118 | newValue := CloneProperties(srcFieldValue) |
| 119 | if dstFieldInterfaceValue.IsValid() { |
| 120 | dstFieldInterfaceValue.Set(newValue) |
| 121 | } else { |
Colin Cross | c3d7312 | 2016-08-05 17:19:36 -0700 | [diff] [blame] | 122 | origDstFieldValue.Set(newValue) |
Colin Cross | 8011768 | 2015-10-30 15:53:55 -0700 | [diff] [blame] | 123 | } |
Colin Cross | f72ef50 | 2015-10-30 11:42:57 -0700 | [diff] [blame] | 124 | } |
Nan Zhang | f586544 | 2017-11-01 14:03:28 -0700 | [diff] [blame] | 125 | case reflect.Bool, reflect.Int64, reflect.String: |
Colin Cross | 5d57b2d | 2020-01-27 16:14:31 -0800 | [diff] [blame] | 126 | newValue := reflect.New(srcFieldValue.Elem().Type()) |
| 127 | newValue.Elem().Set(srcFieldValue.Elem()) |
Colin Cross | c3d7312 | 2016-08-05 17:19:36 -0700 | [diff] [blame] | 128 | origDstFieldValue.Set(newValue) |
Colin Cross | 8011768 | 2015-10-30 15:53:55 -0700 | [diff] [blame] | 129 | default: |
Colin Cross | 5d57b2d | 2020-01-27 16:14:31 -0800 | [diff] [blame] | 130 | panic(fmt.Errorf("can't clone pointer field %q type %s", |
| 131 | field.Name, srcFieldValue.Type())) |
Colin Cross | 8169500 | 2015-10-30 13:19:14 -0700 | [diff] [blame] | 132 | } |
| 133 | default: |
Colin Cross | 5d57b2d | 2020-01-27 16:14:31 -0800 | [diff] [blame] | 134 | panic(fmt.Errorf("unexpected type for property struct field %q: %s", |
| 135 | field.Name, srcFieldValue.Type())) |
Colin Cross | 8169500 | 2015-10-30 13:19:14 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
Colin Cross | 5d57b2d | 2020-01-27 16:14:31 -0800 | [diff] [blame] | 140 | // 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 Cross | 8169500 | 2015-10-30 13:19:14 -0700 | [diff] [blame] | 142 | func ZeroProperties(structValue reflect.Value) { |
Colin Cross | 5d57b2d | 2020-01-27 16:14:31 -0800 | [diff] [blame] | 143 | if !isStructPtr(structValue.Type()) { |
| 144 | panic(fmt.Errorf("ZeroProperties expected *struct, got %s", structValue.Type())) |
| 145 | } |
| 146 | zeroProperties(structValue.Elem()) |
| 147 | } |
| 148 | |
| 149 | func zeroProperties(structValue reflect.Value) { |
Colin Cross | 8169500 | 2015-10-30 13:19:14 -0700 | [diff] [blame] | 150 | typ := structValue.Type() |
| 151 | |
Colin Cross | 01ee36e | 2016-05-17 13:57:12 -0700 | [diff] [blame] | 152 | for i, field := range typeFields(typ) { |
Colin Cross | 8169500 | 2015-10-30 13:19:14 -0700 | [diff] [blame] | 153 | 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 Cross | 83cedbe | 2015-11-20 17:01:25 -0800 | [diff] [blame] | 161 | case reflect.Bool, reflect.String, reflect.Slice, reflect.Int, reflect.Uint: |
Colin Cross | 8169500 | 2015-10-30 13:19:14 -0700 | [diff] [blame] | 162 | fieldValue.Set(reflect.Zero(fieldValue.Type())) |
Colin Cross | f72ef50 | 2015-10-30 11:42:57 -0700 | [diff] [blame] | 163 | case reflect.Interface: |
| 164 | if fieldValue.IsNil() { |
| 165 | break |
Colin Cross | 8169500 | 2015-10-30 13:19:14 -0700 | [diff] [blame] | 166 | } |
Colin Cross | f72ef50 | 2015-10-30 11:42:57 -0700 | [diff] [blame] | 167 | |
| 168 | // We leave the pointer intact and zero out the struct that's |
| 169 | // pointed to. |
| 170 | fieldValue = fieldValue.Elem() |
Colin Cross | 6898d26 | 2020-01-27 16:48:30 -0800 | [diff] [blame] | 171 | 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 Cross | f72ef50 | 2015-10-30 11:42:57 -0700 | [diff] [blame] | 174 | } |
| 175 | fallthrough |
| 176 | case reflect.Ptr: |
Colin Cross | 8011768 | 2015-10-30 15:53:55 -0700 | [diff] [blame] | 177 | switch fieldValue.Type().Elem().Kind() { |
| 178 | case reflect.Struct: |
| 179 | if fieldValue.IsNil() { |
| 180 | break |
| 181 | } |
Colin Cross | 5d57b2d | 2020-01-27 16:14:31 -0800 | [diff] [blame] | 182 | zeroProperties(fieldValue.Elem()) |
Nan Zhang | f586544 | 2017-11-01 14:03:28 -0700 | [diff] [blame] | 183 | case reflect.Bool, reflect.Int64, reflect.String: |
Colin Cross | 8011768 | 2015-10-30 15:53:55 -0700 | [diff] [blame] | 184 | 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 Cross | f72ef50 | 2015-10-30 11:42:57 -0700 | [diff] [blame] | 188 | } |
Colin Cross | 83cedbe | 2015-11-20 17:01:25 -0800 | [diff] [blame] | 189 | case reflect.Struct: |
Colin Cross | 5d57b2d | 2020-01-27 16:14:31 -0800 | [diff] [blame] | 190 | zeroProperties(fieldValue) |
Colin Cross | 8169500 | 2015-10-30 13:19:14 -0700 | [diff] [blame] | 191 | default: |
| 192 | panic(fmt.Errorf("unexpected kind for property struct field %q: %s", |
| 193 | field.Name, fieldValue.Kind())) |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
Colin Cross | 5d57b2d | 2020-01-27 16:14:31 -0800 | [diff] [blame] | 198 | // 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 Cross | 8169500 | 2015-10-30 13:19:14 -0700 | [diff] [blame] | 201 | func CloneEmptyProperties(structValue reflect.Value) reflect.Value { |
Colin Cross | 5d57b2d | 2020-01-27 16:14:31 -0800 | [diff] [blame] | 202 | 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 Cross | 8169500 | 2015-10-30 13:19:14 -0700 | [diff] [blame] | 207 | return result |
| 208 | } |
| 209 | |
| 210 | func cloneEmptyProperties(dstValue, srcValue reflect.Value) { |
| 211 | typ := srcValue.Type() |
Colin Cross | 01ee36e | 2016-05-17 13:57:12 -0700 | [diff] [blame] | 212 | for i, field := range typeFields(typ) { |
Colin Cross | 8169500 | 2015-10-30 13:19:14 -0700 | [diff] [blame] | 213 | 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 Cross | f72ef50 | 2015-10-30 11:42:57 -0700 | [diff] [blame] | 220 | dstFieldInterfaceValue := reflect.Value{} |
Colin Cross | 8169500 | 2015-10-30 13:19:14 -0700 | [diff] [blame] | 221 | |
| 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 Cross | f72ef50 | 2015-10-30 11:42:57 -0700 | [diff] [blame] | 227 | case reflect.Interface: |
| 228 | if srcFieldValue.IsNil() { |
| 229 | break |
| 230 | } |
| 231 | |
| 232 | srcFieldValue = srcFieldValue.Elem() |
Colin Cross | 6898d26 | 2020-01-27 16:48:30 -0800 | [diff] [blame] | 233 | 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 Cross | f72ef50 | 2015-10-30 11:42:57 -0700 | [diff] [blame] | 236 | } |
| 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 Cross | 8011768 | 2015-10-30 15:53:55 -0700 | [diff] [blame] | 244 | switch srcFieldValue.Type().Elem().Kind() { |
| 245 | case reflect.Struct: |
| 246 | if srcFieldValue.IsNil() { |
| 247 | break |
| 248 | } |
Colin Cross | 5d57b2d | 2020-01-27 16:14:31 -0800 | [diff] [blame] | 249 | newValue := CloneEmptyProperties(srcFieldValue) |
Colin Cross | 8011768 | 2015-10-30 15:53:55 -0700 | [diff] [blame] | 250 | if dstFieldInterfaceValue.IsValid() { |
| 251 | dstFieldInterfaceValue.Set(newValue) |
| 252 | } else { |
| 253 | dstFieldValue.Set(newValue) |
| 254 | } |
Nan Zhang | f586544 | 2017-11-01 14:03:28 -0700 | [diff] [blame] | 255 | case reflect.Bool, reflect.Int64, reflect.String: |
Colin Cross | 8011768 | 2015-10-30 15:53:55 -0700 | [diff] [blame] | 256 | // Nothing |
| 257 | default: |
| 258 | panic(fmt.Errorf("can't clone empty field %q: points to a %s", |
| 259 | field.Name, srcFieldValue.Elem().Kind())) |
Colin Cross | f72ef50 | 2015-10-30 11:42:57 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Colin Cross | 8169500 | 2015-10-30 13:19:14 -0700 | [diff] [blame] | 262 | default: |
| 263 | panic(fmt.Errorf("unexpected kind for property struct field %q: %s", |
| 264 | field.Name, srcFieldValue.Kind())) |
| 265 | } |
| 266 | } |
| 267 | } |
Colin Cross | 01ee36e | 2016-05-17 13:57:12 -0700 | [diff] [blame] | 268 | |
Colin Cross | 937efab | 2019-01-23 13:24:26 -0800 | [diff] [blame] | 269 | var typeFieldCache sync.Map |
Colin Cross | 01ee36e | 2016-05-17 13:57:12 -0700 | [diff] [blame] | 270 | |
| 271 | func typeFields(typ reflect.Type) []reflect.StructField { |
Colin Cross | 937efab | 2019-01-23 13:24:26 -0800 | [diff] [blame] | 272 | // 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 Cross | 01ee36e | 2016-05-17 13:57:12 -0700 | [diff] [blame] | 277 | // Fast path |
Colin Cross | 937efab | 2019-01-23 13:24:26 -0800 | [diff] [blame] | 278 | if typeFields, ok := typeFieldCache.Load(typ); ok { |
| 279 | return typeFields.([]reflect.StructField) |
Colin Cross | 01ee36e | 2016-05-17 13:57:12 -0700 | [diff] [blame] | 280 | } |
| 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 Cross | 937efab | 2019-01-23 13:24:26 -0800 | [diff] [blame] | 289 | typeFieldCache.Store(typ, typeFields) |
Colin Cross | 01ee36e | 2016-05-17 13:57:12 -0700 | [diff] [blame] | 290 | |
| 291 | return typeFields |
| 292 | } |