Skip to content

Commit

Permalink
feat: short local name
Browse files Browse the repository at this point in the history
  • Loading branch information
morlay committed Sep 19, 2024
1 parent cc61f99 commit 2709f94
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ jobs:

- uses: actions/setup-go@v3
with:
go-version: '^1.18'
go-version: '^1.23'

- run: make cover
16 changes: 8 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
module github.com/octohelm/gengo

go 1.22
go 1.23.1

require (
github.com/go-courier/logr v0.3.0
github.com/octohelm/x v0.0.0-20240622073357-3fcb5294a9e0
github.com/onsi/gomega v1.33.1
github.com/octohelm/x v0.0.0-20240904081416-42a1ee2d28a9
github.com/onsi/gomega v1.34.2
github.com/pkg/errors v0.9.1
golang.org/x/sync v0.7.0
golang.org/x/text v0.15.0
golang.org/x/tools v0.21.0
golang.org/x/sync v0.8.0
golang.org/x/text v0.18.0
golang.org/x/tools v0.25.0
)

require (
github.com/google/go-cmp v0.6.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/net v0.29.0 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
44 changes: 22 additions & 22 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
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-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
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-20240424215950-a892ee059fd6 h1:k7nVchz72niMH6YLQNvHSdIE7iqsQxK1P41mySCvssg=
github.com/google/pprof v0.0.0-20240424215950-a892ee059fd6/go.mod h1:kf6iHlnVGwgKolg33glAes7Yg/8iWP8ukqeldJSO7jw=
github.com/google/pprof v0.0.0-20240827171923-fa2c70bbbfe5 h1:5iH8iuqE5apketRbSFBy+X1V0o+l+8NF1avt4HWl7cA=
github.com/google/pprof v0.0.0-20240827171923-fa2c70bbbfe5/go.mod h1:vavhavw2zAxS5dIdcRluK6cSGGPlZynqzFM8NdvU144=
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-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/octohelm/x v0.0.0-20240904081416-42a1ee2d28a9 h1:PlgBSARdazQ9dBicDlI8sWILYXyh54yDa9XI4is4MTM=
github.com/octohelm/x v0.0.0-20240904081416-42a1ee2d28a9/go.mod h1:Tql/GYN4vR5X+2LcjgX1r/Qbjr8epCjkvRZgKg4MH3c=
github.com/onsi/ginkgo/v2 v2.20.1 h1:YlVIbqct+ZmnEph770q9Q7NVAz4wwIiVNahee6JyUzo=
github.com/onsi/ginkgo/v2 v2.20.1/go.mod h1:lG9ey2Z29hR41WMVthyJBGUBcBhGOtoPF2VFMvBXFCI=
github.com/onsi/gomega v1.34.2 h1:pNCwDkzrsv7MS9kpaQvVb1aVLahQXyJ/Tv5oAZMI3i8=
github.com/onsi/gomega v1.34.2/go.mod h1:v1xfxRgk0KIsG+QOdm7p8UosrOzPYRo60fd3B/1Dukc=
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=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.21.0 h1:qc0xYgIbsSDt9EyWz05J5wfa7LOVW0YTLOXrqdLAWIw=
golang.org/x/tools v0.21.0/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
golang.org/x/mod v0.21.0 h1:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0=
golang.org/x/mod v0.21.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo=
golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/tools v0.25.0 h1:oFU9pkj/iJgs+0DT+VMHrx+oBKs/LJMV+Uvg78sl+fE=
golang.org/x/tools v0.25.0/go.mod h1:/vtpO8WL1N9cQC3FN5zPqb//fRXskFHbLKk4OW1Q7rg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
4 changes: 2 additions & 2 deletions pkg/gengo/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ func TestDumper_TypeLit(t *testing.T) {

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

testingx.Expect(t,
"*github_com_octohelm_gengo_pkg_gengo.List[github_com_octohelm_gengo_pkg_gengo.List[github_com_octohelm_gengo_pkg_gengo.Item]]",
"*gengo.List[gengo.List[gengo.Item]]",
testingx.Equal(d.ReflectTypeLit(reflect.TypeOf(&List[List[Item]]{}))))
})

Expand Down
91 changes: 79 additions & 12 deletions pkg/namer/import_tracker.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package namer

import (
"go/token"
"slices"
"strconv"
"strings"

"github.com/octohelm/gengo/pkg/camelcase"
gengotypes "github.com/octohelm/gengo/pkg/types"
"golang.org/x/tools/go/packages"
)

type ImportTracker interface {
Expand All @@ -19,38 +22,102 @@ type ImportTracker interface {
type defaultImportTracker struct {
pathToName map[string]string
nameToPath map[string]string
checkStd bool
}

func NewDefaultImportTracker() ImportTracker {
return &defaultImportTracker{
pathToName: map[string]string{},
nameToPath: map[string]string{},
checkStd: true,
}
}

var std = &defaultImportTracker{
pathToName: map[string]string{},
nameToPath: map[string]string{},
}

func init() {
pkgs, err := packages.Load(nil, "std")
if err != nil {
panic(err)
}
for _, p := range pkgs {
std.add(p.PkgPath)
}
}

func (tracker *defaultImportTracker) AddType(o gengotypes.TypeName) {
path := o.Pkg().Path()
tracker.add(o.Pkg().Path())
}

func (tracker *defaultImportTracker) add(path string) {
if _, ok := tracker.pathToName[path]; ok {
return
}

localName := golangTrackerLocalName(path)
parts := strings.Split(path, "/")

for i := range len(parts) {
localName := golangTrackerLocalName(parts, i+1)

tracker.nameToPath[localName] = path
tracker.pathToName[path] = localName
if tracker.checkStd {
if p, ok := std.nameToPath[localName]; ok && p != path {
continue
}
}

if _, ok := tracker.nameToPath[localName]; !ok {
tracker.nameToPath[localName] = path
tracker.pathToName[path] = localName
break
}
}
}

func golangTrackerLocalName(name string) string {
name = strings.Replace(name, "/", "_", -1)
name = strings.Replace(name, ".", "_", -1)
name = strings.Replace(name, "-", "_", -1)
func toLocalName(parts ...string) string {
return strings.ToLower(camelcase.LowerCamelCase(strings.Join(parts, "")))
}

func golangTrackerLocalName(pathSegments []string, n int) string {
if len(pathSegments) == 1 {
return toLocalName(pathSegments[0])
}

if token.Lookup(name).IsKeyword() {
name = "_" + name
if n == 1 {
// first good check
if i := slices.Index(pathSegments, "domain"); i > 0 && i+1 < len(pathSegments) {
return toLocalName(pathSegments[i+1:]...)
}
if i := slices.Index(pathSegments, "apis"); i > 0 && i+1 < len(pathSegments) {
return toLocalName(pathSegments[i+1:]...)
}
}

return name
parts := make([]string, 0)

count := 0
for _, seg := range slices.Backward(pathSegments) {
if count >= n {
break
}

// vNumber
if strings.HasPrefix(seg, "v") {
if _, err := strconv.ParseInt(seg[1:], 10, 64); err == nil {
parts = append(parts, seg)
continue
}
}
parts = append(parts, seg)

count++
}

slices.Reverse(parts)

return toLocalName(parts...)
}

func (tracker *defaultImportTracker) Imports() map[string]string {
Expand Down

0 comments on commit 2709f94

Please sign in to comment.