@@ -69,7 +69,6 @@ func (f *Func) IsLiteral() bool {
69
69
// be of type *ast.FuncDecl or *ast.FuncLit
70
70
func NewFuncSignature (node ast.Node ) * FuncSignature {
71
71
getParams := func (list []* ast.Field ) string {
72
- var named bool
73
72
buf := new (bytes.Buffer )
74
73
for i , p := range list {
75
74
for j , n := range p .Names {
@@ -80,82 +79,74 @@ func NewFuncSignature(node ast.Node) *FuncSignature {
80
79
}
81
80
82
81
if len (p .Names ) != 0 {
83
- named = true
84
82
buf .WriteString (" " )
85
83
}
86
84
87
85
types .WriteExpr (buf , p .Type )
88
86
89
87
if len (list ) != i + 1 {
90
- named = true
91
88
buf .WriteString (", " )
92
89
}
93
90
}
94
- if named {
95
- return fmt .Sprintf ("(%s)" , buf .String ())
96
- }
97
91
return buf .String ()
98
92
}
93
+ isResultsNeedParens := func (list []* ast.Field ) bool {
94
+ if len (list ) > 1 {
95
+ return true
96
+ }
97
+ return len (list ) != 0 && len (list [0 ].Names ) != 0
98
+ }
99
99
100
100
switch x := node .(type ) {
101
101
case * ast.FuncDecl :
102
102
sig := & FuncSignature {
103
103
Name : x .Name .Name ,
104
104
}
105
105
106
- if x .Type .Params != nil {
107
- sig .In = getParams (x .Type .Params .List )
108
- }
109
- if x .Type .Results != nil {
110
- sig .Out = getParams (x .Type .Results .List )
111
- }
106
+ buf := bytes .NewBufferString ("func " )
107
+
112
108
if x .Recv != nil {
113
109
sig .Recv = getParams (x .Recv .List )
110
+ fmt .Fprintf (buf , "(%s) " , sig .Recv )
114
111
}
115
112
116
- full := "func "
117
-
118
- if sig .Recv != "" {
119
- full += fmt .Sprintf ("%s " , sig .Recv )
120
- }
121
-
122
- full += fmt .Sprintf ("%s" , sig .Name )
113
+ fmt .Fprintf (buf , "%s" , sig .Name )
123
114
124
- if sig .In != "" {
125
- full += fmt .Sprintf ("%s" , sig .In )
126
- } else {
127
- full += "()"
115
+ if x .Type .Params != nil {
116
+ sig .In = getParams (x .Type .Params .List )
117
+ fmt .Fprintf (buf , "(%s)" , sig .In )
128
118
}
129
119
130
- if sig .Out != "" {
131
- full += fmt .Sprintf (" %s" , sig .Out )
120
+ if x .Type .Results != nil {
121
+ sig .Out = getParams (x .Type .Results .List )
122
+ if isResultsNeedParens (x .Type .Results .List ) {
123
+ fmt .Fprintf (buf , " (%s)" , sig .Out )
124
+ } else {
125
+ fmt .Fprintf (buf , " %s" , sig .Out )
126
+ }
132
127
}
133
128
134
- sig .Full = full
129
+ sig .Full = buf . String ()
135
130
return sig
136
131
case * ast.FuncLit :
137
132
sig := & FuncSignature {}
138
133
134
+ buf := bytes .NewBufferString ("func" )
135
+
139
136
if x .Type .Params != nil {
140
137
sig .In = getParams (x .Type .Params .List )
138
+ fmt .Fprintf (buf , "(%s)" , sig .In )
141
139
}
142
140
if x .Type .Results != nil {
143
141
sig .Out = getParams (x .Type .Results .List )
142
+ if isResultsNeedParens (x .Type .Results .List ) {
143
+ fmt .Fprintf (buf , " (%s)" , sig .Out )
144
+ } else {
145
+ fmt .Fprintf (buf , " %s" , sig .Out )
146
+ }
144
147
}
145
148
146
- full := "func"
147
-
148
- if sig .In != "" {
149
- full += fmt .Sprintf ("%s" , sig .In )
150
- } else {
151
- full += "()"
152
- }
153
-
154
- if sig .Out != "" {
155
- full += fmt .Sprintf (" %s" , sig .Out )
156
- }
157
-
158
- sig .Full = full
149
+ sig .Full = buf .String ()
159
150
return sig
160
151
default :
161
152
return & FuncSignature {Full : "UNKNOWN" }
0 commit comments