From 513d4d412f60e4478561c52ad387be0aa81e9fd8 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Sun, 10 Sep 2023 22:26:15 +0200 Subject: [PATCH] ci(lint): setup golangci-lint Signed-off-by: Matthieu MOREL --- .golangci.yml | 27 +++++++++++++++++++++++++++ Makefile | 14 ++++---------- go.mod | 3 +-- go.sum | 9 --------- module/checker.go | 12 +++++++----- templates/cc/register.go | 8 +++++--- templates/go/register.go | 2 +- templates/goshared/register.go | 12 ++++++------ templates/java/register.go | 1 - templates/pkg.go | 12 +++++++----- templates/shared/well_known.go | 1 - tools.go | 2 -- 12 files changed, 58 insertions(+), 45 deletions(-) create mode 100644 .golangci.yml diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 000000000..0123d368e --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,27 @@ +linters: + disable: + - unused + enable: + - gci + - gofumpt + - govet + - misspell + +linters-settings: + gci: + sections: + - standard + - default + - prefix(github.com/bufbuild) + govet: + check-shadowing: true + # Settings per analyzer. + settings: + shadow: + # Whether to be strict about shadowing; can be noisy. + strict: true + enable: + - shadow + +run: + timeout: 5m diff --git a/Makefile b/Makefile index 1a7a80f05..c5fd2e265 100644 --- a/Makefile +++ b/Makefile @@ -37,20 +37,14 @@ gazelle: ## runs gazelle against the codebase to generate Bazel BUILD files bazel run //:gazelle .PHONY: lint -lint: bin/golint bin/shadow ## lints the package for common code smells - test -z "$(shell gofmt -d -s ./*.go)" || (gofmt -d -s ./*.go && exit 1) - # golint -set_exit_status - # check for variable shadowing - go vet -vettool=$(shell pwd)/bin/shadow ./... +lint: bin/golangci-lint ## lints the package for common code smells + golangci-lint run ./... # lints the python code for style enforcement flake8 --config=python/setup.cfg python/protoc_gen_validate/validator.py isort --check-only python/protoc_gen_validate/validator.py -bin/shadow: - GOBIN=$(shell pwd)/bin go install golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow - -bin/golint: - GOBIN=$(shell pwd)/bin go install golang.org/x/lint/golint +bin/golangci-lint: + GOBIN=$(shell pwd)/bin go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.54.2 bin/protoc-gen-go: GOBIN=$(shell pwd)/bin go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.30.0 diff --git a/go.mod b/go.mod index c62291bf7..72cf3118a 100644 --- a/go.mod +++ b/go.mod @@ -5,9 +5,7 @@ go 1.19 require ( github.com/iancoleman/strcase v0.2.0 github.com/lyft/protoc-gen-star/v2 v2.0.3 - golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 golang.org/x/net v0.14.0 - golang.org/x/tools v0.12.0 google.golang.org/protobuf v1.31.0 ) @@ -16,6 +14,7 @@ require ( golang.org/x/mod v0.12.0 // indirect golang.org/x/sys v0.11.0 // indirect golang.org/x/text v0.12.0 // indirect + golang.org/x/tools v0.12.0 // indirect ) retract [v0.6.9, v0.6.12] // Published accidentally diff --git a/go.sum b/go.sum index fa7d177f1..471ba1d10 100644 --- a/go.sum +++ b/go.sum @@ -19,18 +19,11 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= 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-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 h1:VLliZ0d+/avPrXXH+OakdXhpJuEoBZuwh1m2j7U6Iug= -golang.org/x/lint v0.0.0-20210508222113-6edffad5e616/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= -golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= 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.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14= golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= 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/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM= @@ -38,10 +31,8 @@ golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc= golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= -golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/tools v0.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss= golang.org/x/tools v0.12.0/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM= -golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/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= diff --git a/module/checker.go b/module/checker.go index ac192ff8f..3f533506d 100644 --- a/module/checker.go +++ b/module/checker.go @@ -13,10 +13,12 @@ import ( "google.golang.org/protobuf/types/known/timestamppb" ) -var unknown = "" -var httpHeaderName = "^:?[0-9a-zA-Z!#$%&'*+-.^_|~\x60]+$" -var httpHeaderValue = "^[^\u0000-\u0008\u000A-\u001F\u007F]*$" -var headerString = "^[^\u0000\u000A\u000D]*$" // For non-strict validation. +var ( + unknown = "" + httpHeaderName = "^:?[0-9a-zA-Z!#$%&'*+-.^_|~\x60]+$" + httpHeaderValue = "^[^\u0000-\u0008\u000A-\u001F\u007F]*$" + headerString = "^[^\u0000\u000A\u000D]*$" // For non-strict validation. +) // Map from well known regex to regex pattern. var regex_map = map[string]*string{ @@ -468,7 +470,7 @@ func (m *Module) checkLen(len, min, max *uint64) { func (m *Module) checkWellKnownRegex(wk validate.KnownRegex, r *validate.StringRules) { if wk != 0 { m.Assert(r.Pattern == nil, "regex `well_known_regex` and regex `pattern` are incompatible") - var non_strict = r.Strict != nil && !*r.Strict + non_strict := r.Strict != nil && !*r.Strict if (wk.String() == "HTTP_HEADER_NAME" || wk.String() == "HTTP_HEADER_VALUE") && non_strict { // Use non-strict header validation. r.Pattern = regex_map["HEADER_STRING"] diff --git a/templates/cc/register.go b/templates/cc/register.go index e376be851..217bf0c7b 100644 --- a/templates/cc/register.go +++ b/templates/cc/register.go @@ -167,7 +167,8 @@ func (fns CCFuncs) packageName(msg pgs.Entity) string { func (fns CCFuncs) quote(s interface { String() string -}) string { +}, +) string { return strconv.Quote(s.String()) } @@ -262,7 +263,8 @@ func (fns CCFuncs) lit(x interface{}) string { func (fns CCFuncs) isBytes(f interface { ProtoType() pgs.ProtoType -}) bool { +}, +) bool { return f.ProtoType() == pgs.BytesT } @@ -296,7 +298,7 @@ func (fns CCFuncs) inType(f pgs.Field, x interface{}) string { return fns.className(f.Type().Element().Embed()) } case pgs.EnumT: - var fldEn = f.Type().Enum() + fldEn := f.Type().Enum() if f.Type().IsRepeated() { fldEn = f.Type().Element().Enum() } diff --git a/templates/go/register.go b/templates/go/register.go index 80d092e17..d88e445f1 100644 --- a/templates/go/register.go +++ b/templates/go/register.go @@ -1,10 +1,10 @@ package golang import ( - pgs "github.com/lyft/protoc-gen-star/v2" "text/template" "github.com/envoyproxy/protoc-gen-validate/templates/goshared" + pgs "github.com/lyft/protoc-gen-star/v2" ) func Register(tpl *template.Template, params pgs.Parameters) { diff --git a/templates/goshared/register.go b/templates/goshared/register.go index ec9574c92..7b2178d26 100644 --- a/templates/goshared/register.go +++ b/templates/goshared/register.go @@ -7,13 +7,12 @@ import ( "strings" "text/template" - "github.com/iancoleman/strcase" - "google.golang.org/protobuf/types/known/durationpb" - "google.golang.org/protobuf/types/known/timestamppb" - "github.com/envoyproxy/protoc-gen-validate/templates/shared" + "github.com/iancoleman/strcase" pgs "github.com/lyft/protoc-gen-star/v2" pgsgo "github.com/lyft/protoc-gen-star/v2/lang/go" + "google.golang.org/protobuf/types/known/durationpb" + "google.golang.org/protobuf/types/known/timestamppb" ) func Register(tpl *template.Template, params pgs.Parameters) { @@ -194,7 +193,8 @@ func (fns goSharedFuncs) lit(x interface{}) string { func (fns goSharedFuncs) isBytes(f interface { ProtoType() pgs.ProtoType -}) bool { +}, +) bool { return f.ProtoType() == pgs.BytesT } @@ -226,7 +226,7 @@ func (fns goSharedFuncs) inType(f pgs.Field, x interface{}) string { ens := fns.enumPackages(fns.externalEnums(f.File())) // Check if the imported name of the enum has collided and been renamed if len(ens) != 0 { - var enType = f.Type().Enum() + enType := f.Type().Enum() if f.Type().IsRepeated() { enType = f.Type().Element().Enum() } diff --git a/templates/java/register.go b/templates/java/register.go index fee88b3ab..8cc24f9f4 100644 --- a/templates/java/register.go +++ b/templates/java/register.go @@ -377,7 +377,6 @@ func (fns javaFuncs) javaTypeFor(ctx shared.RuleContext) string { } func (fns javaFuncs) javaTypeForProtoType(t pgs.ProtoType) string { - switch t { case pgs.Int32T, pgs.UInt32T, pgs.SInt32, pgs.Fixed32T, pgs.SFixed32: return "Integer" diff --git a/templates/pkg.go b/templates/pkg.go index 2a97806af..5a1a6bb66 100644 --- a/templates/pkg.go +++ b/templates/pkg.go @@ -1,19 +1,21 @@ package templates import ( - "github.com/envoyproxy/protoc-gen-validate/templates/cc" - pgs "github.com/lyft/protoc-gen-star/v2" - pgsgo "github.com/lyft/protoc-gen-star/v2/lang/go" "text/template" + "github.com/envoyproxy/protoc-gen-validate/templates/cc" "github.com/envoyproxy/protoc-gen-validate/templates/ccnop" "github.com/envoyproxy/protoc-gen-validate/templates/go" "github.com/envoyproxy/protoc-gen-validate/templates/java" "github.com/envoyproxy/protoc-gen-validate/templates/shared" + pgs "github.com/lyft/protoc-gen-star/v2" + pgsgo "github.com/lyft/protoc-gen-star/v2/lang/go" ) -type RegisterFn func(tpl *template.Template, params pgs.Parameters) -type FilePathFn func(f pgs.File, ctx pgsgo.Context, tpl *template.Template) *pgs.FilePath +type ( + RegisterFn func(tpl *template.Template, params pgs.Parameters) + FilePathFn func(f pgs.File, ctx pgsgo.Context, tpl *template.Template) *pgs.FilePath +) func makeTemplate(ext string, fn RegisterFn, params pgs.Parameters) *template.Template { tpl := template.New(ext) diff --git a/templates/shared/well_known.go b/templates/shared/well_known.go index f2fcd1b4d..5a22dac58 100644 --- a/templates/shared/well_known.go +++ b/templates/shared/well_known.go @@ -27,7 +27,6 @@ func FileNeeds(f pgs.File, wk WellKnown) bool { // Needs returns true if a well-known string validator is needed for this // message. func Needs(m pgs.Message, wk WellKnown) bool { - for _, f := range m.Fields() { var rules validate.FieldRules if _, err := f.Extension(validate.E_Rules, &rules); err != nil { diff --git a/tools.go b/tools.go index a35b07bcf..5ff6367fd 100644 --- a/tools.go +++ b/tools.go @@ -4,8 +4,6 @@ package main import ( - _ "golang.org/x/lint/golint" _ "golang.org/x/net/context" - _ "golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow" _ "google.golang.org/protobuf/cmd/protoc-gen-go" )