Skip to content

Commit 841d3d1

Browse files
committed
fix: not collect anonymous sub struct
1 parent f3a0c0b commit 841d3d1

5 files changed

Lines changed: 22 additions & 14 deletions

File tree

lang/golang/parser/file.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -471,10 +471,17 @@ func (p *GoParser) parseStruct(ctx *fileContext, struName string, name *ast.Iden
471471
fieldname = fieldDecl.Names[0].Name
472472
}
473473
if stru, ok := fieldDecl.Type.(*ast.StructType); ok {
474-
// anonymous struct. parse and collect it
474+
// anonymous struct. parse it
475475
as, _ := p.parseStruct(ctx, "_"+fieldname, nil, stru)
476-
dep := NewDependency(as.Identity, ctx.FileLine(fieldDecl.Type))
477-
st.SubStruct = append(st.SubStruct, dep)
476+
// move out substructs of the anonymous struct
477+
for _, dep := range as.SubStruct {
478+
st.SubStruct = InsertDependency(st.SubStruct, dep)
479+
}
480+
for _, dep := range as.InlineStruct {
481+
st.SubStruct = InsertDependency(st.InlineStruct, dep)
482+
}
483+
// remove the anonymous struct from the repo
484+
delete(p.repo.GetPackage(as.ModPath, as.PkgPath).Types, as.Name)
478485
} else {
479486
p.collectTypes(ctx, fieldDecl.Type, st, inlined)
480487
}

lang/golang/parser/pkg_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,9 @@ func Test_goParser_ParseDirs(t *testing.T) {
114114
if err != nil {
115115
t.Fatal(err)
116116
}
117-
_ = out
118-
println(string(out))
117+
if err := os.WriteFile("golang.json", out, 0644); err != nil {
118+
t.Fatal(err)
119+
}
119120
})
120121
}
121122
}

testdata/golang/go.mod

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ module a.b/c
22

33
go 1.20
44

5-
require (
6-
github.com/bytedance/sonic v1.10.2
7-
github.com/pkg/errors v0.9.1
8-
)
5+
require github.com/bytedance/sonic v1.10.2
96

107
require (
118
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect

testdata/golang/go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
1414
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
1515
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
1616
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
17-
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
18-
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
1917
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2018
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2119
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

testdata/golang/pkg/entity/entity.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// Copyright 2025 CloudWeGo Authors
2-
//
2+
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
55
// You may obtain a copy of the License at
6-
//
6+
//
77
// https://www.apache.org/licenses/LICENSE-2.0
8-
//
8+
//
99
// Unless required by applicable law or agreed to in writing, software
1010
// distributed under the License is distributed on an "AS IS" BASIS,
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -25,13 +25,18 @@ type MyStruct struct {
2525
b string
2626
c MyStructC
2727
MyStructD
28+
Embed struct {
29+
*MyStruct
30+
}
2831
}
2932

3033
type InterfaceB interface {
3134
String() string
3235
}
3336

3437
func (a MyStruct) String() string {
38+
_ = a.Embed.MyStruct
39+
_ = a.MyStructD
3540
return "base struct"
3641
}
3742

0 commit comments

Comments
 (0)