Merge "propertyIndexesWithTag can handle slice of struct" am: 2910183696

Original change: https://android-review.googlesource.com/c/platform/build/blueprint/+/1601875

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: I4908415cc4eb454030c0224fb990be6c2b89b443
This commit is contained in:
Treehugger Robot 2021-02-24 01:34:38 +00:00 committed by Automerger Merge Worker
commit 8d9d43c46c
3 changed files with 31 additions and 2 deletions

View file

@ -125,3 +125,7 @@ func isStructPtr(t reflect.Type) bool {
func isSlice(t reflect.Type) bool { func isSlice(t reflect.Type) bool {
return t.Kind() == reflect.Slice return t.Kind() == reflect.Slice
} }
func isSliceOfStruct(t reflect.Type) bool {
return isSlice(t) && isStruct(t.Elem())
}

View file

@ -56,8 +56,8 @@ func propertyIndexesWithTag(t reflect.Type, key, value string) [][]int {
for i := 0; i < t.NumField(); i++ { for i := 0; i < t.NumField(); i++ {
field := t.Field(i) field := t.Field(i)
ft := field.Type ft := field.Type
if isStruct(ft) || isStructPtr(ft) { if isStruct(ft) || isStructPtr(ft) || isSliceOfStruct(ft) {
if ft.Kind() == reflect.Ptr { if ft.Kind() == reflect.Ptr || ft.Kind() == reflect.Slice {
ft = ft.Elem() ft = ft.Elem()
} }
subIndexes := propertyIndexesWithTag(ft, key, value) subIndexes := propertyIndexesWithTag(ft, key, value)

View file

@ -155,6 +155,31 @@ func TestPropertyIndexesWithTag(t *testing.T) {
}{}, }{},
want: [][]int{{0, 0}}, want: [][]int{{0, 0}},
}, },
{
name: "slice of struct",
ps: &struct {
Other int
Foo []struct {
Other int
Bar string `name:"value"`
}
}{},
want: [][]int{{1, 1}},
},
{
name: "slice^2 of struct",
ps: &struct {
Other int
Foo []struct {
Other int
Bar []struct {
Other int
Baz string `name:"value"`
}
}
}{},
want: [][]int{{1, 1, 1}},
},
{ {
name: "nil", name: "nil",
ps: (*struct { ps: (*struct {