Skip to content

Commit

Permalink
Add GoImports Post Processor (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
asahasrabuddhe committed Sep 20, 2022
1 parent 6a47fcc commit b292ea4
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 2 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ require (
github.com/golang/protobuf v1.5.2
github.com/spf13/afero v1.3.3
github.com/stretchr/testify v1.6.1
google.golang.org/protobuf v1.26.0 // indirect
golang.org/x/tools v0.1.12
google.golang.org/protobuf v1.26.0
)
25 changes: 24 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,36 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
Expand Down
40 changes: 40 additions & 0 deletions lang/go/goimports.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package pgsgo

import (
"strings"

"golang.org/x/tools/imports"

pgs "github.com/lyft/protoc-gen-star"
)

type goImports struct{}

// GoImports returns a PostProcessor that run goimports on any files ending . ".go"
func GoImports() pgs.PostProcessor { return goImports{} }

func (g goImports) Match(a pgs.Artifact) bool {
var n string

switch a := a.(type) {
case pgs.GeneratorFile:
n = a.Name
case pgs.GeneratorTemplateFile:
n = a.Name
case pgs.CustomFile:
n = a.Name
case pgs.CustomTemplateFile:
n = a.Name
default:
return false
}

return strings.HasSuffix(n, ".go")
}

func (g goImports) Process(in []byte) ([]byte, error) {
// We do not want to give a filename here, ever.
return imports.Process("", in, nil)
}

var _ pgs.PostProcessor = goImports{}
54 changes: 54 additions & 0 deletions lang/go/goimports_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package pgsgo

import (
"testing"

"github.com/stretchr/testify/assert"

pgs "github.com/lyft/protoc-gen-star"
)

func TestGoImports_Match(t *testing.T) {
t.Parallel()

pp := GoImports()

tests := []struct {
n string
a pgs.Artifact
m bool
}{
{"GenFile", pgs.GeneratorFile{Name: "foo.go"}, true},
{"GenFileNonGo", pgs.GeneratorFile{Name: "bar.txt"}, false},

{"GenTplFile", pgs.GeneratorTemplateFile{Name: "foo.go"}, true},
{"GenTplFileNonGo", pgs.GeneratorTemplateFile{Name: "bar.txt"}, false},

{"CustomFile", pgs.CustomFile{Name: "foo.go"}, true},
{"CustomFileNonGo", pgs.CustomFile{Name: "bar.txt"}, false},

{"CustomTplFile", pgs.CustomTemplateFile{Name: "foo.go"}, true},
{"CustomTplFileNonGo", pgs.CustomTemplateFile{Name: "bar.txt"}, false},

{"NonMatch", pgs.GeneratorAppend{FileName: "foo.go"}, false},
}

for _, test := range tests {
tc := test
t.Run(tc.n, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tc.m, pp.Match(tc.a))
})
}
}

func TestGoImports_Process(t *testing.T) {
t.Parallel()

src := []byte("package foo\n\nimport (\n\t\"fmt\"\n\t\"strings\"\n)\n\nfunc Hello() {\n\tfmt.Println(\"Hello\")\n}\n")
exp := []byte("package foo\n\nimport (\n\t\"fmt\"\n)\n\nfunc Hello() {\n\tfmt.Println(\"Hello\")\n}\n")

out, err := GoImports().Process(src)
assert.NoError(t, err)
assert.Equal(t, string(exp), string(out))
}

0 comments on commit b292ea4

Please sign in to comment.