Skip to content

Commit 822eba1

Browse files
committed
fix: generateFuncTypeName ignores field names for stable WithArgType/WithResultType matching
1 parent 29d9290 commit 822eba1

2 files changed

Lines changed: 107 additions & 22 deletions

File tree

plugins/core/instrument/enhance.go

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -94,41 +94,55 @@ func generateTypeNameByExp(exp dst.Expr) string {
9494
func generateFuncTypeName(n *dst.FuncType) string {
9595
result := "func("
9696
if n.Params != nil {
97-
for i, field := range n.Params.List {
98-
if i > 0 {
99-
result += ", "
97+
first := true
98+
for _, field := range n.Params.List {
99+
count := len(field.Names)
100+
if count == 0 {
101+
count = 1
100102
}
101-
if len(field.Names) > 0 {
102-
for j, name := range field.Names {
103-
if j > 0 {
104-
result += ", "
105-
}
106-
result += name.Name + " " + generateTypeNameByExp(field.Type)
103+
for k := 0; k < count; k++ {
104+
if !first {
105+
result += ", "
107106
}
108-
} else {
109107
result += generateTypeNameByExp(field.Type)
108+
first = false
110109
}
111110
}
112111
}
113112
result += ")"
114113
if n.Results != nil && len(n.Results.List) > 0 {
115-
if len(n.Results.List) == 1 && len(n.Results.List[0].Names) == 0 {
116-
result += " " + generateTypeNameByExp(n.Results.List[0].Type)
114+
totalResults := 0
115+
for _, field := range n.Results.List {
116+
if len(field.Names) == 0 {
117+
totalResults++
118+
} else {
119+
totalResults += len(field.Names)
120+
}
121+
}
122+
if totalResults == 1 {
123+
for _, field := range n.Results.List {
124+
count := len(field.Names)
125+
if count == 0 {
126+
count = 1
127+
}
128+
for k := 0; k < count; k++ {
129+
result += " " + generateTypeNameByExp(field.Type)
130+
}
131+
}
117132
} else {
118133
result += " ("
119-
for i, field := range n.Results.List {
120-
if i > 0 {
121-
result += ", "
134+
first := true
135+
for _, field := range n.Results.List {
136+
count := len(field.Names)
137+
if count == 0 {
138+
count = 1
122139
}
123-
if len(field.Names) > 0 {
124-
for j, name := range field.Names {
125-
if j > 0 {
126-
result += ", "
127-
}
128-
result += name.Name + " " + generateTypeNameByExp(field.Type)
140+
for k := 0; k < count; k++ {
141+
if !first {
142+
result += ", "
129143
}
130-
} else {
131144
result += generateTypeNameByExp(field.Type)
145+
first = false
132146
}
133147
}
134148
result += ")"

plugins/core/instrument/enhance_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,77 @@ func TestGenerateTypeNameByExp_FuncType(t *testing.T) {
113113
},
114114
"func(context.Context, *primitive.SendResult, error)",
115115
},
116+
{
117+
"func with named params ignores names",
118+
&dst.FuncType{
119+
Params: &dst.FieldList{List: []*dst.Field{
120+
{Names: []*dst.Ident{dst.NewIdent("ctx")}, Type: &dst.SelectorExpr{X: dst.NewIdent("context"), Sel: dst.NewIdent("Context")}},
121+
{Names: []*dst.Ident{dst.NewIdent("err")}, Type: dst.NewIdent("error")},
122+
}},
123+
},
124+
"func(context.Context, error)",
125+
},
126+
{
127+
"func with multi-name field expands types",
128+
&dst.FuncType{
129+
Params: &dst.FieldList{List: []*dst.Field{
130+
{Names: []*dst.Ident{dst.NewIdent("a"), dst.NewIdent("b")}, Type: dst.NewIdent("int")},
131+
{Names: []*dst.Ident{dst.NewIdent("s")}, Type: dst.NewIdent("string")},
132+
}},
133+
},
134+
"func(int, int, string)",
135+
},
136+
{
137+
"func with named single result ignores name",
138+
&dst.FuncType{
139+
Params: &dst.FieldList{List: []*dst.Field{
140+
{Type: dst.NewIdent("int")},
141+
}},
142+
Results: &dst.FieldList{List: []*dst.Field{
143+
{Names: []*dst.Ident{dst.NewIdent("err")}, Type: dst.NewIdent("error")},
144+
}},
145+
},
146+
"func(int) error",
147+
},
148+
{
149+
"func with named multiple results ignores names",
150+
&dst.FuncType{
151+
Params: &dst.FieldList{List: []*dst.Field{
152+
{Type: dst.NewIdent("int")},
153+
}},
154+
Results: &dst.FieldList{List: []*dst.Field{
155+
{Names: []*dst.Ident{dst.NewIdent("n")}, Type: dst.NewIdent("int")},
156+
{Names: []*dst.Ident{dst.NewIdent("err")}, Type: dst.NewIdent("error")},
157+
}},
158+
},
159+
"func(int) (int, error)",
160+
},
161+
{
162+
"func with multi-name result field expands types",
163+
&dst.FuncType{
164+
Params: &dst.FieldList{List: []*dst.Field{
165+
{Type: dst.NewIdent("string")},
166+
}},
167+
Results: &dst.FieldList{List: []*dst.Field{
168+
{Names: []*dst.Ident{dst.NewIdent("x"), dst.NewIdent("y")}, Type: dst.NewIdent("int")},
169+
}},
170+
},
171+
"func(string) (int, int)",
172+
},
173+
{
174+
"func with named params and results from real code",
175+
&dst.FuncType{
176+
Params: &dst.FieldList{List: []*dst.Field{
177+
{Names: []*dst.Ident{dst.NewIdent("ctx")}, Type: &dst.SelectorExpr{X: dst.NewIdent("context"), Sel: dst.NewIdent("Context")}},
178+
{Names: []*dst.Ident{dst.NewIdent("req")}, Type: &dst.StarExpr{X: &dst.SelectorExpr{X: dst.NewIdent("http"), Sel: dst.NewIdent("Request")}}},
179+
}},
180+
Results: &dst.FieldList{List: []*dst.Field{
181+
{Names: []*dst.Ident{dst.NewIdent("resp")}, Type: &dst.StarExpr{X: &dst.SelectorExpr{X: dst.NewIdent("http"), Sel: dst.NewIdent("Response")}}},
182+
{Names: []*dst.Ident{dst.NewIdent("err")}, Type: dst.NewIdent("error")},
183+
}},
184+
},
185+
"func(context.Context, *http.Request) (*http.Response, error)",
186+
},
116187
}
117188
for _, tt := range tests {
118189
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)