propertyIndexesWithTag can handle slice of struct

The function now can traverse into a field whose type is slice of
struct. When reading field values from the returned indexes, Soong will
check if the next field is a slice of struct or not. If so, it will
recurse into all the values in the slice.

Bug: 181018147
Test: m nothing
Change-Id: Ib8a7b7911a0be37a6dc03079adeb906497e60875
This commit is contained in:
Jiyong Park 2021-02-24 00:56:48 +09:00
parent 0335565677
commit b48d4aea85
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 {
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++ {
field := t.Field(i)
ft := field.Type
if isStruct(ft) || isStructPtr(ft) {
if ft.Kind() == reflect.Ptr {
if isStruct(ft) || isStructPtr(ft) || isSliceOfStruct(ft) {
if ft.Kind() == reflect.Ptr || ft.Kind() == reflect.Slice {
ft = ft.Elem()
}
subIndexes := propertyIndexesWithTag(ft, key, value)

View file

@ -155,6 +155,31 @@ func TestPropertyIndexesWithTag(t *testing.T) {
}{},
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",
ps: (*struct {