Skip to content

Commit

Permalink
interfaces: fixup import paths as type definitions
Browse files Browse the repository at this point in the history
This should be fixed The Right Way (tm) some other time.
  • Loading branch information
rjeczalik committed Jun 14, 2017
1 parent 4eb1478 commit c7e9451
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,11 @@ func buildInterfaceForPkg(pkg *loader.PackageInfo, opts *Options) (Interface, er
}
for i := range fn.Ins {
fn.Ins[i] = newType(ins.At(i))
fixup(&fn.Ins[i], opts.Query)
}
for i := range fn.Outs {
fn.Outs[i] = newType(outs.At(i))
fixup(&fn.Outs[i], opts.Query)
}
inter = append(inter, fn)
}
Expand Down
17 changes: 17 additions & 0 deletions type.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package interfaces
import (
"fmt"
"go/types"
"path"
"strings"
)

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

// String gives Go code representation of the type.
Expand Down Expand Up @@ -52,6 +55,7 @@ func (typ *Type) setFromType(t types.Type, depth int, orig types.Type) {
case *types.Named:
typ.setFromNamed(t)
case *types.Signature:
typ.IsFunc = true
typ.setFromSignature(t)
case *types.Pointer:
if depth == 0 {
Expand Down Expand Up @@ -112,3 +116,16 @@ func (typ *Type) setFromComposite(t compositeType, depth int, orig types.Type) {
}
typ.setFromType(t.Elem(), depth+1, orig)
}

func fixup(typ *Type, q *Query) {
// Hacky fixup for renaming:
//
// GeoAdd(string, []*github.com/go-redis/redis.GeoLocation) *redis.IntCmd
//
// to:
//
// GeoAdd(string, []*redis.GeoLocation) *redis.IntCmd
//
// Should be fixed layer below, in type.go.
typ.Name = strings.Replace(typ.Name, q.Package, path.Base(q.Package), -1)
}

0 comments on commit c7e9451

Please # to comment.