mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-29 06:27:05 +00:00
go2idl: Consistently handle comments as []string
This makes subsequent comment-tag PRs more consistent.
This commit is contained in:
parent
57c3196914
commit
b01ac4726f
@ -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{}{
|
||||
|
@ -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{}{
|
||||
|
@ -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
|
||||
|
@ -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 {
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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, " ")
|
||||
|
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user