Skip to content

Commit 78ca620

Browse files
committed
v2/parser: prefer original comment when evaluating aliased Type
Fixes the comment parsing on a Type so that it does not get overwritten by an aliased Type with a different comment. Resolves kubernetes#292 Signed-off-by: Shashank Ram <[email protected]>
1 parent e3bc6f1 commit 78ca620

File tree

4 files changed

+112
-10
lines changed

4 files changed

+112
-10
lines changed

v2/parser/parse.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,12 @@ func (p *Parser) addPkgToUniverse(pkg *packages.Package, u *types.Universe) erro
488488
switch obj := s.Lookup(n).(type) {
489489
case *gotypes.TypeName:
490490
t := p.walkType(*u, nil, obj.Type())
491-
p.addCommentsToType(obj, t)
491+
// If the resolved type belongs to a different package, it implies
492+
// that it is a type alias, in which case we should not update the
493+
// comment lines on the original type with that of the alias
494+
if t.Name.Package == pkgPath {
495+
p.addCommentsToType(obj, t)
496+
}
492497
case *gotypes.Func:
493498
// We only care about functions, not concrete/abstract methods.
494499
if obj.Type() != nil && obj.Type().(*gotypes.Signature).Recv() == nil {

v2/parser/parse_test.go

Lines changed: 88 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -823,12 +823,14 @@ func TestAddOnePkgToUniverse(t *testing.T) {
823823
func TestStructParse(t *testing.T) {
824824
testCases := []struct {
825825
description string
826-
testFile string
826+
testFiles []string
827827
expected func() *types.Type
828828
}{
829829
{
830830
description: "basic comments",
831-
testFile: "k8s.io/gengo/v2/parser/testdata/basic",
831+
testFiles: []string{
832+
"k8s.io/gengo/v2/parser/testdata/basic",
833+
},
832834
expected: func() *types.Type {
833835
return &types.Type{
834836
Name: types.Name{
@@ -860,7 +862,9 @@ func TestStructParse(t *testing.T) {
860862
},
861863
{
862864
description: "generic",
863-
testFile: "./testdata/generic",
865+
testFiles: []string{
866+
"./testdata/generic",
867+
},
864868
expected: func() *types.Type {
865869
return &types.Type{
866870
Name: types.Name{
@@ -897,7 +901,9 @@ func TestStructParse(t *testing.T) {
897901
},
898902
{
899903
description: "generic on field",
900-
testFile: "./testdata/generic-field",
904+
testFiles: []string{
905+
"./testdata/generic-field",
906+
},
901907
expected: func() *types.Type {
902908
fieldType := &types.Type{
903909
Name: types.Name{
@@ -953,7 +959,9 @@ func TestStructParse(t *testing.T) {
953959
},
954960
{
955961
description: "generic multiple",
956-
testFile: "./testdata/generic-multi",
962+
testFiles: []string{
963+
"./testdata/generic-multi",
964+
},
957965
expected: func() *types.Type {
958966
return &types.Type{
959967
Name: types.Name{
@@ -1026,7 +1034,9 @@ func TestStructParse(t *testing.T) {
10261034
},
10271035
{
10281036
description: "generic recursive",
1029-
testFile: "./testdata/generic-recursive",
1037+
testFiles: []string{
1038+
"./testdata/generic-recursive",
1039+
},
10301040
expected: func() *types.Type {
10311041
recursiveT := &types.Type{
10321042
Name: types.Name{
@@ -1095,18 +1105,53 @@ func TestStructParse(t *testing.T) {
10951105
}
10961106
},
10971107
},
1108+
{
1109+
description: "comments on aliased type should not overwrite original type's comments",
1110+
testFiles: []string{
1111+
"k8s.io/gengo/v2/parser/testdata/type-alias/v1",
1112+
"k8s.io/gengo/v2/parser/testdata/type-alias/v2",
1113+
},
1114+
expected: func() *types.Type {
1115+
return &types.Type{
1116+
Name: types.Name{
1117+
Package: "k8s.io/gengo/v2/parser/testdata/type-alias/v1",
1118+
Name: "Blah",
1119+
},
1120+
Kind: types.Struct,
1121+
CommentLines: []string{"Blah is a test.", "A test, I tell you."},
1122+
SecondClosestCommentLines: []string{""},
1123+
Members: []types.Member{
1124+
{
1125+
Name: "A",
1126+
Embedded: false,
1127+
CommentLines: []string{"A is the first field."},
1128+
Tags: `json:"a"`,
1129+
Type: types.Int64,
1130+
},
1131+
{
1132+
Name: "B",
1133+
Embedded: false,
1134+
CommentLines: []string{"B is the second field.", "Multiline comments work."},
1135+
Tags: `json:"b"`,
1136+
Type: types.String,
1137+
},
1138+
},
1139+
TypeParams: map[string]*types.Type{},
1140+
}
1141+
},
1142+
},
10981143
}
10991144

11001145
for _, tc := range testCases {
11011146
t.Run(tc.description, func(t *testing.T) {
11021147
parser := New()
11031148

1104-
pkgs, err := parser.loadPackages(tc.testFile)
1149+
_, err := parser.loadPackages(tc.testFiles...)
11051150
if err != nil {
11061151
t.Errorf("unexpected error: %v", err)
11071152
}
1108-
u := types.Universe{}
1109-
if err := parser.addPkgToUniverse(pkgs[0], &u); err != nil {
1153+
u, err := parser.NewUniverse()
1154+
if err != nil {
11101155
t.Errorf("unexpected error: %v", err)
11111156
}
11121157

@@ -1153,3 +1198,37 @@ func TestGoNameToName(t *testing.T) {
11531198
})
11541199
}
11551200
}
1201+
1202+
func TestCommentsWithAliasedType(t *testing.T) {
1203+
for i := 0; i < 50; i++ {
1204+
parser := NewWithOptions(Options{BuildTags: []string{"ignore_autogenerated"}})
1205+
if _, err := parser.loadPackages("./testdata/type-alias/..."); err != nil {
1206+
t.Fatalf("unexpected error: %v", err)
1207+
}
1208+
1209+
u, err := parser.NewUniverse()
1210+
if err != nil {
1211+
t.Fatalf("unexpected error: %v", err)
1212+
}
1213+
1214+
for name, pkg := range u {
1215+
if name != "k8s.io/gengo/v2/parser/testdata/type-alias/v1" {
1216+
continue
1217+
}
1218+
for _, typ := range pkg.Types {
1219+
if typ.Name.Name != "Blah" {
1220+
continue
1221+
}
1222+
if len(typ.CommentLines) != 2 {
1223+
t.Fatalf("expected 2 comments, got %d", len(typ.CommentLines))
1224+
}
1225+
if typ.CommentLines[0] != "Blah is a test." {
1226+
t.Errorf("expected comment %q, got %q", "Blah is a test.", typ.CommentLines[0])
1227+
}
1228+
if typ.CommentLines[1] != "A test, I tell you." {
1229+
t.Errorf("expected comment %q, got %q", "A test, I tell you.", typ.CommentLines[1])
1230+
}
1231+
}
1232+
}
1233+
}
1234+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package v1
2+
3+
// Blah is a test.
4+
// A test, I tell you.
5+
type Blah struct {
6+
// A is the first field.
7+
A int64 `json:"a"`
8+
9+
// B is the second field.
10+
// Multiline comments work.
11+
B string `json:"b"`
12+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package v2
2+
3+
import v1 "k8s.io/gengo/v2/parser/testdata/type-alias/v1"
4+
5+
// This is an alias for v1.Blah.
6+
type Blah = v1.Blah

0 commit comments

Comments
 (0)