Identify the type of the list for bpdocs (#326)

* Identify the type of the list for bpdocs

Test: go test bpdoc tests
Test: m soong_docs
Change-Id: I6a4a916e1f72b3fc702da90c32a2eddca70b3bac
This commit is contained in:
Liz Kammer 2020-11-04 17:20:11 -08:00 committed by GitHub
parent ed35255eca
commit 764a771917
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 116 additions and 41 deletions

View file

@ -6,6 +6,11 @@ import (
"testing"
)
type propInfo struct {
name string
typ string
}
type parentProps struct {
A string
@ -61,28 +66,76 @@ func TestAllPackages(t *testing.T) {
pkg := packages[0]
expectedProps := map[string][]string{
"bar": []string{
"a",
"nested",
"nested.c",
"nested_struct",
"nested_struct.e",
"struct_has_embed",
"struct_has_embed.nested_in_embedded",
"struct_has_embed.nested_in_embedded.e",
"struct_has_embed.f",
"nested_in_other_embedded",
"nested_in_other_embedded.g",
"h",
expectedProps := map[string][]propInfo{
"bar": []propInfo{
propInfo{
name: "a",
typ: "string",
},
propInfo{
name: "nested",
typ: "",
},
propInfo{
name: "nested.c",
typ: "string",
},
propInfo{
name: "nested_struct",
typ: "structToNest",
},
propInfo{
name: "nested_struct.e",
typ: "string",
},
propInfo{
name: "struct_has_embed",
typ: "StructWithEmbedded",
},
propInfo{
name: "struct_has_embed.nested_in_embedded",
typ: "structToNest",
},
propInfo{
name: "struct_has_embed.nested_in_embedded.e",
typ: "string",
},
propInfo{
name: "struct_has_embed.f",
typ: "string",
},
propInfo{
name: "list_of_ints",
typ: "list of int",
},
propInfo{
name: "list_of_nested",
typ: "list of structToNest",
},
propInfo{
name: "nested_in_other_embedded",
typ: "otherStructToNest",
},
propInfo{
name: "nested_in_other_embedded.g",
typ: "string",
},
propInfo{
name: "h",
typ: "string",
},
},
"foo": []string{
"a",
"foo": []propInfo{
propInfo{
name: "a",
typ: "string",
},
},
}
for _, m := range pkg.ModuleTypes {
foundProps := []string{}
foundProps := []propInfo{}
for _, p := range m.PropertyStructs {
nestedProps, errs := findAllProperties("", p.Properties)
foundProps = append(foundProps, nestedProps...)
@ -98,11 +151,15 @@ func TestAllPackages(t *testing.T) {
}
}
func findAllProperties(prefix string, properties []Property) ([]string, []error) {
foundProps := []string{}
func findAllProperties(prefix string, properties []Property) ([]propInfo, []error) {
foundProps := []propInfo{}
errs := []error{}
for _, p := range properties {
foundProps = append(foundProps, prefix+p.Name)
prop := propInfo{
name: prefix + p.Name,
typ: p.Type,
}
foundProps = append(foundProps, prop)
if hasTag(p.Tag, "blueprint", "mutated") {
err := fmt.Errorf("Property %s has `blueprint:\"mutated\" tag but should have been excluded.", p.Name)
errs = append(errs, err)

View file

@ -197,8 +197,7 @@ func structProperties(structType *ast.StructType) (props []Property, err error)
}
}
for _, n := range names {
var name, typ, tag, text string
var innerProps []Property
var name, tag, text string
if n != nil {
name = proptools.PropertyNameForField(n.Name)
}
@ -211,25 +210,9 @@ func structProperties(structType *ast.StructType) (props []Property, err error)
return nil, err
}
}
t := f.Type
if star, ok := t.(*ast.StarExpr); ok {
t = star.X
}
switch a := t.(type) {
case *ast.ArrayType:
typ = "list of strings"
case *ast.InterfaceType:
typ = "interface"
case *ast.Ident:
typ = a.Name
case *ast.StructType:
innerProps, err = structProperties(a)
if err != nil {
return nil, err
}
default:
typ = fmt.Sprintf("%T", f.Type)
typ, innerProps, err := getType(f.Type)
if err != nil {
return nil, err
}
props = append(props, Property{
@ -245,6 +228,37 @@ func structProperties(structType *ast.StructType) (props []Property, err error)
return props, nil
}
func getType(expr ast.Expr) (typ string, innerProps []Property, err error) {
var t ast.Expr
if star, ok := expr.(*ast.StarExpr); ok {
t = star.X
} else {
t = expr
}
switch a := t.(type) {
case *ast.ArrayType:
var elt string
elt, innerProps, err = getType(a.Elt)
if err != nil {
return "", nil, err
}
typ = "list of " + elt
case *ast.InterfaceType:
typ = "interface"
case *ast.Ident:
typ = a.Name
case *ast.StructType:
innerProps, err = structProperties(a)
if err != nil {
return "", nil, err
}
default:
typ = fmt.Sprintf("%T", expr)
}
return typ, innerProps, nil
}
func (ps *PropertyStruct) ExcludeByTag(key, value string) {
filterPropsByTag(&ps.Properties, key, value, true)
}

View file

@ -77,6 +77,10 @@ type complexProps struct {
Struct_has_embed StructWithEmbedded
OtherStructToEmbed
List_of_ints []int
List_of_nested []structToNest
}
// props docs.