diff --git a/cmd/libs/go2idl/client-gen/generators/fake/generator_fake_for_type.go b/cmd/libs/go2idl/client-gen/generators/fake/generator_fake_for_type.go index b341653ce13..81c750d0a41 100644 --- a/cmd/libs/go2idl/client-gen/generators/fake/generator_fake_for_type.go +++ b/cmd/libs/go2idl/client-gen/generators/fake/generator_fake_for_type.go @@ -95,11 +95,9 @@ func (g *genFakeForType) GenerateType(c *generator.Context, t *types.Type, w io. } // allow user to define a group name that's different from the one parsed from the directory. - for _, comment := range c.Universe.Package(g.inputPackage).DocComments { - comment = strings.TrimLeft(comment, "//") - if override, ok := types.ExtractCommentTags("+", comment)["groupName"]; ok { - groupName = override - } + p := c.Universe.Package(g.inputPackage) + if override, ok := types.ExtractCommentTags("+", p.DocComments)["groupName"]; ok { + groupName = override } m := map[string]interface{}{ diff --git a/cmd/libs/go2idl/client-gen/generators/generator_for_group.go b/cmd/libs/go2idl/client-gen/generators/generator_for_group.go index 9dca192a49e..265f5e584c2 100644 --- a/cmd/libs/go2idl/client-gen/generators/generator_for_group.go +++ b/cmd/libs/go2idl/client-gen/generators/generator_for_group.go @@ -18,7 +18,6 @@ package generators import ( "io" - "strings" "k8s.io/kubernetes/cmd/libs/go2idl/client-gen/generators/normalization" "k8s.io/kubernetes/cmd/libs/go2idl/generator" @@ -74,11 +73,9 @@ func (g *genGroup) GenerateType(c *generator.Context, t *types.Type, w io.Writer groupName = "" } // allow user to define a group name that's different from the one parsed from the directory. - for _, comment := range c.Universe.Package(g.inputPacakge).DocComments { - comment = strings.TrimLeft(comment, "//") - if override, ok := types.ExtractCommentTags("+", comment)["groupName"]; ok && override != "" { - groupName = override - } + p := c.Universe.Package(g.inputPacakge) + if override, ok := types.ExtractCommentTags("+", p.DocComments)["groupName"]; ok && override != "" { + groupName = override } m := map[string]interface{}{ diff --git a/cmd/libs/go2idl/conversion-gen/generators/conversion.go b/cmd/libs/go2idl/conversion-gen/generators/conversion.go index 35dc1f3f37f..9eb6bd88a00 100644 --- a/cmd/libs/go2idl/conversion-gen/generators/conversion.go +++ b/cmd/libs/go2idl/conversion-gen/generators/conversion.go @@ -232,11 +232,8 @@ func Packages(context *generator.Context, arguments *args.GeneratorArgs) generat // Only generate conversions for package which explicitly requested it // byt setting "+genversion=true" in their doc.go file. filtered := false - for _, comment := range p.DocComments { - comment := strings.Trim(comment, "//") - if types.ExtractCommentTags("+", comment)["genconversion"] == "true" { - filtered = true - } + if types.ExtractCommentTags("+", p.DocComments)["genconversion"] == "true" { + filtered = true } if !filtered { continue diff --git a/cmd/libs/go2idl/go-to-protobuf/protobuf/generator.go b/cmd/libs/go2idl/go-to-protobuf/protobuf/generator.go index 3bfa47f9150..23de08692d0 100644 --- a/cmd/libs/go2idl/go-to-protobuf/protobuf/generator.go +++ b/cmd/libs/go2idl/go-to-protobuf/protobuf/generator.go @@ -268,7 +268,7 @@ func (b bodyGen) doAlias(sw *generator.SnippetWriter) error { Members: []types.Member{ { Name: "Items", - CommentLines: fmt.Sprintf("items, if empty, will result in an empty %s\n", kind), + CommentLines: []string{fmt.Sprintf("items, if empty, will result in an empty %s\n", kind)}, Type: b.t.Underlying, }, }, @@ -410,7 +410,7 @@ type protoField struct { Nullable bool Extras map[string]string - CommentLines string + CommentLines []string } var ( @@ -687,8 +687,7 @@ func membersToFields(locator ProtobufLocator, t *types.Type, localPackage types. return fields, nil } -func genComment(out io.Writer, comment, indent string) { - lines := strings.Split(comment, "\n") +func genComment(out io.Writer, lines []string, indent string) { for { l := len(lines) if l == 0 || len(lines[l-1]) != 0 { diff --git a/cmd/libs/go2idl/parser/parse.go b/cmd/libs/go2idl/parser/parse.go index 7915d8c6852..3d264a94bb5 100644 --- a/cmd/libs/go2idl/parser/parse.go +++ b/cmd/libs/go2idl/parser/parse.go @@ -371,10 +371,7 @@ func (b *Builder) FindTypes() (types.Universe, error) { for _, f := range b.parsed[pkgPath] { if strings.HasSuffix(f.name, "/doc.go") { if f.file.Doc != nil { - tp := u.Package(pkgPath) - for _, c := range f.file.Doc.List { - tp.DocComments = append(tp.DocComments, c.Text) - } + u.Package(pkgPath).DocComments = splitLines(f.file.Doc.Text()) } } } @@ -386,11 +383,11 @@ func (b *Builder) FindTypes() (types.Universe, error) { if ok { t := b.walkType(u, nil, tn.Type()) c1 := b.priorCommentLines(obj.Pos(), 1) - t.CommentLines = c1.Text() + t.CommentLines = splitLines(c1.Text()) if c1 == nil { - t.SecondClosestCommentLines = b.priorCommentLines(obj.Pos(), 2).Text() + t.SecondClosestCommentLines = splitLines(b.priorCommentLines(obj.Pos(), 2).Text()) } else { - t.SecondClosestCommentLines = b.priorCommentLines(c1.List[0].Slash, 2).Text() + t.SecondClosestCommentLines = splitLines(b.priorCommentLines(c1.List[0].Slash, 2).Text()) } } tf, ok := obj.(*tc.Func) @@ -418,6 +415,10 @@ func (b *Builder) priorCommentLines(pos token.Pos, lines int) *ast.CommentGroup return b.endLineToCommentGroup[key] } +func splitLines(str string) []string { + return strings.Split(strings.TrimRight(str, "\n"), "\n") +} + func tcFuncNameToName(in string) types.Name { name := strings.TrimLeft(in, "func ") nameParts := strings.Split(name, "(") @@ -494,7 +495,7 @@ func (b *Builder) walkType(u types.Universe, useName *types.Name, in tc.Type) *t Embedded: f.Anonymous(), Tags: t.Tag(i), Type: b.walkType(u, nil, f.Type()), - CommentLines: b.priorCommentLines(f.Pos(), 1).Text(), + CommentLines: splitLines(b.priorCommentLines(f.Pos(), 1).Text()), } out.Members = append(out.Members, m) } diff --git a/cmd/libs/go2idl/parser/parse_test.go b/cmd/libs/go2idl/parser/parse_test.go index 50e0f14017b..7ffd198c432 100644 --- a/cmd/libs/go2idl/parser/parse_test.go +++ b/cmd/libs/go2idl/parser/parse_test.go @@ -197,13 +197,13 @@ type Blah struct { if e, a := types.Struct, blahT.Kind; e != a { t.Errorf("struct kind wrong, wanted %v, got %v", e, a) } - if e, a := "Blah is a test.\nA test, I tell you.\n", blahT.CommentLines; e != a { - t.Errorf("struct comment wrong, wanted %v, got %v", e, a) + if e, a := []string{"Blah is a test.", "A test, I tell you."}, blahT.CommentLines; !reflect.DeepEqual(e, a) { + t.Errorf("struct comment wrong, wanted %q, got %q", e, a) } m := types.Member{ Name: "B", Embedded: false, - CommentLines: "B is the second field.\nMultiline comments work.\n", + CommentLines: []string{"B is the second field.", "Multiline comments work."}, Tags: `json:"b"`, Type: types.String, } @@ -216,7 +216,7 @@ func TestParseSecondClosestCommentLines(t *testing.T) { const fileName = "base/foo/proto/foo.go" testCases := []struct { testFile map[string]string - expected string + expected []string }{ { map[string]string{fileName: `package foo @@ -229,7 +229,7 @@ type Blah struct { a int } `}, - "Blah's SecondClosestCommentLines.\nAnother line.\n", + []string{"Blah's SecondClosestCommentLines.", "Another line."}, }, { map[string]string{fileName: `package foo @@ -240,15 +240,15 @@ type Blah struct { a int } `}, - "Blah's SecondClosestCommentLines.\nAnother line.\n", + []string{"Blah's SecondClosestCommentLines.", "Another line."}, }, } for _, test := range testCases { _, u, o := construct(t, test.testFile, namer.NewPublicNamer(0)) t.Logf("%#v", o) blahT := u.Type(types.Name{Package: "base/foo/proto", Name: "Blah"}) - if e, a := test.expected, blahT.SecondClosestCommentLines; e != a { - t.Errorf("struct second closest comment wrong, wanted %v, got %v", e, a) + if e, a := test.expected, blahT.SecondClosestCommentLines; !reflect.DeepEqual(e, a) { + t.Errorf("struct second closest comment wrong, wanted %q, got %q", e, a) } } } diff --git a/cmd/libs/go2idl/types/comments.go b/cmd/libs/go2idl/types/comments.go index 40187ae6228..4be9de6fdbb 100644 --- a/cmd/libs/go2idl/types/comments.go +++ b/cmd/libs/go2idl/types/comments.go @@ -39,8 +39,7 @@ import ( // TODO: Basically we need to define a standard way of giving instructions to // autogenerators in the comments of a type. This is a first iteration of that. // TODO: allow multiple values per key? -func ExtractCommentTags(marker, allLines string) map[string]string { - lines := strings.Split(allLines, "\n") +func ExtractCommentTags(marker string, lines []string) map[string]string { out := map[string]string{} for _, line := range lines { line = strings.Trim(line, " ") diff --git a/cmd/libs/go2idl/types/types.go b/cmd/libs/go2idl/types/types.go index 9e20e937e1e..d34450868c4 100644 --- a/cmd/libs/go2idl/types/types.go +++ b/cmd/libs/go2idl/types/types.go @@ -235,7 +235,7 @@ type Type struct { // If there are comment lines immediately before the type definition, // they will be recorded here. - CommentLines string + CommentLines []string // If there are comment lines preceding the `CommentLines`, they will be // recorded here. There are two cases: @@ -252,7 +252,7 @@ type Type struct { // a blank line // type definition // --- - SecondClosestCommentLines string + SecondClosestCommentLines []string // If Kind == Struct Members []Member @@ -330,7 +330,7 @@ type Member struct { // If there are comment lines immediately before the member in the type // definition, they will be recorded here. - CommentLines string + CommentLines []string // If there are tags along with this member, they will be saved here. Tags string @@ -358,7 +358,7 @@ type Signature struct { // If there are comment lines immediately before this // signature/method/function declaration, they will be recorded here. - CommentLines string + CommentLines []string } // Built in types.