mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-04 01:40:07 +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,12 +95,10 @@ 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.
|
// 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 {
|
p := c.Universe.Package(g.inputPackage)
|
||||||
comment = strings.TrimLeft(comment, "//")
|
if override, ok := types.ExtractCommentTags("+", p.DocComments)["groupName"]; ok {
|
||||||
if override, ok := types.ExtractCommentTags("+", comment)["groupName"]; ok {
|
|
||||||
groupName = override
|
groupName = override
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
m := map[string]interface{}{
|
m := map[string]interface{}{
|
||||||
"type": t,
|
"type": t,
|
||||||
|
@ -18,7 +18,6 @@ package generators
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"k8s.io/kubernetes/cmd/libs/go2idl/client-gen/generators/normalization"
|
"k8s.io/kubernetes/cmd/libs/go2idl/client-gen/generators/normalization"
|
||||||
"k8s.io/kubernetes/cmd/libs/go2idl/generator"
|
"k8s.io/kubernetes/cmd/libs/go2idl/generator"
|
||||||
@ -74,12 +73,10 @@ func (g *genGroup) GenerateType(c *generator.Context, t *types.Type, w io.Writer
|
|||||||
groupName = ""
|
groupName = ""
|
||||||
}
|
}
|
||||||
// allow user to define a group name that's different from the one parsed from the directory.
|
// 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 {
|
p := c.Universe.Package(g.inputPacakge)
|
||||||
comment = strings.TrimLeft(comment, "//")
|
if override, ok := types.ExtractCommentTags("+", p.DocComments)["groupName"]; ok && override != "" {
|
||||||
if override, ok := types.ExtractCommentTags("+", comment)["groupName"]; ok && override != "" {
|
|
||||||
groupName = override
|
groupName = override
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
m := map[string]interface{}{
|
m := map[string]interface{}{
|
||||||
"group": normalization.BeforeFirstDot(g.group),
|
"group": normalization.BeforeFirstDot(g.group),
|
||||||
|
@ -232,12 +232,9 @@ func Packages(context *generator.Context, arguments *args.GeneratorArgs) generat
|
|||||||
// Only generate conversions for package which explicitly requested it
|
// Only generate conversions for package which explicitly requested it
|
||||||
// byt setting "+genversion=true" in their doc.go file.
|
// byt setting "+genversion=true" in their doc.go file.
|
||||||
filtered := false
|
filtered := false
|
||||||
for _, comment := range p.DocComments {
|
if types.ExtractCommentTags("+", p.DocComments)["genconversion"] == "true" {
|
||||||
comment := strings.Trim(comment, "//")
|
|
||||||
if types.ExtractCommentTags("+", comment)["genconversion"] == "true" {
|
|
||||||
filtered = true
|
filtered = true
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if !filtered {
|
if !filtered {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -268,7 +268,7 @@ func (b bodyGen) doAlias(sw *generator.SnippetWriter) error {
|
|||||||
Members: []types.Member{
|
Members: []types.Member{
|
||||||
{
|
{
|
||||||
Name: "Items",
|
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,
|
Type: b.t.Underlying,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -410,7 +410,7 @@ type protoField struct {
|
|||||||
Nullable bool
|
Nullable bool
|
||||||
Extras map[string]string
|
Extras map[string]string
|
||||||
|
|
||||||
CommentLines string
|
CommentLines []string
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -687,8 +687,7 @@ func membersToFields(locator ProtobufLocator, t *types.Type, localPackage types.
|
|||||||
return fields, nil
|
return fields, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func genComment(out io.Writer, comment, indent string) {
|
func genComment(out io.Writer, lines []string, indent string) {
|
||||||
lines := strings.Split(comment, "\n")
|
|
||||||
for {
|
for {
|
||||||
l := len(lines)
|
l := len(lines)
|
||||||
if l == 0 || len(lines[l-1]) != 0 {
|
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] {
|
for _, f := range b.parsed[pkgPath] {
|
||||||
if strings.HasSuffix(f.name, "/doc.go") {
|
if strings.HasSuffix(f.name, "/doc.go") {
|
||||||
if f.file.Doc != nil {
|
if f.file.Doc != nil {
|
||||||
tp := u.Package(pkgPath)
|
u.Package(pkgPath).DocComments = splitLines(f.file.Doc.Text())
|
||||||
for _, c := range f.file.Doc.List {
|
|
||||||
tp.DocComments = append(tp.DocComments, c.Text)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -386,11 +383,11 @@ func (b *Builder) FindTypes() (types.Universe, error) {
|
|||||||
if ok {
|
if ok {
|
||||||
t := b.walkType(u, nil, tn.Type())
|
t := b.walkType(u, nil, tn.Type())
|
||||||
c1 := b.priorCommentLines(obj.Pos(), 1)
|
c1 := b.priorCommentLines(obj.Pos(), 1)
|
||||||
t.CommentLines = c1.Text()
|
t.CommentLines = splitLines(c1.Text())
|
||||||
if c1 == nil {
|
if c1 == nil {
|
||||||
t.SecondClosestCommentLines = b.priorCommentLines(obj.Pos(), 2).Text()
|
t.SecondClosestCommentLines = splitLines(b.priorCommentLines(obj.Pos(), 2).Text())
|
||||||
} else {
|
} 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)
|
tf, ok := obj.(*tc.Func)
|
||||||
@ -418,6 +415,10 @@ func (b *Builder) priorCommentLines(pos token.Pos, lines int) *ast.CommentGroup
|
|||||||
return b.endLineToCommentGroup[key]
|
return b.endLineToCommentGroup[key]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func splitLines(str string) []string {
|
||||||
|
return strings.Split(strings.TrimRight(str, "\n"), "\n")
|
||||||
|
}
|
||||||
|
|
||||||
func tcFuncNameToName(in string) types.Name {
|
func tcFuncNameToName(in string) types.Name {
|
||||||
name := strings.TrimLeft(in, "func ")
|
name := strings.TrimLeft(in, "func ")
|
||||||
nameParts := strings.Split(name, "(")
|
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(),
|
Embedded: f.Anonymous(),
|
||||||
Tags: t.Tag(i),
|
Tags: t.Tag(i),
|
||||||
Type: b.walkType(u, nil, f.Type()),
|
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)
|
out.Members = append(out.Members, m)
|
||||||
}
|
}
|
||||||
|
@ -197,13 +197,13 @@ type Blah struct {
|
|||||||
if e, a := types.Struct, blahT.Kind; e != a {
|
if e, a := types.Struct, blahT.Kind; e != a {
|
||||||
t.Errorf("struct kind wrong, wanted %v, got %v", 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 {
|
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 %v, got %v", e, a)
|
t.Errorf("struct comment wrong, wanted %q, got %q", e, a)
|
||||||
}
|
}
|
||||||
m := types.Member{
|
m := types.Member{
|
||||||
Name: "B",
|
Name: "B",
|
||||||
Embedded: false,
|
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"`,
|
Tags: `json:"b"`,
|
||||||
Type: types.String,
|
Type: types.String,
|
||||||
}
|
}
|
||||||
@ -216,7 +216,7 @@ func TestParseSecondClosestCommentLines(t *testing.T) {
|
|||||||
const fileName = "base/foo/proto/foo.go"
|
const fileName = "base/foo/proto/foo.go"
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
testFile map[string]string
|
testFile map[string]string
|
||||||
expected string
|
expected []string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
map[string]string{fileName: `package foo
|
map[string]string{fileName: `package foo
|
||||||
@ -229,7 +229,7 @@ type Blah struct {
|
|||||||
a int
|
a int
|
||||||
}
|
}
|
||||||
`},
|
`},
|
||||||
"Blah's SecondClosestCommentLines.\nAnother line.\n",
|
[]string{"Blah's SecondClosestCommentLines.", "Another line."},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
map[string]string{fileName: `package foo
|
map[string]string{fileName: `package foo
|
||||||
@ -240,15 +240,15 @@ type Blah struct {
|
|||||||
a int
|
a int
|
||||||
}
|
}
|
||||||
`},
|
`},
|
||||||
"Blah's SecondClosestCommentLines.\nAnother line.\n",
|
[]string{"Blah's SecondClosestCommentLines.", "Another line."},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, test := range testCases {
|
for _, test := range testCases {
|
||||||
_, u, o := construct(t, test.testFile, namer.NewPublicNamer(0))
|
_, u, o := construct(t, test.testFile, namer.NewPublicNamer(0))
|
||||||
t.Logf("%#v", o)
|
t.Logf("%#v", o)
|
||||||
blahT := u.Type(types.Name{Package: "base/foo/proto", Name: "Blah"})
|
blahT := u.Type(types.Name{Package: "base/foo/proto", Name: "Blah"})
|
||||||
if e, a := test.expected, blahT.SecondClosestCommentLines; e != a {
|
if e, a := test.expected, blahT.SecondClosestCommentLines; !reflect.DeepEqual(e, a) {
|
||||||
t.Errorf("struct second closest comment wrong, wanted %v, got %v", 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
|
// 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.
|
// autogenerators in the comments of a type. This is a first iteration of that.
|
||||||
// TODO: allow multiple values per key?
|
// TODO: allow multiple values per key?
|
||||||
func ExtractCommentTags(marker, allLines string) map[string]string {
|
func ExtractCommentTags(marker string, lines []string) map[string]string {
|
||||||
lines := strings.Split(allLines, "\n")
|
|
||||||
out := map[string]string{}
|
out := map[string]string{}
|
||||||
for _, line := range lines {
|
for _, line := range lines {
|
||||||
line = strings.Trim(line, " ")
|
line = strings.Trim(line, " ")
|
||||||
|
@ -235,7 +235,7 @@ type Type struct {
|
|||||||
|
|
||||||
// If there are comment lines immediately before the type definition,
|
// If there are comment lines immediately before the type definition,
|
||||||
// they will be recorded here.
|
// they will be recorded here.
|
||||||
CommentLines string
|
CommentLines []string
|
||||||
|
|
||||||
// If there are comment lines preceding the `CommentLines`, they will be
|
// If there are comment lines preceding the `CommentLines`, they will be
|
||||||
// recorded here. There are two cases:
|
// recorded here. There are two cases:
|
||||||
@ -252,7 +252,7 @@ type Type struct {
|
|||||||
// a blank line
|
// a blank line
|
||||||
// type definition
|
// type definition
|
||||||
// ---
|
// ---
|
||||||
SecondClosestCommentLines string
|
SecondClosestCommentLines []string
|
||||||
|
|
||||||
// If Kind == Struct
|
// If Kind == Struct
|
||||||
Members []Member
|
Members []Member
|
||||||
@ -330,7 +330,7 @@ type Member struct {
|
|||||||
|
|
||||||
// If there are comment lines immediately before the member in the type
|
// If there are comment lines immediately before the member in the type
|
||||||
// definition, they will be recorded here.
|
// definition, they will be recorded here.
|
||||||
CommentLines string
|
CommentLines []string
|
||||||
|
|
||||||
// If there are tags along with this member, they will be saved here.
|
// If there are tags along with this member, they will be saved here.
|
||||||
Tags string
|
Tags string
|
||||||
@ -358,7 +358,7 @@ type Signature struct {
|
|||||||
|
|
||||||
// If there are comment lines immediately before this
|
// If there are comment lines immediately before this
|
||||||
// signature/method/function declaration, they will be recorded here.
|
// signature/method/function declaration, they will be recorded here.
|
||||||
CommentLines string
|
CommentLines []string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Built in types.
|
// Built in types.
|
||||||
|
Loading…
Reference in New Issue
Block a user