Merge "Refactor build_release and test code" am: 8af15e0297
am: bfb9f280c6
am: 635a71352c
Original change: https://android-review.googlesource.com/c/platform/build/soong/+/1963152 Change-Id: I85e059e5a631b52417d6ec5d3d1eaa2fe3a52a80
This commit is contained in:
@@ -230,51 +230,63 @@ func (p *propertyPruner) gatherFields(structType reflect.Type, containingStructA
|
|||||||
return container.Field(fieldIndex)
|
return container.Field(fieldIndex)
|
||||||
}
|
}
|
||||||
|
|
||||||
zeroValue := reflect.Zero(field.Type)
|
fieldType := field.Type
|
||||||
fieldPruner := func(container reflect.Value) {
|
if selector(name, field) {
|
||||||
if containingStructAccessor != nil {
|
zeroValue := reflect.Zero(fieldType)
|
||||||
// This is an embedded structure so first access the field for the embedded
|
fieldPruner := func(container reflect.Value) {
|
||||||
// structure.
|
if containingStructAccessor != nil {
|
||||||
container = containingStructAccessor(container)
|
// This is an embedded structure so first access the field for the embedded
|
||||||
|
// structure.
|
||||||
|
container = containingStructAccessor(container)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip through interface and pointer values to find the structure.
|
||||||
|
container = getStructValue(container)
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
panic(fmt.Errorf("%s\n\tfor field (index %d, name %s)", r, fieldIndex, name))
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Set the field.
|
||||||
|
container.Field(fieldIndex).Set(zeroValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Skip through interface and pointer values to find the structure.
|
|
||||||
container = getStructValue(container)
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
if r := recover(); r != nil {
|
|
||||||
panic(fmt.Errorf("%s for fieldIndex %d of field %s of container %#v", r, fieldIndex, name, container.Interface()))
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Set the field.
|
|
||||||
container.Field(fieldIndex).Set(zeroValue)
|
|
||||||
}
|
|
||||||
|
|
||||||
if selector(name, field) {
|
|
||||||
property := prunerProperty{
|
property := prunerProperty{
|
||||||
name,
|
name,
|
||||||
fieldPruner,
|
fieldPruner,
|
||||||
}
|
}
|
||||||
p.properties = append(p.properties, property)
|
p.properties = append(p.properties, property)
|
||||||
} else if field.Type.Kind() == reflect.Struct {
|
} else {
|
||||||
// Gather fields from the nested or embedded structure.
|
switch fieldType.Kind() {
|
||||||
var subNamePrefix string
|
case reflect.Struct:
|
||||||
if field.Anonymous {
|
// Gather fields from the nested or embedded structure.
|
||||||
subNamePrefix = namePrefix
|
var subNamePrefix string
|
||||||
} else {
|
if field.Anonymous {
|
||||||
subNamePrefix = name + "."
|
subNamePrefix = namePrefix
|
||||||
|
} else {
|
||||||
|
subNamePrefix = name + "."
|
||||||
|
}
|
||||||
|
p.gatherFields(fieldType, fieldGetter, subNamePrefix, selector)
|
||||||
}
|
}
|
||||||
p.gatherFields(field.Type, fieldGetter, subNamePrefix, selector)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// pruneProperties will prune (set to zero value) any properties in the supplied struct.
|
// pruneProperties will prune (set to zero value) any properties in the struct referenced by the
|
||||||
|
// supplied struct pointer.
|
||||||
//
|
//
|
||||||
// The struct must be of the same type as was originally passed to newPropertyPruner to create this
|
// The struct must be of the same type as was originally passed to newPropertyPruner to create this
|
||||||
// propertyPruner.
|
// propertyPruner.
|
||||||
func (p *propertyPruner) pruneProperties(propertiesStruct interface{}) {
|
func (p *propertyPruner) pruneProperties(propertiesStruct interface{}) {
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
panic(fmt.Errorf("%s\n\tof container %#v", r, propertiesStruct))
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
structValue := reflect.ValueOf(propertiesStruct)
|
structValue := reflect.ValueOf(propertiesStruct)
|
||||||
for _, property := range p.properties {
|
for _, property := range p.properties {
|
||||||
property.prunerFunc(structValue)
|
property.prunerFunc(structValue)
|
||||||
|
@@ -15,6 +15,7 @@
|
|||||||
package sdk
|
package sdk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -132,54 +133,73 @@ func TestPropertyPrunerByBuildRelease(t *testing.T) {
|
|||||||
Nested nested
|
Nested nested
|
||||||
}
|
}
|
||||||
|
|
||||||
input := testBuildReleasePruner{
|
inputFactory := func() testBuildReleasePruner {
|
||||||
Default: "Default",
|
return testBuildReleasePruner{
|
||||||
S_and_T_only: "S_and_T_only",
|
Default: "Default",
|
||||||
T_later: "T_later",
|
S_and_T_only: "S_and_T_only",
|
||||||
Nested: nested{
|
T_later: "T_later",
|
||||||
F1_only: "F1_only",
|
Nested: nested{
|
||||||
},
|
F1_only: "F1_only",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
marshal := func(t interface{}) string {
|
||||||
|
bytes, err := json.MarshalIndent(t, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return string(bytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
assertJsonEquals := func(t *testing.T, expected, actual interface{}) {
|
||||||
|
t.Helper()
|
||||||
|
expectedJson := marshal(expected)
|
||||||
|
actualJson := marshal(actual)
|
||||||
|
if actualJson != expectedJson {
|
||||||
|
t.Errorf("test struct: expected:\n%s\n got:\n%s", expectedJson, actualJson)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Run("target S", func(t *testing.T) {
|
t.Run("target S", func(t *testing.T) {
|
||||||
testStruct := input
|
testStruct := inputFactory()
|
||||||
pruner := newPropertyPrunerByBuildRelease(&testStruct, buildReleaseS)
|
pruner := newPropertyPrunerByBuildRelease(&testStruct, buildReleaseS)
|
||||||
pruner.pruneProperties(&testStruct)
|
pruner.pruneProperties(&testStruct)
|
||||||
|
|
||||||
expected := input
|
expected := inputFactory()
|
||||||
expected.T_later = ""
|
expected.T_later = ""
|
||||||
expected.Nested.F1_only = ""
|
expected.Nested.F1_only = ""
|
||||||
android.AssertDeepEquals(t, "test struct", expected, testStruct)
|
assertJsonEquals(t, expected, testStruct)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("target T", func(t *testing.T) {
|
t.Run("target T", func(t *testing.T) {
|
||||||
testStruct := input
|
testStruct := inputFactory()
|
||||||
pruner := newPropertyPrunerByBuildRelease(&testStruct, buildReleaseT)
|
pruner := newPropertyPrunerByBuildRelease(&testStruct, buildReleaseT)
|
||||||
pruner.pruneProperties(&testStruct)
|
pruner.pruneProperties(&testStruct)
|
||||||
|
|
||||||
expected := input
|
expected := inputFactory()
|
||||||
expected.Nested.F1_only = ""
|
expected.Nested.F1_only = ""
|
||||||
android.AssertDeepEquals(t, "test struct", expected, testStruct)
|
assertJsonEquals(t, expected, testStruct)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("target F1", func(t *testing.T) {
|
t.Run("target F1", func(t *testing.T) {
|
||||||
testStruct := input
|
testStruct := inputFactory()
|
||||||
pruner := newPropertyPrunerByBuildRelease(&testStruct, buildReleaseFuture1)
|
pruner := newPropertyPrunerByBuildRelease(&testStruct, buildReleaseFuture1)
|
||||||
pruner.pruneProperties(&testStruct)
|
pruner.pruneProperties(&testStruct)
|
||||||
|
|
||||||
expected := input
|
expected := inputFactory()
|
||||||
expected.S_and_T_only = ""
|
expected.S_and_T_only = ""
|
||||||
android.AssertDeepEquals(t, "test struct", expected, testStruct)
|
assertJsonEquals(t, expected, testStruct)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("target F2", func(t *testing.T) {
|
t.Run("target F2", func(t *testing.T) {
|
||||||
testStruct := input
|
testStruct := inputFactory()
|
||||||
pruner := newPropertyPrunerByBuildRelease(&testStruct, buildReleaseFuture2)
|
pruner := newPropertyPrunerByBuildRelease(&testStruct, buildReleaseFuture2)
|
||||||
pruner.pruneProperties(&testStruct)
|
pruner.pruneProperties(&testStruct)
|
||||||
|
|
||||||
expected := input
|
expected := inputFactory()
|
||||||
expected.S_and_T_only = ""
|
expected.S_and_T_only = ""
|
||||||
expected.Nested.F1_only = ""
|
expected.Nested.F1_only = ""
|
||||||
android.AssertDeepEquals(t, "test struct", expected, testStruct)
|
assertJsonEquals(t, expected, testStruct)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user