Skip to content

feat: Support generic output type #1

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

Merged
merged 1 commit into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 15 additions & 1 deletion interfaces/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"path"
"reflect"
"regexp"
"strings"
"text/template"

Expand Down Expand Up @@ -87,6 +88,19 @@ func Generate(clients []any, dir string, opts ...Option) error {
return nil
}

func normalizedGenericTypeName(str string) string {
// Generic output types have the full import path in the string value, so we need to normalize it
pattern := regexp.MustCompile(`\[(.*?)\]`)
groups := pattern.FindStringSubmatch((str))
if len(groups) < 2 {
return str
}

typeName := groups[1]
normalizedGenericTypeName := strings.Split(typeName, "/")
return pattern.ReplaceAllString(str, "["+normalizedGenericTypeName[len(normalizedGenericTypeName)-1]+"]")
}

// Adapted from https://stackoverflow.com/a/54129236
func signature(name string, f any) string {
t := reflect.TypeOf(f)
Expand Down Expand Up @@ -117,7 +131,7 @@ func signature(name string, f any) string {
if i > 0 {
buf.WriteString(", ")
}
buf.WriteString(t.Out(i).String())
buf.WriteString(normalizedGenericTypeName(t.Out(i).String()))
}
if numOut > 1 {
buf.WriteString(")")
Expand Down
15 changes: 12 additions & 3 deletions interfaces/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import (
"github.com/google/go-cmp/cmp"
)

type Response struct {
}
type Pager[T any] struct {
}

type Client struct{}

func (*Client) ListThings() error {
Expand All @@ -25,6 +30,10 @@ func (*Client) CreateTables(_ context.Context, _ []string) error {
return nil
}

func (*Client) NewPager(_ context.Context) *Pager[Response] {
return nil
}

var wantOutput = `// Code generated by codegen; DO NOT EDIT.
package services

Expand All @@ -36,17 +45,17 @@ import (
//go:generate mockgen -package=mocks -destination=../mocks/interfaces.go -source=interfaces.go InterfacesClient
type InterfacesClient interface {
ListTables(context.Context) error
NewPager(context.Context) *interfaces.Pager[interfaces.Response]
}
`

func TestGenerate(t *testing.T) {
dir := t.TempDir()
err := Generate([]any{&Client{}}, dir,
WithIncludeFunc(func(m reflect.Method) bool {
return MethodHasAnyPrefix(m, []string{"List"}) && MethodHasAnySuffix(m, []string{"Tables"})
return MethodHasAnyPrefix(m, []string{"List"}) && MethodHasAnySuffix(m, []string{"Tables"}) || MethodHasAnyPrefix(m, []string{"NewPager"})
}),
WithExtraImports(func(m reflect.Method) []string { return []string{"net/http"} },
))
WithExtraImports(func(m reflect.Method) []string { return []string{"net/http"} }))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand Down