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:
parent
0335565677
commit
b48d4aea85
3 changed files with 31 additions and 2 deletions
|
@ -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())
|
||||||
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
Loading…
Reference in a new issue