Skip to content

Commit

Permalink
ci(lint): setup golangci-lint
Browse files Browse the repository at this point in the history
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
  • Loading branch information
mmorel-35 committed Sep 10, 2023
1 parent fce8e8d commit c70da62
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 45 deletions.
27 changes: 27 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -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
14 changes: 4 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 --fix ./...
# 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
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand All @@ -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
9 changes: 0 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,20 @@ 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=
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=
Expand Down
12 changes: 7 additions & 5 deletions module/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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"]
Expand Down
8 changes: 5 additions & 3 deletions templates/cc/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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()
}
Expand Down
2 changes: 1 addition & 1 deletion templates/go/register.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
12 changes: 6 additions & 6 deletions templates/goshared/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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()
}
Expand Down
1 change: 0 additions & 1 deletion templates/java/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 7 additions & 5 deletions templates/pkg.go
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
1 change: 0 additions & 1 deletion templates/shared/well_known.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 0 additions & 2 deletions tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

0 comments on commit c70da62

Please sign in to comment.