Skip to content

Commit

Permalink
fix: better typelit with generics
Browse files Browse the repository at this point in the history
  • Loading branch information
morlay committed Jun 22, 2024
1 parent 974fb89 commit 54148f4
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 27 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ go 1.22

require (
github.com/go-courier/logr v0.3.0
github.com/octohelm/x v0.0.0-20240507072036-923e675e18dc
github.com/onsi/gomega v1.33.0
github.com/octohelm/x v0.0.0-20240622073357-3fcb5294a9e0
github.com/onsi/gomega v1.33.1
github.com/pkg/errors v0.9.1
golang.org/x/sync v0.7.0
golang.org/x/text v0.15.0
Expand Down
20 changes: 10 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ github.com/go-courier/logr v0.3.0 h1:0VEQB1b53EmYQ+ZehrIgD8l2IO+WX7TY+CqzlykIFmo
github.com/go-courier/logr v0.3.0/go.mod h1:OI7f/JCFZ1ZMD5qG3bIJr5WMNnGzd24+II1D9D9w5x4=
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI=
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/pprof v0.0.0-20240424215950-a892ee059fd6 h1:k7nVchz72niMH6YLQNvHSdIE7iqsQxK1P41mySCvssg=
github.com/google/pprof v0.0.0-20240424215950-a892ee059fd6/go.mod h1:kf6iHlnVGwgKolg33glAes7Yg/8iWP8ukqeldJSO7jw=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/octohelm/x v0.0.0-20240507072036-923e675e18dc h1:iPCbeuhgkry0E/OGJImem5IgHNvn9flDOvh70mWlF0I=
github.com/octohelm/x v0.0.0-20240507072036-923e675e18dc/go.mod h1:Sbbekf0mbZbqc+uOokYMGus+35y30BAca0y0V58CyD0=
github.com/onsi/ginkgo/v2 v2.17.1 h1:V++EzdbhI4ZV4ev0UTIj0PzhzOcReJFyJaLjtSF55M8=
github.com/onsi/ginkgo/v2 v2.17.1/go.mod h1:llBI3WDLL9Z6taip6f33H76YcWtJv+7R3HigUjbIBOs=
github.com/onsi/gomega v1.33.0 h1:snPCflnZrpMsy94p4lXVEkHo12lmPnc3vY5XBbreexE=
github.com/onsi/gomega v1.33.0/go.mod h1:+925n5YtiFsLzzafLUHzVMBpvvRAzrydIBiSIxjX3wY=
github.com/octohelm/x v0.0.0-20240622073357-3fcb5294a9e0 h1:25JOHKNSWuU65Omf7Ob9VVPA6e7M+hWLAmzZL/ndYQY=
github.com/octohelm/x v0.0.0-20240622073357-3fcb5294a9e0/go.mod h1:H8L5swKf8wx4DfgGbOhnPMe+POcExkmQmUm3p7W1EF0=
github.com/onsi/ginkgo/v2 v2.17.2 h1:7eMhcy3GimbsA3hEnVKdw/PQM9XN9krpKVXsZdph0/g=
github.com/onsi/ginkgo/v2 v2.17.2/go.mod h1:nP2DPOQoNsQmsVyv5rDA8JkXQoCs6goXIvr/PRJ1eCc=
github.com/onsi/gomega v1.33.1 h1:dsYjIxxSR755MDmKVsaFQTE22ChNBcuuTWgkUDSubOk=
github.com/onsi/gomega v1.33.1/go.mod h1:U4R44UsT+9eLIaYRB2a5qajjtQYn0hauxvRm16AVYg0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
Expand Down
31 changes: 30 additions & 1 deletion pkg/gengo/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"reflect"
"sort"
"strconv"
"strings"

"github.com/octohelm/gengo/pkg/namer"
gengotypes "github.com/octohelm/gengo/pkg/types"
Expand Down Expand Up @@ -37,9 +38,35 @@ func (d *Dumper) TypesTypeLit(tpe types.Type) string {
return d.TypeLit(typesutil.FromTType(tpe))
}

func (d *Dumper) normalizeTypeName(name string) string {
if i := strings.Index(name, "["); i > 0 {
b := &strings.Builder{}
b.WriteString(name[0:i])
b.WriteByte('[')

for i, n := range strings.Split(name[i+1:len(name)-1], ",") {
if i > 0 {
b.WriteByte(',')
}

if j := strings.LastIndex(n, "."); j != -1 {
b.WriteString(d.Name(gengotypes.Ref(n[0:j], n[j+1:])))
continue
}

b.WriteString(n)
}

b.WriteByte(']')
return b.String()
}

return name
}

func (d *Dumper) TypeLit(tpe typesutil.Type) string {
if tpe.PkgPath() != "" {
return d.Name(gengotypes.Ref(tpe.PkgPath(), tpe.Name()))
return d.Name(gengotypes.Ref(tpe.PkgPath(), d.normalizeTypeName(tpe.Name())))
}

switch tpe.Kind() {
Expand Down Expand Up @@ -75,6 +102,8 @@ func (d *Dumper) TypeLit(tpe typesutil.Type) string {
return fmt.Sprintf("[]%s", d.TypeLit(tpe.Elem()))
case reflect.Map:
return fmt.Sprintf("map[%s]%s", d.TypeLit(tpe.Key()), d.TypeLit(tpe.Elem()))
case reflect.Interface:
return "any"
default:
return tpe.String()
}
Expand Down
30 changes: 20 additions & 10 deletions pkg/gengo/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,42 @@ package gengo

import (
"bytes"
testingx "github.com/octohelm/x/testing"
"reflect"
"testing"

"github.com/octohelm/gengo/pkg/namer"
"github.com/onsi/gomega"
)

func TestDumper_TypeLit(t *testing.T) {
tt := gomega.NewWithT(t)
type Item struct {
Name string `json:"name"`
}

type List[T any] struct {
Items []T `json:"items,omitempty"`
}

func TestDumper_TypeLit(t *testing.T) {
d := NewDumper(namer.NewRawNamer("", namer.NewDefaultImportTracker()))

t.Run("TypeLit", func(t *testing.T) {
tt.Expect("*bytes.Buffer").To(gomega.Equal(d.ReflectTypeLit(reflect.TypeOf(&bytes.Buffer{}))))
tt.Expect("[]string").To(gomega.Equal(d.ReflectTypeLit(reflect.TypeOf([]string{}))))
tt.Expect("map[string]string").To(gomega.Equal(d.ReflectTypeLit(reflect.TypeOf(map[string]string{}))))
tt.Expect("*struct {V string `json:\"v\" validate:\"@int[0,10]\"`\n}").To(gomega.Equal(d.ReflectTypeLit(reflect.TypeOf(&struct {
testingx.Expect(t, "*bytes.Buffer", testingx.Be(d.ReflectTypeLit(reflect.TypeOf(&bytes.Buffer{}))))
testingx.Expect(t, "[]string", testingx.Be(d.ReflectTypeLit(reflect.TypeOf([]string{}))))
testingx.Expect(t, "map[string]string", testingx.Be(d.ReflectTypeLit(reflect.TypeOf(map[string]string{}))))
testingx.Expect(t, "*struct {V string `json:\"v\" validate:\"@int[0,10]\"`\n}", testingx.Be(d.ReflectTypeLit(reflect.TypeOf(&struct {
V string `json:"v" validate:"@int[0,10]"`
}{}))))
})

t.Run("TypeListWithGenerics", func(t *testing.T) {
testingx.Expect(t, "*github_com_octohelm_gengo_pkg_gengo.List[github_com_octohelm_gengo_pkg_gengo.Item]", testingx.Equal(d.ReflectTypeLit(reflect.TypeOf(&List[Item]{}))))
})

t.Run("ValueLit", func(t *testing.T) {
tt.Expect("&(bytes.Buffer{})").To(gomega.Equal(d.ValueLit(reflect.ValueOf(&(bytes.Buffer{})))))
tt.Expect(`[]string{
testingx.Expect(t, "&(bytes.Buffer{})", testingx.Be(d.ValueLit(reflect.ValueOf(&(bytes.Buffer{})))))
testingx.Expect(t, `[]string{
"1",
"2",
}`).To(gomega.Equal(d.ValueLit(reflect.ValueOf([]string{"1", "2"}))))
}`, testingx.Be(d.ValueLit(reflect.ValueOf([]string{"1", "2"}))))
})
}
2 changes: 1 addition & 1 deletion testdata/a/b/zz_generated.defaulter.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Package b GENERATED BY gengo:defaulter
Package b GENERATED BY gengo:defaulter
DON'T EDIT THIS FILE
*/
package b
Expand Down
2 changes: 1 addition & 1 deletion testdata/a/b/zz_generated.runtimedoc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Package b GENERATED BY gengo:runtimedoc
Package b GENERATED BY gengo:runtimedoc
DON'T EDIT THIS FILE
*/
package b
Expand Down
4 changes: 2 additions & 2 deletions testdata/a/c/zz_generated.deepcopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (in *KubePkgSpec) DeepCopyInto(out *KubePkgSpec) {
}
if in.Manifests != nil {
i, o := &in.Manifests, &out.Manifests
*o = make(map[string]interface{}, len(*i))
*o = make(map[string]any, len(*i))
for key, val := range *i {
(*o)[key] = val
}
Expand All @@ -95,7 +95,7 @@ func (in *KubePkgStatus) DeepCopy() *KubePkgStatus {
func (in *KubePkgStatus) DeepCopyInto(out *KubePkgStatus) {
if in.Statuses != nil {
i, o := &in.Statuses, &out.Statuses
*o = make(map[string]interface{}, len(*i))
*o = make(map[string]any, len(*i))
for key, val := range *i {
(*o)[key] = val
}
Expand Down

0 comments on commit 54148f4

Please sign in to comment.