Skip to content

Commit

Permalink
review changes:
Browse files Browse the repository at this point in the history
* fixed type conversion for support go1.22
* simplified loops by using range
* removed unused method "generateSalt"
* renamed method of test interface: GiveBirth -> AddHumans
  • Loading branch information
tulzke committed Sep 26, 2024
1 parent 08f7db9 commit 5b309e7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 55 deletions.
67 changes: 26 additions & 41 deletions mockgen/import_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"go/types"
"math/rand"

"go.uber.org/mock/mockgen/model"
"golang.org/x/tools/go/packages"
Expand Down Expand Up @@ -59,17 +58,6 @@ func (p *importModeParser) loadPackage(packageName string) (*packages.Package, e
return pkgs[0], nil
}

func (p *importModeParser) generateSalt() string {
var numbers = []byte("0123456789")

result := make([]byte, 8)
for i := range result {
result[i] = numbers[rand.Intn(len(numbers))]
}

return string(result)
}

func (p *importModeParser) extractInterfacesFromPackage(pkg *packages.Package, ifaces []string) ([]*model.Interface, error) {
interfaces := make([]*model.Interface, len(ifaces))
for i, iface := range ifaces {
Expand Down Expand Up @@ -105,7 +93,7 @@ func (p *importModeParser) parseInterface(obj types.Object) (*model.Interface, e
}

methods := make([]*model.Method, iface.NumMethods())
for i := 0; i < iface.NumMethods(); i++ {
for i := range iface.NumMethods() {
method := iface.Method(i)
typedMethod, ok := method.Type().(*types.Signature)
if !ok {
Expand All @@ -125,25 +113,26 @@ func (p *importModeParser) parseInterface(obj types.Object) (*model.Interface, e
}
}

var typeParams []*model.Parameter
if named.TypeParams() != nil {
typeParams = make([]*model.Parameter, named.TypeParams().Len())
for i := 0; i < named.TypeParams().Len(); i++ {
param := named.TypeParams().At(i)
typeParam, err := p.parseConstraint(param)
if err != nil {
return nil, newParseTypeError("parse type parameter", param.String(), err)
}
if named.TypeParams() == nil {
return &model.Interface{Name: obj.Name(), Methods: methods}, nil
}

typeParams[i] = &model.Parameter{Name: param.Obj().Name(), Type: typeParam}
typeParams := make([]*model.Parameter, named.TypeParams().Len())
for i := range named.TypeParams().Len() {
param := named.TypeParams().At(i)
typeParam, err := p.parseConstraint(param)
if err != nil {
return nil, newParseTypeError("parse type parameter", param.String(), err)
}

typeParams[i] = &model.Parameter{Name: param.Obj().Name(), Type: typeParam}
}

return &model.Interface{Name: obj.Name(), Methods: methods, TypeParams: typeParams}, nil
}

func (o *importModeParser) isConstraint(t *types.Interface) bool {
for i := 0; i < t.NumEmbeddeds(); i++ {
for i := range t.NumEmbeddeds() {
embed := t.EmbeddedType(i)
if _, ok := embed.Underlying().(*types.Interface); !ok {
return true
Expand Down Expand Up @@ -191,22 +180,24 @@ func (p *importModeParser) parseType(t types.Type) (model.Type, error) {

return sig, nil
case *types.Named, *types.Alias:
named := t.(namedType)
object := t.(interface{ Obj() *types.TypeName })
var pkg string
if named.Obj().Pkg() != nil {
pkg = named.Obj().Pkg().Path()
if object.Obj().Pkg() != nil {
pkg = object.Obj().Pkg().Path()
}

if named.TypeArgs() == nil {
// TypeArgs method not available for aliases in go1.22
genericType, ok := t.(interface{ TypeArgs() *types.TypeList })
if !ok || genericType.TypeArgs() == nil {
return &model.NamedType{
Package: pkg,
Type: named.Obj().Name(),
Type: object.Obj().Name(),
}, nil
}

typeParams := &model.TypeParametersType{TypeParameters: make([]model.Type, named.TypeArgs().Len())}
for i := 0; i < named.TypeArgs().Len(); i++ {
typeParam := named.TypeArgs().At(i)
typeParams := &model.TypeParametersType{TypeParameters: make([]model.Type, genericType.TypeArgs().Len())}
for i := range genericType.TypeArgs().Len() {
typeParam := genericType.TypeArgs().At(i)
typedParam, err := p.parseType(typeParam)
if err != nil {
return nil, newParseTypeError("parse type parameter", typeParam.String(), err)
Expand All @@ -217,7 +208,7 @@ func (p *importModeParser) parseType(t types.Type) (model.Type, error) {

return &model.NamedType{
Package: pkg,
Type: named.Obj().Name(),
Type: object.Obj().Name(),
TypeParams: typeParams,
}, nil
case *types.Interface:
Expand Down Expand Up @@ -264,7 +255,7 @@ func (p *importModeParser) parseType(t types.Type) (model.Type, error) {
func (p *importModeParser) parseFunc(sig *types.Signature) (*model.FuncType, error) {
var variadic *model.Parameter
params := make([]*model.Parameter, 0, sig.Params().Len())
for i := 0; i < sig.Params().Len(); i++ {
for i := range sig.Params().Len() {
param := sig.Params().At(i)

isVariadicParam := i == sig.Params().Len()-1 && sig.Variadic()
Expand Down Expand Up @@ -297,7 +288,7 @@ func (p *importModeParser) parseFunc(sig *types.Signature) (*model.FuncType, err
}

results := make([]*model.Parameter, sig.Results().Len())
for i := 0; i < sig.Results().Len(); i++ {
for i := range sig.Results().Len() {
result := sig.Results().At(i)

resultType, err := p.parseType(result.Type())
Expand Down Expand Up @@ -353,9 +344,3 @@ func (p parseTypeError) Error() string {
func (p parseTypeError) Unwrap() error {
return p.error
}

// namedType is an interface for combining Named and Alias
type namedType interface {
Obj() *types.TypeName
TypeArgs() *types.TypeList
}
2 changes: 1 addition & 1 deletion mockgen/import_mode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func Test_importModeParser_parsePackage(t *testing.T) {
Name: "Earth",
Methods: []*model.Method{
{
Name: "GiveBirth",
Name: "AddHumans",
In: []*model.Parameter{
{
Type: &model.NamedType{
Expand Down
2 changes: 1 addition & 1 deletion mockgen/internal/tests/import_mode/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ type Counter interface {
type HumansCount = int

type Earth interface {
GiveBirth(HumansCount) []Human
AddHumans(HumansCount) []Human
HumanPopulation() HumansCount
}
24 changes: 12 additions & 12 deletions mockgen/internal/tests/import_mode/mock/interfaces.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5b309e7

Please sign in to comment.