diff --git a/go.mod b/go.mod index 4db0ebd..b681d57 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 38c7646..093ed9c 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/mockgen/model/model.go b/mockgen/model/model.go index 853dbf2..6626f64 100644 --- a/mockgen/model/model.go +++ b/mockgen/model/model.go @@ -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, "]") + 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