2015-10-30 21:19:14 +01:00
|
|
|
// Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package proptools
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2016-05-17 22:57:12 +02:00
|
|
|
"sync"
|
2015-10-30 21:19:14 +01:00
|
|
|
)
|
|
|
|
|
2020-01-28 01:14:31 +01:00
|
|
|
// CloneProperties takes a reflect.Value of a pointer to a struct and returns a reflect.Value
|
|
|
|
// of a pointer to a new struct that copies of the values for its fields. It recursively clones
|
|
|
|
// struct pointers and interfaces that contain struct pointers.
|
2015-10-30 21:19:14 +01:00
|
|
|
func CloneProperties(structValue reflect.Value) reflect.Value {
|
2020-01-28 01:14:31 +01:00
|
|
|
if !isStructPtr(structValue.Type()) {
|
|
|
|
panic(fmt.Errorf("CloneProperties expected *struct, got %s", structValue.Type()))
|
|
|
|
}
|
|
|
|
result := reflect.New(structValue.Type().Elem())
|
|
|
|
copyProperties(result.Elem(), structValue.Elem())
|
2015-10-30 21:19:14 +01:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-01-28 01:14:31 +01:00
|
|
|
// CopyProperties takes destination and source reflect.Values of a pointer to structs and returns
|
|
|
|
// copies each field from the source into the destination. It recursively copies struct pointers
|
|
|
|
// and interfaces that contain struct pointers.
|
2015-10-30 21:19:14 +01:00
|
|
|
func CopyProperties(dstValue, srcValue reflect.Value) {
|
2020-01-28 01:14:31 +01:00
|
|
|
if !isStructPtr(dstValue.Type()) {
|
|
|
|
panic(fmt.Errorf("CopyProperties expected dstValue *struct, got %s", dstValue.Type()))
|
|
|
|
}
|
|
|
|
if !isStructPtr(srcValue.Type()) {
|
|
|
|
panic(fmt.Errorf("CopyProperties expected srcValue *struct, got %s", srcValue.Type()))
|
|
|
|
}
|
|
|
|
copyProperties(dstValue.Elem(), srcValue.Elem())
|
|
|
|
}
|
|
|
|
|
|
|
|
func copyProperties(dstValue, srcValue reflect.Value) {
|
2015-10-30 21:19:14 +01:00
|
|
|
typ := dstValue.Type()
|
|
|
|
if srcValue.Type() != typ {
|
|
|
|
panic(fmt.Errorf("can't copy mismatching types (%s <- %s)",
|
|
|
|
dstValue.Kind(), srcValue.Kind()))
|
|
|
|
}
|
|
|
|
|
2016-05-17 22:57:12 +02:00
|
|
|
for i, field := range typeFields(typ) {
|
2015-10-30 21:19:14 +01:00
|
|
|
if field.PkgPath != "" {
|
2019-03-07 23:01:22 +01:00
|
|
|
panic(fmt.Errorf("can't copy a private field %q", field.Name))
|
2015-10-30 21:19:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
srcFieldValue := srcValue.Field(i)
|
|
|
|
dstFieldValue := dstValue.Field(i)
|
2015-10-30 19:42:57 +01:00
|
|
|
dstFieldInterfaceValue := reflect.Value{}
|
2016-08-06 02:19:36 +02:00
|
|
|
origDstFieldValue := dstFieldValue
|
2015-10-30 21:19:14 +01:00
|
|
|
|
|
|
|
switch srcFieldValue.Kind() {
|
|
|
|
case reflect.Bool, reflect.String, reflect.Int, reflect.Uint:
|
|
|
|
dstFieldValue.Set(srcFieldValue)
|
|
|
|
case reflect.Struct:
|
2020-01-28 01:14:31 +01:00
|
|
|
copyProperties(dstFieldValue, srcFieldValue)
|
2015-10-30 21:19:14 +01:00
|
|
|
case reflect.Slice:
|
|
|
|
if !srcFieldValue.IsNil() {
|
|
|
|
if srcFieldValue != dstFieldValue {
|
|
|
|
newSlice := reflect.MakeSlice(field.Type, srcFieldValue.Len(),
|
|
|
|
srcFieldValue.Len())
|
|
|
|
reflect.Copy(newSlice, srcFieldValue)
|
|
|
|
dstFieldValue.Set(newSlice)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dstFieldValue.Set(srcFieldValue)
|
|
|
|
}
|
2021-05-21 23:56:53 +02:00
|
|
|
case reflect.Map:
|
|
|
|
if !srcFieldValue.IsNil() {
|
|
|
|
newMap := reflect.MakeMap(field.Type)
|
|
|
|
|
|
|
|
iter := srcFieldValue.MapRange()
|
|
|
|
for iter.Next() {
|
|
|
|
newMap.SetMapIndex(iter.Key(), iter.Value())
|
|
|
|
}
|
|
|
|
dstFieldValue.Set(newMap)
|
|
|
|
} else {
|
|
|
|
dstFieldValue.Set(srcFieldValue)
|
|
|
|
}
|
2015-10-30 19:42:57 +01:00
|
|
|
case reflect.Interface:
|
|
|
|
if srcFieldValue.IsNil() {
|
|
|
|
dstFieldValue.Set(srcFieldValue)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
srcFieldValue = srcFieldValue.Elem()
|
|
|
|
|
2020-01-28 01:48:30 +01:00
|
|
|
if !isStructPtr(srcFieldValue.Type()) {
|
|
|
|
panic(fmt.Errorf("can't clone field %q: expected interface to contain *struct, found %s",
|
|
|
|
field.Name, srcFieldValue.Type()))
|
2015-10-30 19:42:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if dstFieldValue.IsNil() || dstFieldValue.Elem().Type() != srcFieldValue.Type() {
|
|
|
|
// We can't use the existing destination allocation, so
|
|
|
|
// clone a new one.
|
|
|
|
newValue := reflect.New(srcFieldValue.Type()).Elem()
|
|
|
|
dstFieldValue.Set(newValue)
|
|
|
|
dstFieldInterfaceValue = dstFieldValue
|
|
|
|
dstFieldValue = newValue
|
2015-10-30 21:19:14 +01:00
|
|
|
} else {
|
2015-10-30 19:42:57 +01:00
|
|
|
dstFieldValue = dstFieldValue.Elem()
|
|
|
|
}
|
|
|
|
fallthrough
|
|
|
|
case reflect.Ptr:
|
|
|
|
if srcFieldValue.IsNil() {
|
2016-08-06 02:19:36 +02:00
|
|
|
origDstFieldValue.Set(srcFieldValue)
|
2015-10-30 19:42:57 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2020-01-28 01:14:31 +01:00
|
|
|
switch srcFieldValue.Elem().Kind() {
|
2015-10-30 23:53:55 +01:00
|
|
|
case reflect.Struct:
|
|
|
|
if !dstFieldValue.IsNil() {
|
|
|
|
// Re-use the existing allocation.
|
2020-01-28 01:14:31 +01:00
|
|
|
copyProperties(dstFieldValue.Elem(), srcFieldValue.Elem())
|
2015-10-30 23:53:55 +01:00
|
|
|
break
|
2015-10-30 19:42:57 +01:00
|
|
|
} else {
|
2015-10-30 23:53:55 +01:00
|
|
|
newValue := CloneProperties(srcFieldValue)
|
|
|
|
if dstFieldInterfaceValue.IsValid() {
|
|
|
|
dstFieldInterfaceValue.Set(newValue)
|
|
|
|
} else {
|
2016-08-06 02:19:36 +02:00
|
|
|
origDstFieldValue.Set(newValue)
|
2015-10-30 23:53:55 +01:00
|
|
|
}
|
2015-10-30 19:42:57 +01:00
|
|
|
}
|
Support parsing int64 in Blueprint file.
Support int64 number instead of int to be more fixed to bit size so
that the underlying arch won't affect overflow cases. Besides,
refection: func (v Value) Int() int64 always cast to int64 no matter the
input is int, int16, int32. Currently we always treat "-" as negative
sign to bind to next value, and "+" as plus operator to add operands
together.
So we allow:
a = 5 + -4 + 5 or a = -4 + 5
But we don't allow:
a = +5 + 4 + -4 since we don't treat "+" as a positive sign, otherwise,
a = 5 + +5 would exist which looks pretty weird. In the future, we may
want fully support number calculator logic eg, "+"/"-" can be
positive/negative sign or operator, and "(" and ")" will be considered
to group expressions with a higher precedence.
int & uint properties within struct keeps unchanged, which is only
allowed when tagged with 'blueprint:mutated'. We only allow *int64
property instead of int64 property within struct since it does't make
sense to do prepending or appending to int64.
Change-Id: I565e046dbd268af3538aee148cd7300037e56523
2017-11-01 22:03:28 +01:00
|
|
|
case reflect.Bool, reflect.Int64, reflect.String:
|
2020-01-28 01:14:31 +01:00
|
|
|
newValue := reflect.New(srcFieldValue.Elem().Type())
|
|
|
|
newValue.Elem().Set(srcFieldValue.Elem())
|
2016-08-06 02:19:36 +02:00
|
|
|
origDstFieldValue.Set(newValue)
|
2015-10-30 23:53:55 +01:00
|
|
|
default:
|
2020-01-28 01:14:31 +01:00
|
|
|
panic(fmt.Errorf("can't clone pointer field %q type %s",
|
|
|
|
field.Name, srcFieldValue.Type()))
|
2015-10-30 21:19:14 +01:00
|
|
|
}
|
|
|
|
default:
|
2020-01-28 01:14:31 +01:00
|
|
|
panic(fmt.Errorf("unexpected type for property struct field %q: %s",
|
|
|
|
field.Name, srcFieldValue.Type()))
|
2015-10-30 21:19:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-28 01:14:31 +01:00
|
|
|
// ZeroProperties takes a reflect.Value of a pointer to a struct and replaces all of its fields
|
|
|
|
// with zero values, recursing into struct, pointer to struct and interface fields.
|
2015-10-30 21:19:14 +01:00
|
|
|
func ZeroProperties(structValue reflect.Value) {
|
2020-01-28 01:14:31 +01:00
|
|
|
if !isStructPtr(structValue.Type()) {
|
|
|
|
panic(fmt.Errorf("ZeroProperties expected *struct, got %s", structValue.Type()))
|
|
|
|
}
|
|
|
|
zeroProperties(structValue.Elem())
|
|
|
|
}
|
|
|
|
|
|
|
|
func zeroProperties(structValue reflect.Value) {
|
2015-10-30 21:19:14 +01:00
|
|
|
typ := structValue.Type()
|
|
|
|
|
2016-05-17 22:57:12 +02:00
|
|
|
for i, field := range typeFields(typ) {
|
2015-10-30 21:19:14 +01:00
|
|
|
if field.PkgPath != "" {
|
|
|
|
// The field is not exported so just skip it.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fieldValue := structValue.Field(i)
|
|
|
|
|
|
|
|
switch fieldValue.Kind() {
|
2021-05-21 23:56:53 +02:00
|
|
|
case reflect.Bool, reflect.String, reflect.Slice, reflect.Int, reflect.Uint, reflect.Map:
|
2015-10-30 21:19:14 +01:00
|
|
|
fieldValue.Set(reflect.Zero(fieldValue.Type()))
|
2015-10-30 19:42:57 +01:00
|
|
|
case reflect.Interface:
|
|
|
|
if fieldValue.IsNil() {
|
|
|
|
break
|
2015-10-30 21:19:14 +01:00
|
|
|
}
|
2015-10-30 19:42:57 +01:00
|
|
|
|
|
|
|
// We leave the pointer intact and zero out the struct that's
|
|
|
|
// pointed to.
|
|
|
|
fieldValue = fieldValue.Elem()
|
2020-01-28 01:48:30 +01:00
|
|
|
if !isStructPtr(fieldValue.Type()) {
|
|
|
|
panic(fmt.Errorf("can't zero field %q: expected interface to contain *struct, found %s",
|
|
|
|
field.Name, fieldValue.Type()))
|
2015-10-30 19:42:57 +01:00
|
|
|
}
|
|
|
|
fallthrough
|
|
|
|
case reflect.Ptr:
|
2015-10-30 23:53:55 +01:00
|
|
|
switch fieldValue.Type().Elem().Kind() {
|
|
|
|
case reflect.Struct:
|
|
|
|
if fieldValue.IsNil() {
|
|
|
|
break
|
|
|
|
}
|
2020-01-28 01:14:31 +01:00
|
|
|
zeroProperties(fieldValue.Elem())
|
Support parsing int64 in Blueprint file.
Support int64 number instead of int to be more fixed to bit size so
that the underlying arch won't affect overflow cases. Besides,
refection: func (v Value) Int() int64 always cast to int64 no matter the
input is int, int16, int32. Currently we always treat "-" as negative
sign to bind to next value, and "+" as plus operator to add operands
together.
So we allow:
a = 5 + -4 + 5 or a = -4 + 5
But we don't allow:
a = +5 + 4 + -4 since we don't treat "+" as a positive sign, otherwise,
a = 5 + +5 would exist which looks pretty weird. In the future, we may
want fully support number calculator logic eg, "+"/"-" can be
positive/negative sign or operator, and "(" and ")" will be considered
to group expressions with a higher precedence.
int & uint properties within struct keeps unchanged, which is only
allowed when tagged with 'blueprint:mutated'. We only allow *int64
property instead of int64 property within struct since it does't make
sense to do prepending or appending to int64.
Change-Id: I565e046dbd268af3538aee148cd7300037e56523
2017-11-01 22:03:28 +01:00
|
|
|
case reflect.Bool, reflect.Int64, reflect.String:
|
2015-10-30 23:53:55 +01:00
|
|
|
fieldValue.Set(reflect.Zero(fieldValue.Type()))
|
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("can't zero field %q: points to a %s",
|
|
|
|
field.Name, fieldValue.Elem().Kind()))
|
2015-10-30 19:42:57 +01:00
|
|
|
}
|
2015-11-21 02:01:25 +01:00
|
|
|
case reflect.Struct:
|
2020-01-28 01:14:31 +01:00
|
|
|
zeroProperties(fieldValue)
|
2015-10-30 21:19:14 +01:00
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("unexpected kind for property struct field %q: %s",
|
|
|
|
field.Name, fieldValue.Kind()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-28 01:14:31 +01:00
|
|
|
// CloneEmptyProperties takes a reflect.Value of a pointer to a struct and returns a reflect.Value
|
|
|
|
// of a pointer to a new struct that has the zero values for its fields. It recursively clones
|
|
|
|
// struct pointers and interfaces that contain struct pointers.
|
2015-10-30 21:19:14 +01:00
|
|
|
func CloneEmptyProperties(structValue reflect.Value) reflect.Value {
|
2020-01-28 01:14:31 +01:00
|
|
|
if !isStructPtr(structValue.Type()) {
|
|
|
|
panic(fmt.Errorf("CloneEmptyProperties expected *struct, got %s", structValue.Type()))
|
|
|
|
}
|
|
|
|
result := reflect.New(structValue.Type().Elem())
|
|
|
|
cloneEmptyProperties(result.Elem(), structValue.Elem())
|
2015-10-30 21:19:14 +01:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func cloneEmptyProperties(dstValue, srcValue reflect.Value) {
|
|
|
|
typ := srcValue.Type()
|
2016-05-17 22:57:12 +02:00
|
|
|
for i, field := range typeFields(typ) {
|
2015-10-30 21:19:14 +01:00
|
|
|
if field.PkgPath != "" {
|
|
|
|
// The field is not exported so just skip it.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
srcFieldValue := srcValue.Field(i)
|
|
|
|
dstFieldValue := dstValue.Field(i)
|
2015-10-30 19:42:57 +01:00
|
|
|
dstFieldInterfaceValue := reflect.Value{}
|
2015-10-30 21:19:14 +01:00
|
|
|
|
|
|
|
switch srcFieldValue.Kind() {
|
2021-05-21 23:56:53 +02:00
|
|
|
case reflect.Bool, reflect.String, reflect.Slice, reflect.Map, reflect.Int, reflect.Uint:
|
2015-10-30 21:19:14 +01:00
|
|
|
// Nothing
|
|
|
|
case reflect.Struct:
|
|
|
|
cloneEmptyProperties(dstFieldValue, srcFieldValue)
|
2015-10-30 19:42:57 +01:00
|
|
|
case reflect.Interface:
|
|
|
|
if srcFieldValue.IsNil() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
srcFieldValue = srcFieldValue.Elem()
|
2020-01-28 01:48:30 +01:00
|
|
|
if !isStructPtr(srcFieldValue.Type()) {
|
|
|
|
panic(fmt.Errorf("can't clone empty field %q: expected interface to contain *struct, found %s",
|
|
|
|
field.Name, srcFieldValue.Type()))
|
2015-10-30 19:42:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
newValue := reflect.New(srcFieldValue.Type()).Elem()
|
|
|
|
dstFieldValue.Set(newValue)
|
|
|
|
dstFieldInterfaceValue = dstFieldValue
|
|
|
|
dstFieldValue = newValue
|
|
|
|
fallthrough
|
|
|
|
case reflect.Ptr:
|
2015-10-30 23:53:55 +01:00
|
|
|
switch srcFieldValue.Type().Elem().Kind() {
|
|
|
|
case reflect.Struct:
|
|
|
|
if srcFieldValue.IsNil() {
|
|
|
|
break
|
|
|
|
}
|
2020-01-28 01:14:31 +01:00
|
|
|
newValue := CloneEmptyProperties(srcFieldValue)
|
2015-10-30 23:53:55 +01:00
|
|
|
if dstFieldInterfaceValue.IsValid() {
|
|
|
|
dstFieldInterfaceValue.Set(newValue)
|
|
|
|
} else {
|
|
|
|
dstFieldValue.Set(newValue)
|
|
|
|
}
|
Support parsing int64 in Blueprint file.
Support int64 number instead of int to be more fixed to bit size so
that the underlying arch won't affect overflow cases. Besides,
refection: func (v Value) Int() int64 always cast to int64 no matter the
input is int, int16, int32. Currently we always treat "-" as negative
sign to bind to next value, and "+" as plus operator to add operands
together.
So we allow:
a = 5 + -4 + 5 or a = -4 + 5
But we don't allow:
a = +5 + 4 + -4 since we don't treat "+" as a positive sign, otherwise,
a = 5 + +5 would exist which looks pretty weird. In the future, we may
want fully support number calculator logic eg, "+"/"-" can be
positive/negative sign or operator, and "(" and ")" will be considered
to group expressions with a higher precedence.
int & uint properties within struct keeps unchanged, which is only
allowed when tagged with 'blueprint:mutated'. We only allow *int64
property instead of int64 property within struct since it does't make
sense to do prepending or appending to int64.
Change-Id: I565e046dbd268af3538aee148cd7300037e56523
2017-11-01 22:03:28 +01:00
|
|
|
case reflect.Bool, reflect.Int64, reflect.String:
|
2015-10-30 23:53:55 +01:00
|
|
|
// Nothing
|
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("can't clone empty field %q: points to a %s",
|
|
|
|
field.Name, srcFieldValue.Elem().Kind()))
|
2015-10-30 19:42:57 +01:00
|
|
|
}
|
|
|
|
|
2015-10-30 21:19:14 +01:00
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("unexpected kind for property struct field %q: %s",
|
|
|
|
field.Name, srcFieldValue.Kind()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-17 22:57:12 +02:00
|
|
|
|
2019-01-23 22:24:26 +01:00
|
|
|
var typeFieldCache sync.Map
|
2016-05-17 22:57:12 +02:00
|
|
|
|
|
|
|
func typeFields(typ reflect.Type) []reflect.StructField {
|
2019-01-23 22:24:26 +01:00
|
|
|
// reflect.Type.Field allocates a []int{} to hold the index every time it is called, which ends up
|
|
|
|
// being a significant portion of the GC pressure. It can't reuse the same one in case a caller
|
|
|
|
// modifies the backing array through the slice. Since we don't modify it, cache the result
|
|
|
|
// locally to reduce allocations.
|
|
|
|
|
2016-05-17 22:57:12 +02:00
|
|
|
// Fast path
|
2019-01-23 22:24:26 +01:00
|
|
|
if typeFields, ok := typeFieldCache.Load(typ); ok {
|
|
|
|
return typeFields.([]reflect.StructField)
|
2016-05-17 22:57:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Slow path
|
|
|
|
typeFields := make([]reflect.StructField, typ.NumField())
|
|
|
|
|
|
|
|
for i := range typeFields {
|
|
|
|
typeFields[i] = typ.Field(i)
|
|
|
|
}
|
|
|
|
|
2019-01-23 22:24:26 +01:00
|
|
|
typeFieldCache.Store(typ, typeFields)
|
2016-05-17 22:57:12 +02:00
|
|
|
|
|
|
|
return typeFields
|
|
|
|
}
|