Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: ignore interfaces that contains type constraints #93

Merged
merged 4 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions mockgen/generic_go118.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
package main

import (
"errors"
"fmt"
"go/ast"
"go/token"
"strings"

"go.uber.org/mock/mockgen/model"
Expand Down Expand Up @@ -98,6 +100,16 @@ func (p *fileParser) parseGenericMethod(field *ast.Field, it *namedInterface, if
case *ast.IndexListExpr:
indices = v.Indices
typ = v.X
case *ast.UnaryExpr:
if v.Op == token.TILDE {
return nil, errIgnore
}
return nil, fmt.Errorf("~T may only appear as constraint for %T", field.Type)
case *ast.BinaryExpr:
if v.Op == token.OR {
return nil, errIgnore
}
return nil, fmt.Errorf("A|B may only appear as constraint for %T", field.Type)
default:
return nil, fmt.Errorf("don't know how to mock method of type %T", field.Type)
}
Expand All @@ -114,3 +126,5 @@ func (p *fileParser) parseGenericMethod(field *ast.Field, it *namedInterface, if

return p.parseMethod(nf, it, iface, pkg, tps)
}

var errIgnore = errors.New("parser decided to ignore the interface for constraints")
hoonmin marked this conversation as resolved.
Show resolved Hide resolved
8 changes: 8 additions & 0 deletions mockgen/internal/tests/generics/generics.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,11 @@ type SolarSystem[T constraints.Ordered] interface {
type Earth[R any] interface {
Water(R) []R
}

type Water[R any, C UnsignedInteger] interface {
Fish(R) []C
}

type UnsignedInteger interface {
~uint | ~uint32 | ~uint64
}
66 changes: 42 additions & 24 deletions mockgen/internal/tests/generics/source/mock_generics_mock.go

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

3 changes: 3 additions & 0 deletions mockgen/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ func (p *fileParser) parseFile(importPath string, file *ast.File) (*model.Packag
continue
}
i, err := p.parseInterface(ni.name.String(), importPath, ni)
if err == errIgnore {
hoonmin marked this conversation as resolved.
Show resolved Hide resolved
continue
}
if err != nil {
return nil, err
}
Expand Down