@@ -4,24 +4,28 @@ import (
44 "bytes"
55 "fmt"
66 "sort"
7+ "strings"
78)
89
910// Func represents an interface function.
1011type Func struct {
11- Name string `json:"name,omitempty"` // name of the function
12- Ins []Type `json:"ins,omitempty"` // input parameters
13- Outs []Type `json:"outs,omitempty"` // output parameters
12+ Name string `json:"name,omitempty"` // name of the function
13+ Ins []Type `json:"ins,omitempty"` // input parameters
14+ Outs []Type `json:"outs,omitempty"` // output parameters
15+ IsVariadic bool // whether the function is variadic
1416}
1517
18+ var variadic = strings .NewReplacer ("[]" , "..." )
19+
1620// String gives Go code representation of the function.
1721func (f Func ) String () string {
1822 var buf bytes.Buffer
1923 if len (f .Ins ) == 0 {
2024 fmt .Fprintf (& buf , "%s()" , f .Name )
2125 } else {
22- fmt .Fprintf (& buf , "%s(%s" , f .Name , f .Ins [ 0 ] )
23- for _ , typ := range f .Ins [1 :] {
24- fmt .Fprintf (& buf , ", %s" , typ )
26+ fmt .Fprintf (& buf , "%s(%s" , f .Name , f .in ( 0 ) )
27+ for i := range f .Ins [1 :] {
28+ fmt .Fprintf (& buf , ", %s" , f . in ( i + 1 ) )
2529 }
2630 buf .WriteString (")" )
2731 }
@@ -37,6 +41,14 @@ func (f Func) String() string {
3741 return buf .String ()
3842}
3943
44+ func (f Func ) in (i int ) string {
45+ if typ := f .Ins [i ]; i == len (f .Ins )- 1 && f .IsVariadic {
46+ return variadic .Replace (typ .String ())
47+ } else {
48+ return typ .String ()
49+ }
50+ }
51+
4052// Deps gives a list of packages the function depends on. E.g. if the function
4153// represents Serve(net.Listener, http.Handler) error, calling Deps() will
4254// return []string{"http", "net"}.
0 commit comments