From 764a77191737d68820082af40cf5ddc98dd264a3 Mon Sep 17 00:00:00 2001 From: Liz Kammer Date: Wed, 4 Nov 2020 17:20:11 -0800 Subject: [PATCH] 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 --- bootstrap/bpdoc/bpdoc_test.go | 97 +++++++++++++++++++++++++++------- bootstrap/bpdoc/properties.go | 56 ++++++++++++-------- bootstrap/bpdoc/reader_test.go | 4 ++ 3 files changed, 116 insertions(+), 41 deletions(-) diff --git a/bootstrap/bpdoc/bpdoc_test.go b/bootstrap/bpdoc/bpdoc_test.go index 70834ef..67ad783 100644 --- a/bootstrap/bpdoc/bpdoc_test.go +++ b/bootstrap/bpdoc/bpdoc_test.go @@ -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) diff --git a/bootstrap/bpdoc/properties.go b/bootstrap/bpdoc/properties.go index a28384e..2ca8e65 100644 --- a/bootstrap/bpdoc/properties.go +++ b/bootstrap/bpdoc/properties.go @@ -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) } diff --git a/bootstrap/bpdoc/reader_test.go b/bootstrap/bpdoc/reader_test.go index 69077e2..bf324bf 100644 --- a/bootstrap/bpdoc/reader_test.go +++ b/bootstrap/bpdoc/reader_test.go @@ -77,6 +77,10 @@ type complexProps struct { Struct_has_embed StructWithEmbedded OtherStructToEmbed + + List_of_ints []int + + List_of_nested []structToNest } // props docs.