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

add support for generics in reflect mode #136

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ go 1.20

require (
golang.org/x/mod v0.11.0
golang.org/x/tools v0.2.0
golang.org/x/tools v0.6.0
)

require (
github.com/yuin/goldmark v1.4.13 // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/sys v0.15.0 // indirect
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/mod v0.11.0 h1:bUO06HqtnRcc/7l71XBe4WcqTZ+3AH1J59zWDDwLKgU=
golang.org/x/mod v0.11.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/tools v0.2.0 h1:G6AHpWxTMGY1KyEYoAQ5WTtIekUUvDNjan3ugu60JvE=
golang.org/x/tools v0.2.0/go.mod h1:y4OqIKeOV/fWJetJ8bXPU1sEVniLMIyDAZWeHdV+NTA=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
25 changes: 23 additions & 2 deletions mockgen/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,10 +415,31 @@ func typeFromType(t reflect.Type) (Type, error) {
}

if imp := t.PkgPath(); imp != "" {
return &NamedType{

typeObj := &NamedType{
Package: impPath(imp),
Type: t.Name(),
}, nil
}
if typeName := t.Name(); strings.ContainsAny(typeName, "[]") {
var ts []Type
startIndex := strings.Index(typeName, "[")
endIndex := strings.Index(typeName, "]")
Copy link
Contributor

@bcho bcho Jan 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Golang's type parameter can be a slice X[] or another type parameter, therefore, I am afraid the assumption here for unwrapping [] is incorrect giving case like foo.bar[baz[]].

FWIW, since golang's reflection doesn't provide the type parameter details, we don't have a good way to tell what is the concrete type used. One way I was attempting to use is to use a recursive parser to extract the nested type parameters, but not that simple to write right... Another way is to wait for support from reflect...

Another workaround is to use source mode instead of reflect mode...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll try the recursive parser. Thanks for the hint!

if startIndex == -1 || endIndex == -1 || startIndex > endIndex {
return nil, fmt.Errorf("%s is not a valid generic type name", typeName)
}
typeObj.Type = typeName[:startIndex]
for _, element := range strings.Split(typeName[startIndex+1:endIndex], ",") {
sepIndex := strings.LastIndex(element, ".")
ts = append(ts, &NamedType{
Package: impPath(element[:sepIndex]),
Type: element[sepIndex+1:],
})
}
typeObj.TypeParams = &TypeParametersType{
TypeParameters: ts,
}
}
return typeObj, nil
}

// only unnamed or predeclared types after here
Expand Down