From 53d835564305d73b17e9b122d58e5001b2e93fc6 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Wed, 4 Oct 2023 00:05:41 +0200 Subject: [PATCH] Setup golangci-lint (#987) This PR setup golangci-lint: * gofmt is replace by gofumpt * gci is organising the imports * go vet is configured to be used with shadow rule Supersedes #953 Signed-off-by: Matthieu MOREL --------- Signed-off-by: Matthieu MOREL --- .golangci.yml | 27 +++++++++++++++++++++++++++ Makefile | 14 ++++---------- cmd/protoc-gen-validate-cpp/main.go | 3 ++- cmd/protoc-gen-validate-go/main.go | 3 ++- cmd/protoc-gen-validate-java/main.go | 3 ++- go.mod | 3 +-- go.sum | 21 +-------------------- main.go | 3 ++- module/checker.go | 15 +++++++++------ module/validate.go | 5 +++-- templates/cc/register.go | 11 +++++++---- templates/ccnop/register.go | 3 ++- templates/go/register.go | 3 ++- templates/goshared/register.go | 9 +++++---- templates/java/register.go | 4 ++-- templates/pkg.go | 11 +++++++---- templates/shared/context.go | 3 ++- templates/shared/disabled.go | 3 ++- templates/shared/well_known.go | 4 ++-- tools.go | 2 -- 20 files changed, 84 insertions(+), 66 deletions(-) create mode 100644 .golangci.yml diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 000000000..809a572ec --- /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/envoyproxy) + 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..b130e602f 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 + $(shell pwd)/bin/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/cmd/protoc-gen-validate-cpp/main.go b/cmd/protoc-gen-validate-cpp/main.go index a1db60c62..3174519fe 100644 --- a/cmd/protoc-gen-validate-cpp/main.go +++ b/cmd/protoc-gen-validate-cpp/main.go @@ -1,9 +1,10 @@ package main import ( - "github.com/envoyproxy/protoc-gen-validate/module" pgs "github.com/lyft/protoc-gen-star/v2" "google.golang.org/protobuf/types/pluginpb" + + "github.com/envoyproxy/protoc-gen-validate/module" ) func main() { diff --git a/cmd/protoc-gen-validate-go/main.go b/cmd/protoc-gen-validate-go/main.go index ff979f75b..c749e266d 100644 --- a/cmd/protoc-gen-validate-go/main.go +++ b/cmd/protoc-gen-validate-go/main.go @@ -1,10 +1,11 @@ package main import ( - "github.com/envoyproxy/protoc-gen-validate/module" pgs "github.com/lyft/protoc-gen-star/v2" pgsgo "github.com/lyft/protoc-gen-star/v2/lang/go" "google.golang.org/protobuf/types/pluginpb" + + "github.com/envoyproxy/protoc-gen-validate/module" ) func main() { diff --git a/cmd/protoc-gen-validate-java/main.go b/cmd/protoc-gen-validate-java/main.go index 8b1782839..5b06bbf2d 100644 --- a/cmd/protoc-gen-validate-java/main.go +++ b/cmd/protoc-gen-validate-java/main.go @@ -1,9 +1,10 @@ package main import ( - "github.com/envoyproxy/protoc-gen-validate/module" pgs "github.com/lyft/protoc-gen-star/v2" "google.golang.org/protobuf/types/pluginpb" + + "github.com/envoyproxy/protoc-gen-validate/module" ) func main() { diff --git a/go.mod b/go.mod index 59081f2b0..2651c2838 100644 --- a/go.mod +++ b/go.mod @@ -5,9 +5,7 @@ go 1.21.1 require ( github.com/iancoleman/strcase v0.3.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.15.0 - golang.org/x/tools v0.13.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.12.0 // indirect golang.org/x/text v0.13.0 // indirect + golang.org/x/tools v0.13.0 // indirect ) retract [v0.6.9, v0.6.12] // Published accidentally diff --git a/go.sum b/go.sum index 79f38e8f9..cf2d21399 100644 --- a/go.sum +++ b/go.sum @@ -116,8 +116,6 @@ github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5m github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= -github.com/iancoleman/strcase v0.2.0 h1:05I4QRnGpI0m37iZQRuskXh+w77mr6Z41lwQzuHLwW0= -github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho= github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI= github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= @@ -131,22 +129,17 @@ 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/lyft/protoc-gen-star/v2 v2.0.3 h1:/3+/2sWyXeMLzKd1bX+ixWKgEMsULrIivpDsuaF441o= github.com/lyft/protoc-gen-star/v2 v2.0.3/go.mod h1:amey7yeodaJhXSbf/TlLvWiqQfLOSpEk//mLlc+axEk= -github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI= github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= -github.com/spf13/afero v1.3.3 h1:p5gZEKLYoL7wh8VrJesMaYeNxdEd1v3cb4irOk9zB54= -github.com/spf13/afero v1.3.3/go.mod h1:5KUK8ByomD5Ti5Artl0RtHeI5pTF7MIDuXL3yY520V4= github.com/spf13/afero v1.10.0 h1:EaGW2JJh15aKOejeuJ+wpFSHnbd7GE6Wvp3TsNhb6LY= github.com/spf13/afero v1.10.0/go.mod h1:UBogFpq8E9Hx+xc5CNTTEpTnuHVmXDwZcZcE1eb/UhQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= -github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= @@ -162,7 +155,6 @@ go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -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/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= @@ -190,8 +182,6 @@ golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f/go.mod h1:5qLYkcX4OjUUV8bRu golang.org/x/lint v0.0.0-20200130185559-910be7a94367/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= golang.org/x/lint v0.0.0-20201208152925-83fdc39ff7b5/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= -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/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE= golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o= golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= @@ -236,8 +226,6 @@ golang.org/x/net v0.0.0-20201209123823-ac852fbbde11/go.mod h1:m0MpNAwzfU5UDzcl9v golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14= -golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI= golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8= golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= @@ -259,7 +247,6 @@ golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -295,8 +282,6 @@ golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -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/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= @@ -308,8 +293,6 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -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/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= @@ -362,14 +345,12 @@ golang.org/x/tools v0.0.0-20201208233053-a543418bbed2/go.mod h1:emZCQorbCU4vsT4f golang.org/x/tools v0.0.0-20210105154028-b0ab187a4818/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.0.0-20210108195828-e2f9c7f1fc8e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= -golang.org/x/tools v0.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss= -golang.org/x/tools v0.12.0/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM= golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ= golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 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= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE= google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M= diff --git a/main.go b/main.go index 6ee2e49fd..716404544 100644 --- a/main.go +++ b/main.go @@ -1,10 +1,11 @@ package main import ( - "github.com/envoyproxy/protoc-gen-validate/module" pgs "github.com/lyft/protoc-gen-star/v2" pgsgo "github.com/lyft/protoc-gen-star/v2/lang/go" "google.golang.org/protobuf/types/pluginpb" + + "github.com/envoyproxy/protoc-gen-validate/module" ) func main() { diff --git a/module/checker.go b/module/checker.go index ac192ff8f..7eff3d261 100644 --- a/module/checker.go +++ b/module/checker.go @@ -6,17 +6,20 @@ import ( "time" "unicode/utf8" - "github.com/envoyproxy/protoc-gen-validate/validate" pgs "github.com/lyft/protoc-gen-star/v2" "google.golang.org/protobuf/proto" "google.golang.org/protobuf/types/known/durationpb" "google.golang.org/protobuf/types/known/timestamppb" + + "github.com/envoyproxy/protoc-gen-validate/validate" ) -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 +471,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/module/validate.go b/module/validate.go index d54ff4882..7197601a9 100644 --- a/module/validate.go +++ b/module/validate.go @@ -4,10 +4,11 @@ import ( "path/filepath" "strings" - "github.com/envoyproxy/protoc-gen-validate/templates" - "github.com/envoyproxy/protoc-gen-validate/templates/java" pgs "github.com/lyft/protoc-gen-star/v2" pgsgo "github.com/lyft/protoc-gen-star/v2/lang/go" + + "github.com/envoyproxy/protoc-gen-validate/templates" + "github.com/envoyproxy/protoc-gen-validate/templates/java" ) const ( diff --git a/templates/cc/register.go b/templates/cc/register.go index e376be851..dcc5ed6a6 100644 --- a/templates/cc/register.go +++ b/templates/cc/register.go @@ -7,12 +7,13 @@ import ( "strings" "text/template" - "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" + + "github.com/envoyproxy/protoc-gen-validate/templates/shared" ) func RegisterModule(tpl *template.Template, params pgs.Parameters) { @@ -167,7 +168,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 +264,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 +299,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/ccnop/register.go b/templates/ccnop/register.go index e98d81c85..a633f1211 100644 --- a/templates/ccnop/register.go +++ b/templates/ccnop/register.go @@ -3,8 +3,9 @@ package ccnop import ( "text/template" - "github.com/envoyproxy/protoc-gen-validate/templates/cc" pgs "github.com/lyft/protoc-gen-star/v2" + + "github.com/envoyproxy/protoc-gen-validate/templates/cc" ) func RegisterModule(tpl *template.Template, params pgs.Parameters) { diff --git a/templates/go/register.go b/templates/go/register.go index 80d092e17..59961825f 100644 --- a/templates/go/register.go +++ b/templates/go/register.go @@ -1,9 +1,10 @@ package golang import ( - pgs "github.com/lyft/protoc-gen-star/v2" "text/template" + pgs "github.com/lyft/protoc-gen-star/v2" + "github.com/envoyproxy/protoc-gen-validate/templates/goshared" ) diff --git a/templates/goshared/register.go b/templates/goshared/register.go index ec9574c92..e4f2aed43 100644 --- a/templates/goshared/register.go +++ b/templates/goshared/register.go @@ -8,12 +8,12 @@ import ( "text/template" "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" "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" ) func Register(tpl *template.Template, params pgs.Parameters) { @@ -194,7 +194,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 +227,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..c376ba5d1 100644 --- a/templates/java/register.go +++ b/templates/java/register.go @@ -8,12 +8,13 @@ import ( "text/template" "unicode" - "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" + + "github.com/envoyproxy/protoc-gen-validate/templates/shared" ) func RegisterIndex(tpl *template.Template, params pgs.Parameters) { @@ -377,7 +378,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..04a6881df 100644 --- a/templates/pkg.go +++ b/templates/pkg.go @@ -1,19 +1,22 @@ package templates import ( - "github.com/envoyproxy/protoc-gen-validate/templates/cc" + "text/template" + 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" ) -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/context.go b/templates/shared/context.go index d37a2017d..81916f333 100644 --- a/templates/shared/context.go +++ b/templates/shared/context.go @@ -5,9 +5,10 @@ import ( "fmt" "text/template" - "github.com/envoyproxy/protoc-gen-validate/validate" pgs "github.com/lyft/protoc-gen-star/v2" "google.golang.org/protobuf/proto" + + "github.com/envoyproxy/protoc-gen-validate/validate" ) type RuleContext struct { diff --git a/templates/shared/disabled.go b/templates/shared/disabled.go index 606cba97c..60187a96f 100644 --- a/templates/shared/disabled.go +++ b/templates/shared/disabled.go @@ -1,8 +1,9 @@ package shared import ( - "github.com/envoyproxy/protoc-gen-validate/validate" pgs "github.com/lyft/protoc-gen-star/v2" + + "github.com/envoyproxy/protoc-gen-validate/validate" ) // Disabled returns true if validations are disabled for msg diff --git a/templates/shared/well_known.go b/templates/shared/well_known.go index f2fcd1b4d..125a5000d 100644 --- a/templates/shared/well_known.go +++ b/templates/shared/well_known.go @@ -1,8 +1,9 @@ package shared import ( - "github.com/envoyproxy/protoc-gen-validate/validate" pgs "github.com/lyft/protoc-gen-star/v2" + + "github.com/envoyproxy/protoc-gen-validate/validate" ) type WellKnown string @@ -27,7 +28,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" )