Skip to content

Commit 5cb50a7

Browse files
committed
fix: missing pkgpath when collect sub struct (cloudwego#24)
1 parent 86c94d7 commit 5cb50a7

3 files changed

Lines changed: 38 additions & 3 deletions

File tree

src/compress/golang/plugin/file.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ func (i Identity) String() string {
4040

4141
// return packagename.name
4242
func (i Identity) CallName() string {
43-
return filepath.Base(i.PkgPath) + "." + i.Name
43+
if i.PkgPath != "" {
44+
return filepath.Base(i.PkgPath) + "." + i.Name
45+
}
46+
return i.Name
4447
}
4548

4649
// Function holds the information about a function
@@ -423,7 +426,7 @@ func (p *goParser) collectTypes(ctx *fileContext, field string, typ ast.Expr, st
423426
}
424427
st.Methods[field] = Identity{ctx.pkgPath, mname}
425428
} else {
426-
var impt string
429+
impt := ctx.pkgPath
427430
if ty.PkgPath != "" {
428431
if _, ok := ctx.sysImports[ty.PkgPath]; ok {
429432
continue

src/compress/golang/plugin/go_ast_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func Test_goParser_ParseRepo(t *testing.T) {
6868
{
6969
name: "test",
7070
fields: fields{
71-
homePageDir: "/Users/admin/GOPATH/work/hertz",
71+
homePageDir: "../../../../testdata/golang",
7272
},
7373
},
7474
}
@@ -80,6 +80,8 @@ func Test_goParser_ParseRepo(t *testing.T) {
8080
t.Fatalf("goParser.ParseTilTheEnd() error = %v", err)
8181
}
8282
spew.Dump(p)
83+
x := p.repo.GetType(Identity{"a.b/c/pkg/entity", "MyStruct"})
84+
spew.Dump(x.InlineStruct, x.SubStruct)
8385
out, fun := p.getMain(2)
8486
if fun.Name != "main" {
8587
t.Fail()
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package entity
2+
3+
type MyStructC struct {
4+
}
5+
6+
type MyStructD struct {
7+
}
8+
9+
type MyStruct struct {
10+
a string
11+
b string
12+
c MyStructC
13+
MyStructD
14+
}
15+
16+
func (a MyStruct) String() string {
17+
return "base struct"
18+
}
19+
20+
func (c MyStructC) String() string {
21+
return "I'm struct c"
22+
}
23+
24+
func (c MyStructD) String() string {
25+
return "I'm struct d"
26+
}
27+
28+
func A() {
29+
return
30+
}

0 commit comments

Comments
 (0)