Skip to content

Commit 3c33efd

Browse files
authored
Merge pull request #6 from rjeczalik/redis-mock
interfaces: add func member support
2 parents 7521fc0 + c7e9451 commit 3c33efd

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

interface.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,11 @@ func buildInterfaceForPkg(pkg *loader.PackageInfo, opts *Options) (Interface, er
138138
}
139139
for i := range fn.Ins {
140140
fn.Ins[i] = newType(ins.At(i))
141+
fixup(&fn.Ins[i], opts.Query)
141142
}
142143
for i := range fn.Outs {
143144
fn.Outs[i] = newType(outs.At(i))
145+
fixup(&fn.Outs[i], opts.Query)
144146
}
145147
inter = append(inter, fn)
146148
}

type.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package interfaces
33
import (
44
"fmt"
55
"go/types"
6+
"path"
7+
"strings"
68
)
79

810
// Type is a simple representation of a single parameter type.
@@ -12,6 +14,7 @@ type Type struct {
1214
ImportPath string `json:"importPath,omitempty"` // import path of the package
1315
IsPointer bool `json:"isPointer,omitempty"` // whether the parameter is a pointer
1416
IsComposite bool `json:"isComposite,omitempty"` // whether the type is map, slice, chan or array
17+
IsFunc bool `json:"isFunc,omitempty"` // whether the type if function
1518
}
1619

1720
// String gives Go code representation of the type.
@@ -51,6 +54,9 @@ func (typ *Type) setFromType(t types.Type, depth int, orig types.Type) {
5154
typ.setFromStruct(t)
5255
case *types.Named:
5356
typ.setFromNamed(t)
57+
case *types.Signature:
58+
typ.IsFunc = true
59+
typ.setFromSignature(t)
5460
case *types.Pointer:
5561
if depth == 0 {
5662
typ.IsPointer = true
@@ -84,6 +90,12 @@ func (typ *Type) setFromStruct(t *types.Struct) {
8490
}
8591
}
8692

93+
func (typ *Type) setFromSignature(t *types.Signature) {
94+
if typ.Name == "" {
95+
typ.Name = t.String()
96+
}
97+
}
98+
8799
func (typ *Type) setFromNamed(t *types.Named) {
88100
if typ.Name == "" {
89101
typ.Name = t.Obj().Name()
@@ -104,3 +116,16 @@ func (typ *Type) setFromComposite(t compositeType, depth int, orig types.Type) {
104116
}
105117
typ.setFromType(t.Elem(), depth+1, orig)
106118
}
119+
120+
func fixup(typ *Type, q *Query) {
121+
// Hacky fixup for renaming:
122+
//
123+
// GeoAdd(string, []*github.com/go-redis/redis.GeoLocation) *redis.IntCmd
124+
//
125+
// to:
126+
//
127+
// GeoAdd(string, []*redis.GeoLocation) *redis.IntCmd
128+
//
129+
// Should be fixed layer below, in type.go.
130+
typ.Name = strings.Replace(typ.Name, q.Package, path.Base(q.Package), -1)
131+
}

0 commit comments

Comments
 (0)