Skip to content

Commit

Permalink
extractNamed parse named.TypeArgs for TypesRecord.toNamedType
Browse files Browse the repository at this point in the history
  • Loading branch information
visualfc committed Sep 7, 2022
1 parent 1eaf1e3 commit da20891
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
9 changes: 9 additions & 0 deletions types_go117.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ func hasTypeParam(t types.Type) bool {
return false
}

func extractNamed(named *types.Named) (pkgpath string, name string) {
obj := named.Obj()
if pkg := obj.Pkg(); pkg != nil {
pkgpath = pkg.Path()
}
name = obj.Name()
return
}

func (sp *sourcePackage) Load() (err error) {
if sp.Info == nil {
sp.Info = &types.Info{
Expand Down
19 changes: 19 additions & 0 deletions types_go118.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,25 @@ func hasTypeParam(t types.Type) bool {
return false
}

func extractNamed(named *types.Named) (pkgpath string, name string) {
obj := named.Obj()
if pkg := obj.Pkg(); pkg != nil {
pkgpath = pkg.Path()
}
name = obj.Name()
if args := named.TypeArgs(); args != nil {
name += "["
for i := 0; i < args.Len(); i++ {
if i != 0 {
name += ","
}
name += args.At(i).String()
}
name += "]"
}
return
}

func (sp *sourcePackage) Load() (err error) {
if sp.Info == nil {
sp.Info = &types.Info{
Expand Down
10 changes: 5 additions & 5 deletions xtypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,9 @@ func (r *TypesRecord) toInterfaceType(t *types.Interface) reflect.Type {

func (r *TypesRecord) toNamedType(t *types.Named) reflect.Type {
ut := t.Underlying()
name := t.Obj()
if name.Pkg() == nil {
if name.Name() == "error" {
pkgpath, name := extractNamed(t)
if pkgpath == "" {
if name == "error" {
return tyErrorInterface
}
return r.ToType(ut)
Expand All @@ -286,7 +286,7 @@ func (r *TypesRecord) toNamedType(t *types.Named) reflect.Type {
numMethods := len(methods)
if numMethods == 0 {
styp := toMockType(t.Underlying())
typ := reflectx.NamedTypeOf(name.Pkg().Path(), name.Name(), styp)
typ := reflectx.NamedTypeOf(pkgpath, name, styp)
r.saveType(t, typ)
utype := r.ToType(ut)
reflectx.SetUnderlying(typ, utype)
Expand All @@ -302,7 +302,7 @@ func (r *TypesRecord) toNamedType(t *types.Named) reflect.Type {
}
// toMockType for size/align
etyp := toMockType(ut)
styp := reflectx.NamedTypeOf(name.Pkg().Path(), name.Name(), etyp)
styp := reflectx.NamedTypeOf(pkgpath, name, etyp)
typ := reflectx.NewMethodSet(styp, mcount, pcount)
r.saveType(t, typ)
utype := r.ToType(ut)
Expand Down

0 comments on commit da20891

Please sign in to comment.