Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 1 | // Copyright 2015 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" |
| 20 | ) |
| 21 | |
| 22 | // AppendProperties appends the values of properties in the property struct src to the property |
| 23 | // struct dst. dst and src must be the same type, and both must be pointers to structs. |
| 24 | // |
| 25 | // The filter function can prevent individual properties from being appended by returning false, or |
| 26 | // abort AppendProperties with an error by returning an error. Passing nil for filter will append |
| 27 | // all properties. |
| 28 | // |
| 29 | // An error returned by AppendProperties that applies to a specific property will be an |
| 30 | // *ExtendPropertyError, and can have the property name and error extracted from it. |
| 31 | // |
Dan Willemsen | 4c00085 | 2016-01-05 14:16:04 -0800 | [diff] [blame] | 32 | // The append operation is defined as appending strings and slices of strings normally, OR-ing bool |
| 33 | // values, replacing non-nil pointers to booleans or strings, and recursing into |
Colin Cross | 8011768 | 2015-10-30 15:53:55 -0700 | [diff] [blame] | 34 | // embedded structs, pointers to structs, and interfaces containing |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 35 | // pointers to structs. Appending the zero value of a property will always be a no-op. |
| 36 | func AppendProperties(dst interface{}, src interface{}, filter ExtendPropertyFilterFunc) error { |
Jaewoong Jung | 4764fa7 | 2019-06-22 11:19:34 -0700 | [diff] [blame] | 37 | return extendProperties(dst, src, filter, OrderAppend) |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | // PrependProperties prepends the values of properties in the property struct src to the property |
| 41 | // struct dst. dst and src must be the same type, and both must be pointers to structs. |
| 42 | // |
| 43 | // The filter function can prevent individual properties from being prepended by returning false, or |
| 44 | // abort PrependProperties with an error by returning an error. Passing nil for filter will prepend |
| 45 | // all properties. |
| 46 | // |
| 47 | // An error returned by PrependProperties that applies to a specific property will be an |
| 48 | // *ExtendPropertyError, and can have the property name and error extracted from it. |
| 49 | // |
Dan Willemsen | 4c00085 | 2016-01-05 14:16:04 -0800 | [diff] [blame] | 50 | // The prepend operation is defined as prepending strings, and slices of strings normally, OR-ing |
| 51 | // bool values, replacing non-nil pointers to booleans or strings, and recursing into |
Colin Cross | 8011768 | 2015-10-30 15:53:55 -0700 | [diff] [blame] | 52 | // embedded structs, pointers to structs, and interfaces containing |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 53 | // pointers to structs. Prepending the zero value of a property will always be a no-op. |
| 54 | func PrependProperties(dst interface{}, src interface{}, filter ExtendPropertyFilterFunc) error { |
Jaewoong Jung | 4764fa7 | 2019-06-22 11:19:34 -0700 | [diff] [blame] | 55 | return extendProperties(dst, src, filter, OrderPrepend) |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | // AppendMatchingProperties appends the values of properties in the property struct src to the |
| 59 | // property structs in dst. dst and src do not have to be the same type, but every property in src |
| 60 | // must be found in at least one property in dst. dst must be a slice of pointers to structs, and |
| 61 | // src must be a pointer to a struct. |
| 62 | // |
| 63 | // The filter function can prevent individual properties from being appended by returning false, or |
| 64 | // abort AppendProperties with an error by returning an error. Passing nil for filter will append |
| 65 | // all properties. |
| 66 | // |
| 67 | // An error returned by AppendMatchingProperties that applies to a specific property will be an |
| 68 | // *ExtendPropertyError, and can have the property name and error extracted from it. |
| 69 | // |
Dan Willemsen | 4c00085 | 2016-01-05 14:16:04 -0800 | [diff] [blame] | 70 | // The append operation is defined as appending strings, and slices of strings normally, OR-ing bool |
Jaewoong Jung | c067251 | 2019-07-08 13:40:28 -0700 | [diff] [blame] | 71 | // values, replacing pointers to booleans or strings whether they are nil or not, and recursing into |
Colin Cross | 8011768 | 2015-10-30 15:53:55 -0700 | [diff] [blame] | 72 | // embedded structs, pointers to structs, and interfaces containing |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 73 | // pointers to structs. Appending the zero value of a property will always be a no-op. |
| 74 | func AppendMatchingProperties(dst []interface{}, src interface{}, |
| 75 | filter ExtendPropertyFilterFunc) error { |
Jaewoong Jung | 4764fa7 | 2019-06-22 11:19:34 -0700 | [diff] [blame] | 76 | return extendMatchingProperties(dst, src, filter, OrderAppend) |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | // PrependMatchingProperties prepends the values of properties in the property struct src to the |
| 80 | // property structs in dst. dst and src do not have to be the same type, but every property in src |
| 81 | // must be found in at least one property in dst. dst must be a slice of pointers to structs, and |
| 82 | // src must be a pointer to a struct. |
| 83 | // |
| 84 | // The filter function can prevent individual properties from being prepended by returning false, or |
| 85 | // abort PrependProperties with an error by returning an error. Passing nil for filter will prepend |
| 86 | // all properties. |
| 87 | // |
| 88 | // An error returned by PrependProperties that applies to a specific property will be an |
| 89 | // *ExtendPropertyError, and can have the property name and error extracted from it. |
| 90 | // |
Dan Willemsen | 4c00085 | 2016-01-05 14:16:04 -0800 | [diff] [blame] | 91 | // The prepend operation is defined as prepending strings, and slices of strings normally, OR-ing |
Jaewoong Jung | c067251 | 2019-07-08 13:40:28 -0700 | [diff] [blame] | 92 | // bool values, replacing nil pointers to booleans or strings, and recursing into |
Colin Cross | 8011768 | 2015-10-30 15:53:55 -0700 | [diff] [blame] | 93 | // embedded structs, pointers to structs, and interfaces containing |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 94 | // pointers to structs. Prepending the zero value of a property will always be a no-op. |
| 95 | func PrependMatchingProperties(dst []interface{}, src interface{}, |
| 96 | filter ExtendPropertyFilterFunc) error { |
Jaewoong Jung | 4764fa7 | 2019-06-22 11:19:34 -0700 | [diff] [blame] | 97 | return extendMatchingProperties(dst, src, filter, OrderPrepend) |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Colin Cross | 75c4701 | 2016-05-05 15:58:02 -0700 | [diff] [blame] | 100 | // ExtendProperties appends or prepends the values of properties in the property struct src to the |
| 101 | // property struct dst. dst and src must be the same type, and both must be pointers to structs. |
| 102 | // |
| 103 | // The filter function can prevent individual properties from being appended or prepended by |
| 104 | // returning false, or abort ExtendProperties with an error by returning an error. Passing nil for |
| 105 | // filter will append or prepend all properties. |
| 106 | // |
| 107 | // The order function is called on each non-filtered property to determine if it should be appended |
| 108 | // or prepended. |
| 109 | // |
| 110 | // An error returned by ExtendProperties that applies to a specific property will be an |
| 111 | // *ExtendPropertyError, and can have the property name and error extracted from it. |
| 112 | // |
| 113 | // The append operation is defined as appending strings and slices of strings normally, OR-ing bool |
| 114 | // values, replacing non-nil pointers to booleans or strings, and recursing into |
| 115 | // embedded structs, pointers to structs, and interfaces containing |
| 116 | // pointers to structs. Appending or prepending the zero value of a property will always be a |
| 117 | // no-op. |
| 118 | func ExtendProperties(dst interface{}, src interface{}, filter ExtendPropertyFilterFunc, |
| 119 | order ExtendPropertyOrderFunc) error { |
| 120 | return extendProperties(dst, src, filter, order) |
| 121 | } |
| 122 | |
| 123 | // ExtendMatchingProperties appends or prepends the values of properties in the property struct src |
| 124 | // to the property structs in dst. dst and src do not have to be the same type, but every property |
| 125 | // in src must be found in at least one property in dst. dst must be a slice of pointers to |
| 126 | // structs, and src must be a pointer to a struct. |
| 127 | // |
| 128 | // The filter function can prevent individual properties from being appended or prepended by |
| 129 | // returning false, or abort ExtendMatchingProperties with an error by returning an error. Passing |
| 130 | // nil for filter will append or prepend all properties. |
| 131 | // |
| 132 | // The order function is called on each non-filtered property to determine if it should be appended |
| 133 | // or prepended. |
| 134 | // |
| 135 | // An error returned by ExtendMatchingProperties that applies to a specific property will be an |
| 136 | // *ExtendPropertyError, and can have the property name and error extracted from it. |
| 137 | // |
| 138 | // The append operation is defined as appending strings, and slices of strings normally, OR-ing bool |
| 139 | // values, replacing non-nil pointers to booleans or strings, and recursing into |
| 140 | // embedded structs, pointers to structs, and interfaces containing |
| 141 | // pointers to structs. Appending or prepending the zero value of a property will always be a |
| 142 | // no-op. |
| 143 | func ExtendMatchingProperties(dst []interface{}, src interface{}, |
| 144 | filter ExtendPropertyFilterFunc, order ExtendPropertyOrderFunc) error { |
| 145 | return extendMatchingProperties(dst, src, filter, order) |
| 146 | } |
| 147 | |
| 148 | type Order int |
| 149 | |
| 150 | const ( |
| 151 | Append Order = iota |
| 152 | Prepend |
Jiyong Park | 10f27f8 | 2019-11-15 18:31:56 +0900 | [diff] [blame] | 153 | Replace |
Colin Cross | 75c4701 | 2016-05-05 15:58:02 -0700 | [diff] [blame] | 154 | ) |
| 155 | |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 156 | type ExtendPropertyFilterFunc func(property string, |
| 157 | dstField, srcField reflect.StructField, |
| 158 | dstValue, srcValue interface{}) (bool, error) |
| 159 | |
Colin Cross | 75c4701 | 2016-05-05 15:58:02 -0700 | [diff] [blame] | 160 | type ExtendPropertyOrderFunc func(property string, |
| 161 | dstField, srcField reflect.StructField, |
| 162 | dstValue, srcValue interface{}) (Order, error) |
| 163 | |
Jaewoong Jung | 4764fa7 | 2019-06-22 11:19:34 -0700 | [diff] [blame] | 164 | func OrderAppend(property string, |
Colin Cross | 75c4701 | 2016-05-05 15:58:02 -0700 | [diff] [blame] | 165 | dstField, srcField reflect.StructField, |
| 166 | dstValue, srcValue interface{}) (Order, error) { |
| 167 | return Append, nil |
| 168 | } |
| 169 | |
Jaewoong Jung | 4764fa7 | 2019-06-22 11:19:34 -0700 | [diff] [blame] | 170 | func OrderPrepend(property string, |
Colin Cross | 75c4701 | 2016-05-05 15:58:02 -0700 | [diff] [blame] | 171 | dstField, srcField reflect.StructField, |
| 172 | dstValue, srcValue interface{}) (Order, error) { |
| 173 | return Prepend, nil |
| 174 | } |
| 175 | |
Jiyong Park | 10f27f8 | 2019-11-15 18:31:56 +0900 | [diff] [blame] | 176 | func OrderReplace(property string, |
| 177 | dstField, srcField reflect.StructField, |
| 178 | dstValue, srcValue interface{}) (Order, error) { |
| 179 | return Replace, nil |
| 180 | } |
| 181 | |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 182 | type ExtendPropertyError struct { |
| 183 | Err error |
| 184 | Property string |
| 185 | } |
| 186 | |
| 187 | func (e *ExtendPropertyError) Error() string { |
| 188 | return fmt.Sprintf("can't extend property %q: %s", e.Property, e.Err) |
| 189 | } |
| 190 | |
| 191 | func extendPropertyErrorf(property string, format string, a ...interface{}) *ExtendPropertyError { |
| 192 | return &ExtendPropertyError{ |
| 193 | Err: fmt.Errorf(format, a...), |
| 194 | Property: property, |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | func extendProperties(dst interface{}, src interface{}, filter ExtendPropertyFilterFunc, |
Colin Cross | 75c4701 | 2016-05-05 15:58:02 -0700 | [diff] [blame] | 199 | order ExtendPropertyOrderFunc) error { |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 200 | |
Colin Cross | c3d7312 | 2016-08-05 17:19:36 -0700 | [diff] [blame] | 201 | srcValue, err := getStruct(src) |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 202 | if err != nil { |
Colin Cross | c3d7312 | 2016-08-05 17:19:36 -0700 | [diff] [blame] | 203 | if _, ok := err.(getStructEmptyError); ok { |
| 204 | return nil |
| 205 | } |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 206 | return err |
| 207 | } |
Colin Cross | c3d7312 | 2016-08-05 17:19:36 -0700 | [diff] [blame] | 208 | |
| 209 | dstValue, err := getOrCreateStruct(dst) |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 210 | if err != nil { |
| 211 | return err |
| 212 | } |
| 213 | |
| 214 | if dstValue.Type() != srcValue.Type() { |
| 215 | return fmt.Errorf("expected matching types for dst and src, got %T and %T", dst, src) |
| 216 | } |
| 217 | |
| 218 | dstValues := []reflect.Value{dstValue} |
| 219 | |
Colin Cross | 75c4701 | 2016-05-05 15:58:02 -0700 | [diff] [blame] | 220 | return extendPropertiesRecursive(dstValues, srcValue, "", filter, true, order) |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | func extendMatchingProperties(dst []interface{}, src interface{}, filter ExtendPropertyFilterFunc, |
Colin Cross | 75c4701 | 2016-05-05 15:58:02 -0700 | [diff] [blame] | 224 | order ExtendPropertyOrderFunc) error { |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 225 | |
Colin Cross | c3d7312 | 2016-08-05 17:19:36 -0700 | [diff] [blame] | 226 | srcValue, err := getStruct(src) |
| 227 | if err != nil { |
| 228 | if _, ok := err.(getStructEmptyError); ok { |
| 229 | return nil |
| 230 | } |
| 231 | return err |
| 232 | } |
| 233 | |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 234 | dstValues := make([]reflect.Value, len(dst)) |
| 235 | for i := range dst { |
| 236 | var err error |
Colin Cross | c3d7312 | 2016-08-05 17:19:36 -0700 | [diff] [blame] | 237 | dstValues[i], err = getOrCreateStruct(dst[i]) |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 238 | if err != nil { |
| 239 | return err |
| 240 | } |
| 241 | } |
| 242 | |
Colin Cross | 75c4701 | 2016-05-05 15:58:02 -0700 | [diff] [blame] | 243 | return extendPropertiesRecursive(dstValues, srcValue, "", filter, false, order) |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | func extendPropertiesRecursive(dstValues []reflect.Value, srcValue reflect.Value, |
Colin Cross | 75c4701 | 2016-05-05 15:58:02 -0700 | [diff] [blame] | 247 | prefix string, filter ExtendPropertyFilterFunc, sameTypes bool, |
Colin Cross | 05b3607 | 2017-07-28 17:51:37 -0700 | [diff] [blame] | 248 | orderFunc ExtendPropertyOrderFunc) error { |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 249 | |
| 250 | srcType := srcValue.Type() |
Colin Cross | 01ee36e | 2016-05-17 13:57:12 -0700 | [diff] [blame] | 251 | for i, srcField := range typeFields(srcType) { |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 252 | if srcField.PkgPath != "" { |
| 253 | // The field is not exported so just skip it. |
| 254 | continue |
| 255 | } |
| 256 | if HasTag(srcField, "blueprint", "mutated") { |
| 257 | continue |
| 258 | } |
| 259 | |
| 260 | propertyName := prefix + PropertyNameForField(srcField.Name) |
| 261 | srcFieldValue := srcValue.Field(i) |
| 262 | |
Colin Cross | bf2adbf | 2016-08-19 18:16:33 -0700 | [diff] [blame] | 263 | // Step into source interfaces |
| 264 | if srcFieldValue.Kind() == reflect.Interface { |
| 265 | if srcFieldValue.IsNil() { |
| 266 | continue |
| 267 | } |
| 268 | |
| 269 | srcFieldValue = srcFieldValue.Elem() |
| 270 | |
| 271 | if srcFieldValue.Kind() != reflect.Ptr { |
| 272 | return extendPropertyErrorf(propertyName, "interface not a pointer") |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | // Step into source pointers to structs |
Colin Cross | 6898d26 | 2020-01-27 16:48:30 -0800 | [diff] [blame] | 277 | if isStructPtr(srcFieldValue.Type()) { |
Colin Cross | bf2adbf | 2016-08-19 18:16:33 -0700 | [diff] [blame] | 278 | if srcFieldValue.IsNil() { |
| 279 | continue |
| 280 | } |
| 281 | |
| 282 | srcFieldValue = srcFieldValue.Elem() |
| 283 | } |
| 284 | |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 285 | found := false |
Colin Cross | bf2adbf | 2016-08-19 18:16:33 -0700 | [diff] [blame] | 286 | var recurse []reflect.Value |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 287 | for _, dstValue := range dstValues { |
| 288 | dstType := dstValue.Type() |
| 289 | var dstField reflect.StructField |
| 290 | |
Colin Cross | 01ee36e | 2016-05-17 13:57:12 -0700 | [diff] [blame] | 291 | dstFields := typeFields(dstType) |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 292 | if dstType == srcType { |
Colin Cross | 01ee36e | 2016-05-17 13:57:12 -0700 | [diff] [blame] | 293 | dstField = dstFields[i] |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 294 | } else { |
| 295 | var ok bool |
Colin Cross | 01ee36e | 2016-05-17 13:57:12 -0700 | [diff] [blame] | 296 | for _, field := range dstFields { |
| 297 | if field.Name == srcField.Name { |
| 298 | dstField = field |
| 299 | ok = true |
| 300 | } |
| 301 | } |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 302 | if !ok { |
| 303 | continue |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | found = true |
| 308 | |
| 309 | dstFieldValue := dstValue.FieldByIndex(dstField.Index) |
Colin Cross | c3d7312 | 2016-08-05 17:19:36 -0700 | [diff] [blame] | 310 | origDstFieldValue := dstFieldValue |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 311 | |
Colin Cross | bf2adbf | 2016-08-19 18:16:33 -0700 | [diff] [blame] | 312 | // Step into destination interfaces |
| 313 | if dstFieldValue.Kind() == reflect.Interface { |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 314 | if dstFieldValue.IsNil() { |
Colin Cross | bf2adbf | 2016-08-19 18:16:33 -0700 | [diff] [blame] | 315 | return extendPropertyErrorf(propertyName, "nilitude mismatch") |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | dstFieldValue = dstFieldValue.Elem() |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 319 | |
Colin Cross | bf2adbf | 2016-08-19 18:16:33 -0700 | [diff] [blame] | 320 | if dstFieldValue.Kind() != reflect.Ptr { |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 321 | return extendPropertyErrorf(propertyName, "interface not a pointer") |
| 322 | } |
Colin Cross | bf2adbf | 2016-08-19 18:16:33 -0700 | [diff] [blame] | 323 | } |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 324 | |
Colin Cross | bf2adbf | 2016-08-19 18:16:33 -0700 | [diff] [blame] | 325 | // Step into destination pointers to structs |
Colin Cross | 6898d26 | 2020-01-27 16:48:30 -0800 | [diff] [blame] | 326 | if isStructPtr(dstFieldValue.Type()) { |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 327 | if dstFieldValue.IsNil() { |
Colin Cross | bf2adbf | 2016-08-19 18:16:33 -0700 | [diff] [blame] | 328 | dstFieldValue = reflect.New(dstFieldValue.Type().Elem()) |
Colin Cross | c3d7312 | 2016-08-05 17:19:36 -0700 | [diff] [blame] | 329 | origDstFieldValue.Set(dstFieldValue) |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | dstFieldValue = dstFieldValue.Elem() |
Colin Cross | bf2adbf | 2016-08-19 18:16:33 -0700 | [diff] [blame] | 333 | } |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 334 | |
Colin Cross | bf2adbf | 2016-08-19 18:16:33 -0700 | [diff] [blame] | 335 | switch srcFieldValue.Kind() { |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 336 | case reflect.Struct: |
| 337 | if sameTypes && dstFieldValue.Type() != srcFieldValue.Type() { |
| 338 | return extendPropertyErrorf(propertyName, "mismatched types %s and %s", |
| 339 | dstFieldValue.Type(), srcFieldValue.Type()) |
| 340 | } |
| 341 | |
| 342 | // Recursively extend the struct's fields. |
Colin Cross | bf2adbf | 2016-08-19 18:16:33 -0700 | [diff] [blame] | 343 | recurse = append(recurse, dstFieldValue) |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 344 | continue |
| 345 | case reflect.Bool, reflect.String, reflect.Slice: |
| 346 | if srcFieldValue.Type() != dstFieldValue.Type() { |
| 347 | return extendPropertyErrorf(propertyName, "mismatched types %s and %s", |
| 348 | dstFieldValue.Type(), srcFieldValue.Type()) |
| 349 | } |
Colin Cross | bf2adbf | 2016-08-19 18:16:33 -0700 | [diff] [blame] | 350 | case reflect.Ptr: |
| 351 | if srcFieldValue.Type() != dstFieldValue.Type() { |
| 352 | return extendPropertyErrorf(propertyName, "mismatched types %s and %s", |
| 353 | dstFieldValue.Type(), srcFieldValue.Type()) |
| 354 | } |
| 355 | switch ptrKind := srcFieldValue.Type().Elem().Kind(); ptrKind { |
Nan Zhang | f586544 | 2017-11-01 14:03:28 -0700 | [diff] [blame] | 356 | case reflect.Bool, reflect.Int64, reflect.String, reflect.Struct: |
Colin Cross | bf2adbf | 2016-08-19 18:16:33 -0700 | [diff] [blame] | 357 | // Nothing |
| 358 | default: |
| 359 | return extendPropertyErrorf(propertyName, "pointer is a %s", ptrKind) |
| 360 | } |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 361 | default: |
| 362 | return extendPropertyErrorf(propertyName, "unsupported kind %s", |
| 363 | srcFieldValue.Kind()) |
| 364 | } |
| 365 | |
Colin Cross | 01ee36e | 2016-05-17 13:57:12 -0700 | [diff] [blame] | 366 | dstFieldInterface := dstFieldValue.Interface() |
| 367 | srcFieldInterface := srcFieldValue.Interface() |
| 368 | |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 369 | if filter != nil { |
| 370 | b, err := filter(propertyName, dstField, srcField, |
Colin Cross | 01ee36e | 2016-05-17 13:57:12 -0700 | [diff] [blame] | 371 | dstFieldInterface, srcFieldInterface) |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 372 | if err != nil { |
| 373 | return &ExtendPropertyError{ |
| 374 | Property: propertyName, |
| 375 | Err: err, |
| 376 | } |
| 377 | } |
| 378 | if !b { |
| 379 | continue |
| 380 | } |
| 381 | } |
| 382 | |
Colin Cross | 05b3607 | 2017-07-28 17:51:37 -0700 | [diff] [blame] | 383 | order := Append |
| 384 | if orderFunc != nil { |
| 385 | var err error |
| 386 | order, err = orderFunc(propertyName, dstField, srcField, |
Colin Cross | 01ee36e | 2016-05-17 13:57:12 -0700 | [diff] [blame] | 387 | dstFieldInterface, srcFieldInterface) |
Colin Cross | 75c4701 | 2016-05-05 15:58:02 -0700 | [diff] [blame] | 388 | if err != nil { |
| 389 | return &ExtendPropertyError{ |
| 390 | Property: propertyName, |
| 391 | Err: err, |
| 392 | } |
| 393 | } |
Colin Cross | 75c4701 | 2016-05-05 15:58:02 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Colin Cross | 05b3607 | 2017-07-28 17:51:37 -0700 | [diff] [blame] | 396 | ExtendBasicType(dstFieldValue, srcFieldValue, order) |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 397 | } |
Colin Cross | 05b3607 | 2017-07-28 17:51:37 -0700 | [diff] [blame] | 398 | |
Colin Cross | bf2adbf | 2016-08-19 18:16:33 -0700 | [diff] [blame] | 399 | if len(recurse) > 0 { |
| 400 | err := extendPropertiesRecursive(recurse, srcFieldValue, |
Colin Cross | 05b3607 | 2017-07-28 17:51:37 -0700 | [diff] [blame] | 401 | propertyName+".", filter, sameTypes, orderFunc) |
Colin Cross | bf2adbf | 2016-08-19 18:16:33 -0700 | [diff] [blame] | 402 | if err != nil { |
| 403 | return err |
| 404 | } |
| 405 | } else if !found { |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 406 | return extendPropertyErrorf(propertyName, "failed to find property to extend") |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | return nil |
| 411 | } |
| 412 | |
Colin Cross | 05b3607 | 2017-07-28 17:51:37 -0700 | [diff] [blame] | 413 | func ExtendBasicType(dstFieldValue, srcFieldValue reflect.Value, order Order) { |
| 414 | prepend := order == Prepend |
| 415 | |
| 416 | switch srcFieldValue.Kind() { |
| 417 | case reflect.Bool: |
| 418 | // Boolean OR |
| 419 | dstFieldValue.Set(reflect.ValueOf(srcFieldValue.Bool() || dstFieldValue.Bool())) |
| 420 | case reflect.String: |
| 421 | if prepend { |
| 422 | dstFieldValue.SetString(srcFieldValue.String() + |
| 423 | dstFieldValue.String()) |
| 424 | } else { |
| 425 | dstFieldValue.SetString(dstFieldValue.String() + |
| 426 | srcFieldValue.String()) |
| 427 | } |
| 428 | case reflect.Slice: |
| 429 | if srcFieldValue.IsNil() { |
| 430 | break |
| 431 | } |
| 432 | |
| 433 | newSlice := reflect.MakeSlice(srcFieldValue.Type(), 0, |
| 434 | dstFieldValue.Len()+srcFieldValue.Len()) |
| 435 | if prepend { |
| 436 | newSlice = reflect.AppendSlice(newSlice, srcFieldValue) |
| 437 | newSlice = reflect.AppendSlice(newSlice, dstFieldValue) |
Jiyong Park | 10f27f8 | 2019-11-15 18:31:56 +0900 | [diff] [blame] | 438 | } else if order == Append { |
Colin Cross | 05b3607 | 2017-07-28 17:51:37 -0700 | [diff] [blame] | 439 | newSlice = reflect.AppendSlice(newSlice, dstFieldValue) |
| 440 | newSlice = reflect.AppendSlice(newSlice, srcFieldValue) |
Jiyong Park | 10f27f8 | 2019-11-15 18:31:56 +0900 | [diff] [blame] | 441 | } else { |
| 442 | // replace |
| 443 | newSlice = reflect.AppendSlice(newSlice, srcFieldValue) |
Colin Cross | 05b3607 | 2017-07-28 17:51:37 -0700 | [diff] [blame] | 444 | } |
| 445 | dstFieldValue.Set(newSlice) |
| 446 | case reflect.Ptr: |
| 447 | if srcFieldValue.IsNil() { |
| 448 | break |
| 449 | } |
| 450 | |
| 451 | switch ptrKind := srcFieldValue.Type().Elem().Kind(); ptrKind { |
| 452 | case reflect.Bool: |
| 453 | if prepend { |
| 454 | if dstFieldValue.IsNil() { |
| 455 | dstFieldValue.Set(reflect.ValueOf(BoolPtr(srcFieldValue.Elem().Bool()))) |
| 456 | } |
| 457 | } else { |
| 458 | // For append, replace the original value. |
| 459 | dstFieldValue.Set(reflect.ValueOf(BoolPtr(srcFieldValue.Elem().Bool()))) |
| 460 | } |
Nan Zhang | f586544 | 2017-11-01 14:03:28 -0700 | [diff] [blame] | 461 | case reflect.Int64: |
| 462 | if prepend { |
| 463 | if dstFieldValue.IsNil() { |
| 464 | // Int() returns Int64 |
| 465 | dstFieldValue.Set(reflect.ValueOf(Int64Ptr(srcFieldValue.Elem().Int()))) |
| 466 | } |
| 467 | } else { |
| 468 | // For append, replace the original value. |
| 469 | // Int() returns Int64 |
| 470 | dstFieldValue.Set(reflect.ValueOf(Int64Ptr(srcFieldValue.Elem().Int()))) |
| 471 | } |
Colin Cross | 05b3607 | 2017-07-28 17:51:37 -0700 | [diff] [blame] | 472 | case reflect.String: |
| 473 | if prepend { |
| 474 | if dstFieldValue.IsNil() { |
| 475 | dstFieldValue.Set(reflect.ValueOf(StringPtr(srcFieldValue.Elem().String()))) |
| 476 | } |
| 477 | } else { |
| 478 | // For append, replace the original value. |
| 479 | dstFieldValue.Set(reflect.ValueOf(StringPtr(srcFieldValue.Elem().String()))) |
| 480 | } |
| 481 | default: |
| 482 | panic(fmt.Errorf("unexpected pointer kind %s", ptrKind)) |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | |
Colin Cross | c3d7312 | 2016-08-05 17:19:36 -0700 | [diff] [blame] | 487 | type getStructEmptyError struct{} |
| 488 | |
| 489 | func (getStructEmptyError) Error() string { return "interface containing nil pointer" } |
| 490 | |
| 491 | func getOrCreateStruct(in interface{}) (reflect.Value, error) { |
| 492 | value, err := getStruct(in) |
| 493 | if _, ok := err.(getStructEmptyError); ok { |
| 494 | value := reflect.ValueOf(in) |
| 495 | newValue := reflect.New(value.Type().Elem()) |
| 496 | value.Set(newValue) |
| 497 | } |
| 498 | |
| 499 | return value, err |
| 500 | } |
| 501 | |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 502 | func getStruct(in interface{}) (reflect.Value, error) { |
| 503 | value := reflect.ValueOf(in) |
Colin Cross | 6898d26 | 2020-01-27 16:48:30 -0800 | [diff] [blame] | 504 | if !isStructPtr(value.Type()) { |
| 505 | return reflect.Value{}, fmt.Errorf("expected pointer to struct, got %s", value.Type()) |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 506 | } |
Colin Cross | c3d7312 | 2016-08-05 17:19:36 -0700 | [diff] [blame] | 507 | if value.IsNil() { |
| 508 | return reflect.Value{}, getStructEmptyError{} |
| 509 | } |
| 510 | value = value.Elem() |
Colin Cross | 0bc7e07 | 2015-10-27 18:15:15 -0700 | [diff] [blame] | 511 | return value, nil |
| 512 | } |