2015-10-30 19:42:57 +01:00
|
|
|
// Copyright 2015 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"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var clonePropertiesTestCases = []struct {
|
|
|
|
in interface{}
|
|
|
|
out interface{}
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
// Valid inputs
|
|
|
|
|
|
|
|
{
|
|
|
|
// Clone bool
|
|
|
|
in: &struct{ B1, B2 bool }{
|
|
|
|
B1: true,
|
|
|
|
B2: false,
|
|
|
|
},
|
|
|
|
out: &struct{ B1, B2 bool }{
|
|
|
|
B1: true,
|
|
|
|
B2: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Clone strings
|
|
|
|
in: &struct{ S string }{
|
|
|
|
S: "string1",
|
|
|
|
},
|
|
|
|
out: &struct{ S string }{
|
|
|
|
S: "string1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Clone slice
|
|
|
|
in: &struct{ S []string }{
|
|
|
|
S: []string{"string1"},
|
|
|
|
},
|
|
|
|
out: &struct{ S []string }{
|
|
|
|
S: []string{"string1"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Clone empty slice
|
|
|
|
in: &struct{ S []string }{
|
|
|
|
S: []string{},
|
|
|
|
},
|
|
|
|
out: &struct{ S []string }{
|
|
|
|
S: []string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Clone nil slice
|
|
|
|
in: &struct{ S []string }{},
|
|
|
|
out: &struct{ S []string }{},
|
|
|
|
},
|
2018-10-03 06:57:47 +02:00
|
|
|
{
|
|
|
|
// Clone slice of structs
|
|
|
|
in: &struct{ S []struct{ T string } }{
|
|
|
|
S: []struct{ T string }{
|
|
|
|
{"string1"}, {"string2"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
out: &struct{ S []struct{ T string } }{
|
|
|
|
S: []struct{ T string }{
|
|
|
|
{"string1"}, {"string2"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-05-21 23:56:53 +02:00
|
|
|
{
|
|
|
|
// Clone map
|
|
|
|
in: &struct{ S map[string]string }{
|
|
|
|
S: map[string]string{"key": "string1"},
|
|
|
|
},
|
|
|
|
out: &struct{ S map[string]string }{
|
|
|
|
S: map[string]string{"key": "string1"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Clone empty map
|
|
|
|
in: &struct{ S map[string]string }{
|
|
|
|
S: map[string]string{},
|
|
|
|
},
|
|
|
|
out: &struct{ S map[string]string }{
|
|
|
|
S: map[string]string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Clone nil map
|
|
|
|
in: &struct{ S map[string]string }{},
|
|
|
|
out: &struct{ S map[string]string }{},
|
|
|
|
},
|
2015-10-30 19:42:57 +01:00
|
|
|
{
|
2015-10-30 23:53:55 +01:00
|
|
|
// Clone pointer to bool
|
|
|
|
in: &struct{ B1, B2 *bool }{
|
|
|
|
B1: BoolPtr(true),
|
|
|
|
B2: BoolPtr(false),
|
|
|
|
},
|
|
|
|
out: &struct{ B1, B2 *bool }{
|
|
|
|
B1: BoolPtr(true),
|
|
|
|
B2: BoolPtr(false),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Clone pointer to string
|
|
|
|
in: &struct{ S *string }{
|
|
|
|
S: StringPtr("string1"),
|
|
|
|
},
|
|
|
|
out: &struct{ S *string }{
|
|
|
|
S: StringPtr("string1"),
|
|
|
|
},
|
|
|
|
},
|
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
|
|
|
{
|
|
|
|
// Clone pointer to int64
|
|
|
|
in: &struct{ S *int64 }{
|
|
|
|
S: Int64Ptr(5),
|
|
|
|
},
|
|
|
|
out: &struct{ S *int64 }{
|
|
|
|
S: Int64Ptr(5),
|
|
|
|
},
|
|
|
|
},
|
2015-10-30 19:42:57 +01:00
|
|
|
{
|
|
|
|
// Clone struct
|
|
|
|
in: &struct{ S struct{ S string } }{
|
|
|
|
S: struct{ S string }{
|
|
|
|
S: "string1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
out: &struct{ S struct{ S string } }{
|
|
|
|
S: struct{ S string }{
|
|
|
|
S: "string1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Clone struct pointer
|
|
|
|
in: &struct{ S *struct{ S string } }{
|
|
|
|
S: &struct{ S string }{
|
|
|
|
S: "string1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
out: &struct{ S *struct{ S string } }{
|
|
|
|
S: &struct{ S string }{
|
|
|
|
S: "string1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Clone interface
|
|
|
|
in: &struct{ S interface{} }{
|
|
|
|
S: &struct{ S string }{
|
|
|
|
S: "string1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
out: &struct{ S interface{} }{
|
|
|
|
S: &struct{ S string }{
|
|
|
|
S: "string1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2015-11-21 02:01:25 +01:00
|
|
|
// Clone nested interface
|
|
|
|
in: &struct {
|
|
|
|
Nested struct{ S interface{} }
|
|
|
|
}{
|
|
|
|
Nested: struct{ S interface{} }{
|
|
|
|
S: &struct{ S string }{
|
|
|
|
S: "string1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
out: &struct {
|
|
|
|
Nested struct{ S interface{} }
|
|
|
|
}{
|
|
|
|
Nested: struct{ S interface{} }{
|
|
|
|
S: &struct{ S string }{
|
|
|
|
S: "string1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, {
|
2015-10-30 19:42:57 +01:00
|
|
|
// Empty struct
|
|
|
|
in: &struct{}{},
|
|
|
|
out: &struct{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Interface nil
|
|
|
|
in: &struct{ S interface{} }{
|
|
|
|
S: nil,
|
|
|
|
},
|
|
|
|
out: &struct{ S interface{} }{
|
|
|
|
S: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Interface pointer to nil
|
|
|
|
in: &struct{ S interface{} }{
|
|
|
|
S: (*struct{ S string })(nil),
|
|
|
|
},
|
|
|
|
out: &struct{ S interface{} }{
|
|
|
|
S: (*struct{ S string })(nil),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Pointer nil
|
|
|
|
in: &struct{ S *struct{} }{
|
|
|
|
S: nil,
|
|
|
|
},
|
|
|
|
out: &struct{ S *struct{} }{
|
|
|
|
S: nil,
|
|
|
|
},
|
|
|
|
},
|
2015-11-21 02:03:25 +01:00
|
|
|
{
|
|
|
|
// Anonymous struct
|
|
|
|
in: &struct {
|
|
|
|
EmbeddedStruct
|
|
|
|
Nested struct{ EmbeddedStruct }
|
|
|
|
}{
|
|
|
|
EmbeddedStruct: EmbeddedStruct{
|
|
|
|
S: "string1",
|
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
|
|
|
I: Int64Ptr(55),
|
2015-11-21 02:03:25 +01:00
|
|
|
},
|
|
|
|
Nested: struct{ EmbeddedStruct }{
|
|
|
|
EmbeddedStruct: EmbeddedStruct{
|
|
|
|
S: "string2",
|
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
|
|
|
I: Int64Ptr(5),
|
2015-11-21 02:03:25 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
out: &struct {
|
|
|
|
EmbeddedStruct
|
|
|
|
Nested struct{ EmbeddedStruct }
|
|
|
|
}{
|
|
|
|
EmbeddedStruct: EmbeddedStruct{
|
|
|
|
S: "string1",
|
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
|
|
|
I: Int64Ptr(55),
|
2015-11-21 02:03:25 +01:00
|
|
|
},
|
|
|
|
Nested: struct{ EmbeddedStruct }{
|
|
|
|
EmbeddedStruct: EmbeddedStruct{
|
|
|
|
S: "string2",
|
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
|
|
|
I: Int64Ptr(5),
|
2015-11-21 02:03:25 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Anonymous interface
|
|
|
|
in: &struct {
|
|
|
|
EmbeddedInterface
|
|
|
|
Nested struct{ EmbeddedInterface }
|
|
|
|
}{
|
|
|
|
EmbeddedInterface: &struct{ S string }{
|
|
|
|
S: "string1",
|
|
|
|
},
|
|
|
|
Nested: struct{ EmbeddedInterface }{
|
|
|
|
EmbeddedInterface: &struct{ S string }{
|
|
|
|
S: "string2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
out: &struct {
|
|
|
|
EmbeddedInterface
|
|
|
|
Nested struct{ EmbeddedInterface }
|
|
|
|
}{
|
|
|
|
EmbeddedInterface: &struct{ S string }{
|
|
|
|
S: "string1",
|
|
|
|
},
|
|
|
|
Nested: struct{ EmbeddedInterface }{
|
|
|
|
EmbeddedInterface: &struct{ S string }{
|
|
|
|
S: "string2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
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
|
|
|
type EmbeddedStruct struct {
|
|
|
|
S string
|
|
|
|
I *int64
|
|
|
|
}
|
2015-11-21 02:03:25 +01:00
|
|
|
type EmbeddedInterface interface{}
|
|
|
|
|
2023-11-09 06:59:29 +01:00
|
|
|
func isPointerToEmptyStruct(v any) bool {
|
|
|
|
t := reflect.TypeOf(v)
|
|
|
|
if t.Kind() != reflect.Ptr {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
t = t.Elem()
|
|
|
|
if t.Kind() != reflect.Struct {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if t.NumField() > 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-10-30 19:42:57 +01:00
|
|
|
func TestCloneProperties(t *testing.T) {
|
|
|
|
for _, testCase := range clonePropertiesTestCases {
|
|
|
|
testString := fmt.Sprintf("%s", testCase.in)
|
|
|
|
|
2020-01-28 01:14:31 +01:00
|
|
|
got := CloneProperties(reflect.ValueOf(testCase.in)).Interface()
|
2015-10-30 19:42:57 +01:00
|
|
|
|
|
|
|
if !reflect.DeepEqual(testCase.out, got) {
|
|
|
|
t.Errorf("test case %s", testString)
|
|
|
|
t.Errorf("incorrect output")
|
|
|
|
t.Errorf(" expected: %#v", testCase.out)
|
|
|
|
t.Errorf(" got: %#v", got)
|
|
|
|
}
|
2023-11-09 06:59:29 +01:00
|
|
|
if testCase.out == got && !isPointerToEmptyStruct(testCase.out) {
|
2021-05-21 23:56:53 +02:00
|
|
|
t.Errorf("test case %s", testString)
|
|
|
|
t.Errorf("items should be cloned, not the original")
|
|
|
|
t.Errorf(" expected: %s", testCase.out)
|
|
|
|
t.Errorf(" got: %s", got)
|
|
|
|
}
|
2015-10-30 19:42:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var cloneEmptyPropertiesTestCases = []struct {
|
|
|
|
in interface{}
|
|
|
|
out interface{}
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
// Valid inputs
|
|
|
|
|
|
|
|
{
|
|
|
|
// Clone bool
|
|
|
|
in: &struct{ B1, B2 bool }{
|
|
|
|
B1: true,
|
|
|
|
B2: false,
|
|
|
|
},
|
|
|
|
out: &struct{ B1, B2 bool }{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Clone strings
|
|
|
|
in: &struct{ S string }{
|
|
|
|
S: "string1",
|
|
|
|
},
|
|
|
|
out: &struct{ S string }{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Clone slice
|
|
|
|
in: &struct{ S []string }{
|
|
|
|
S: []string{"string1"},
|
|
|
|
},
|
|
|
|
out: &struct{ S []string }{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Clone empty slice
|
|
|
|
in: &struct{ S []string }{
|
|
|
|
S: []string{},
|
|
|
|
},
|
|
|
|
out: &struct{ S []string }{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Clone nil slice
|
|
|
|
in: &struct{ S []string }{},
|
|
|
|
out: &struct{ S []string }{},
|
|
|
|
},
|
2018-10-03 06:57:47 +02:00
|
|
|
{
|
|
|
|
// Clone slice of structs
|
|
|
|
in: &struct{ S []struct{ T string } }{
|
|
|
|
S: []struct{ T string }{
|
|
|
|
{"string1"}, {"string2"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
out: &struct{ S []struct{ T string } }{
|
|
|
|
S: []struct{ T string }(nil),
|
|
|
|
},
|
|
|
|
},
|
2015-10-30 19:42:57 +01:00
|
|
|
{
|
2015-10-30 23:53:55 +01:00
|
|
|
// Clone pointer to bool
|
|
|
|
in: &struct{ B1, B2 *bool }{
|
|
|
|
B1: BoolPtr(true),
|
|
|
|
B2: BoolPtr(false),
|
|
|
|
},
|
|
|
|
out: &struct{ B1, B2 *bool }{},
|
|
|
|
},
|
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
|
|
|
{
|
|
|
|
// Clone pointer to int64
|
|
|
|
in: &struct{ B1, B2 *int64 }{
|
|
|
|
B1: Int64Ptr(5),
|
|
|
|
B2: Int64Ptr(4),
|
|
|
|
},
|
|
|
|
out: &struct{ B1, B2 *int64 }{},
|
|
|
|
},
|
2015-10-30 23:53:55 +01:00
|
|
|
{
|
|
|
|
// Clone pointer to string
|
|
|
|
in: &struct{ S *string }{
|
|
|
|
S: StringPtr("string1"),
|
|
|
|
},
|
|
|
|
out: &struct{ S *string }{},
|
|
|
|
},
|
2015-10-30 19:42:57 +01:00
|
|
|
{
|
|
|
|
// Clone struct
|
|
|
|
in: &struct{ S struct{ S string } }{
|
|
|
|
S: struct{ S string }{
|
|
|
|
S: "string1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
out: &struct{ S struct{ S string } }{
|
|
|
|
S: struct{ S string }{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Clone struct pointer
|
|
|
|
in: &struct{ S *struct{ S string } }{
|
|
|
|
S: &struct{ S string }{
|
|
|
|
S: "string1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
out: &struct{ S *struct{ S string } }{
|
|
|
|
S: &struct{ S string }{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Clone interface
|
|
|
|
in: &struct{ S interface{} }{
|
|
|
|
S: &struct{ S string }{
|
|
|
|
S: "string1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
out: &struct{ S interface{} }{
|
|
|
|
S: &struct{ S string }{},
|
|
|
|
},
|
|
|
|
},
|
2015-11-21 02:01:25 +01:00
|
|
|
{
|
|
|
|
// Clone nested interface
|
|
|
|
in: &struct {
|
|
|
|
Nested struct{ S interface{} }
|
|
|
|
}{
|
|
|
|
Nested: struct{ S interface{} }{
|
|
|
|
S: &struct{ S string }{
|
|
|
|
S: "string1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
out: &struct {
|
|
|
|
Nested struct{ S interface{} }
|
|
|
|
}{
|
|
|
|
Nested: struct{ S interface{} }{
|
|
|
|
S: &struct{ S string }{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-10-30 19:42:57 +01:00
|
|
|
{
|
|
|
|
// Empty struct
|
|
|
|
in: &struct{}{},
|
|
|
|
out: &struct{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Interface nil
|
|
|
|
in: &struct{ S interface{} }{
|
|
|
|
S: nil,
|
|
|
|
},
|
|
|
|
out: &struct{ S interface{} }{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Interface pointer to nil
|
|
|
|
in: &struct{ S interface{} }{
|
|
|
|
S: (*struct{ S string })(nil),
|
|
|
|
},
|
|
|
|
out: &struct{ S interface{} }{
|
|
|
|
S: (*struct{ S string })(nil),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Pointer nil
|
|
|
|
in: &struct{ S *struct{} }{
|
|
|
|
S: nil,
|
|
|
|
},
|
|
|
|
out: &struct{ S *struct{} }{},
|
|
|
|
},
|
2015-11-21 02:01:25 +01:00
|
|
|
{
|
|
|
|
// Anonymous struct
|
|
|
|
in: &struct {
|
|
|
|
EmbeddedStruct
|
|
|
|
Nested struct{ EmbeddedStruct }
|
|
|
|
}{
|
|
|
|
EmbeddedStruct: EmbeddedStruct{
|
|
|
|
S: "string1",
|
|
|
|
},
|
|
|
|
Nested: struct{ EmbeddedStruct }{
|
|
|
|
EmbeddedStruct: EmbeddedStruct{
|
|
|
|
S: "string2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
out: &struct {
|
|
|
|
EmbeddedStruct
|
|
|
|
Nested struct{ EmbeddedStruct }
|
|
|
|
}{
|
|
|
|
EmbeddedStruct: EmbeddedStruct{},
|
|
|
|
Nested: struct{ EmbeddedStruct }{
|
|
|
|
EmbeddedStruct: EmbeddedStruct{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Anonymous interface
|
|
|
|
in: &struct {
|
|
|
|
EmbeddedInterface
|
|
|
|
Nested struct{ EmbeddedInterface }
|
|
|
|
}{
|
|
|
|
EmbeddedInterface: &struct{ S string }{
|
|
|
|
S: "string1",
|
|
|
|
},
|
|
|
|
Nested: struct{ EmbeddedInterface }{
|
|
|
|
EmbeddedInterface: &struct{ S string }{
|
|
|
|
S: "string2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
out: &struct {
|
|
|
|
EmbeddedInterface
|
|
|
|
Nested struct{ EmbeddedInterface }
|
|
|
|
}{
|
|
|
|
EmbeddedInterface: &struct{ S string }{},
|
|
|
|
Nested: struct{ EmbeddedInterface }{
|
|
|
|
EmbeddedInterface: &struct{ S string }{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-10-30 19:42:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCloneEmptyProperties(t *testing.T) {
|
|
|
|
for _, testCase := range cloneEmptyPropertiesTestCases {
|
2015-11-21 02:01:25 +01:00
|
|
|
testString := fmt.Sprintf("%#v", testCase.in)
|
2015-10-30 19:42:57 +01:00
|
|
|
|
2020-01-28 01:14:31 +01:00
|
|
|
got := CloneEmptyProperties(reflect.ValueOf(testCase.in)).Interface()
|
2015-10-30 19:42:57 +01:00
|
|
|
|
|
|
|
if !reflect.DeepEqual(testCase.out, got) {
|
|
|
|
t.Errorf("test case %s", testString)
|
|
|
|
t.Errorf("incorrect output")
|
|
|
|
t.Errorf(" expected: %#v", testCase.out)
|
|
|
|
t.Errorf(" got: %#v", got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestZeroProperties(t *testing.T) {
|
|
|
|
for _, testCase := range cloneEmptyPropertiesTestCases {
|
2015-11-21 02:01:25 +01:00
|
|
|
testString := fmt.Sprintf("%#v", testCase.in)
|
2015-10-30 19:42:57 +01:00
|
|
|
|
2020-01-28 01:14:31 +01:00
|
|
|
got := CloneProperties(reflect.ValueOf(testCase.in)).Interface()
|
|
|
|
ZeroProperties(reflect.ValueOf(got))
|
2015-10-30 19:42:57 +01:00
|
|
|
|
|
|
|
if !reflect.DeepEqual(testCase.out, got) {
|
|
|
|
t.Errorf("test case %s", testString)
|
|
|
|
t.Errorf("incorrect output")
|
|
|
|
t.Errorf(" expected: %#v", testCase.out)
|
|
|
|
t.Errorf(" got: %#v", got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|