diff --git a/.bazelversion b/.bazelversion index c7cb1311a..84197c894 100644 --- a/.bazelversion +++ b/.bazelversion @@ -1 +1 @@ -5.3.1 +5.3.2 diff --git a/.dockerignore b/.dockerignore index 434978f75..bf65cd7a5 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,3 +1,4 @@ .git/ .idea/ .bin/ +dist/ diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 2f0156157..1cc05c8ac 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -10,9 +10,9 @@ on: env: # Keep these versions in sync with the Dockerfile BDR_VER: '5.1.0' - GO_VER: '1.19.1' - PGG_VER: 'v1.27.1' - PROTOC_VER: '21.5' + GO_VER: '1.19.3' + PGG_VER: 'v1.28.1' + PROTOC_VER: '21.9' PYTHON_VER: '3.8' TEST_TAG: protoc-gen-validate:${{ github.sha }} diff --git a/.github/workflows/goreleaser.yaml b/.github/workflows/goreleaser.yaml index 899cde386..ad971bd28 100644 --- a/.github/workflows/goreleaser.yaml +++ b/.github/workflows/goreleaser.yaml @@ -1,3 +1,4 @@ +# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json name: goreleaser on: @@ -19,6 +20,7 @@ jobs: - uses: actions/setup-go@v3 with: go-version: 1.19.x + use-latest: true cache: true - uses: goreleaser/goreleaser-action@v2 with: diff --git a/.github/workflows/maven-build.yaml b/.github/workflows/maven-build.yaml index be1fa37e1..cdd9a4bda 100644 --- a/.github/workflows/maven-build.yaml +++ b/.github/workflows/maven-build.yaml @@ -19,7 +19,7 @@ jobs: with: java-version: "17" distribution: "temurin" - + cache: "maven" - name: Verify with Maven working-directory: ${{ github.workspace }}/java run: mvn -B verify diff --git a/.github/workflows/maven-deploy.yaml b/.github/workflows/maven-deploy.yaml index 4a688ca2b..57894211a 100644 --- a/.github/workflows/maven-deploy.yaml +++ b/.github/workflows/maven-deploy.yaml @@ -52,6 +52,7 @@ jobs: with: distribution: 'temurin' java-version: '17' + cache: 'maven' server-id: sonatype-nexus-snapshots server-username: ${ env.SONATYPE_USER } server-password: ${ env.SONATYPE_PASSWORD } diff --git a/.gitignore b/.gitignore index 9ab12bb17..57c747c69 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,10 @@ bazel-* !vendor/* /bin +/cmd/protoc-gen-validate-cpp/protoc-gen-validate-cpp +/cmd/protoc-gen-validate-go/protoc-gen-validate-go +/cmd/protoc-gen-validate-java/protoc-gen-validate-java +/dist/ /protoc-gen-validate /python/LICENSE diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 59c47589c..3a09eace9 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -1,3 +1,17 @@ +builds: + - main: . + id: "protoc-gen-validate" + binary: "protoc-gen-validate" + - main: ./cmd/protoc-gen-validate-cpp + id: "protoc-gen-validate-cpp" + binary: "protoc-gen-validate-cpp" + - main: ./cmd/protoc-gen-validate-go + id: "protoc-gen-validate-go" + binary: "protoc-gen-validate-go" + - main: ./cmd/protoc-gen-validate-java + id: "protoc-gen-validate-java" + binary: "protoc-gen-validate-java" + release: # If set to true, will not auto-publish the release. # Available only for GitHub and Gitea. diff --git a/Dockerfile b/Dockerfile index 393fbd407..cc2dd9f8a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -25,31 +25,32 @@ RUN apt update \ && apt clean # bazel -ENV BAZEL_VER=5.3.1 -RUN wget -O bazel https://github.com/bazelbuild/bazel/releases/download/${BAZEL_VER}/bazel-${BAZEL_VER}-linux-$([ $(uname -m) = "aarch64" ] && echo "arm64" || echo "x86_64") \ +ENV BAZEL_VER=5.3.2 +RUN wget -q -O bazel https://github.com/bazelbuild/bazel/releases/download/${BAZEL_VER}/bazel-${BAZEL_VER}-linux-$([ $(uname -m) = "aarch64" ] && echo "arm64" || echo "x86_64") \ && chmod +x bazel \ && mv bazel usr/local/bin/bazel # protoc -ENV PROTOC_VER=21.5 +ENV PROTOC_VER=21.9 RUN export PROTOC_REL=protoc-${PROTOC_VER}-linux-$([ $(uname -m) = "aarch64" ] && echo "aarch" || echo "x86")_64.zip \ - && wget https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VER}/${PROTOC_REL} \ + && wget -q https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VER}/${PROTOC_REL} \ && unzip ${PROTOC_REL} -d protoc \ && mv protoc /usr/local \ - && ln -s /usr/local/protoc/bin/protoc /usr/local/bin + && ln -s /usr/local/protoc/bin/protoc /usr/local/bin \ + && rm ${PROTOC_REL} # go ENV GOROOT /usr/local/go ENV GOPATH /go ENV PATH $GOPATH/bin:$GOROOT/bin:$PATH -RUN export GORELEASE=go1.19.1.linux-$([ $(uname -m) = "aarch64" ] && echo "arm64" || echo "amd64").tar.gz \ +RUN export GORELEASE=go1.19.3.linux-$([ $(uname -m) = "aarch64" ] && echo "arm64" || echo "amd64").tar.gz \ && wget -q https://dl.google.com/go/$GORELEASE \ && tar -C $(dirname $GOROOT) -xzf $GORELEASE \ && rm $GORELEASE \ && mkdir -p $GOPATH/{src,bin,pkg} # protoc-gen-go -ENV PGG_VER=v1.27.1 +ENV PGG_VER=v1.28.1 RUN go install google.golang.org/protobuf/cmd/protoc-gen-go@${PGG_VER} \ && rm -rf $(go env GOCACHE) \ && rm -rf $(go env GOMODCACHE) @@ -60,17 +61,19 @@ RUN go install github.com/bazelbuild/buildtools/buildozer@${BDR_VER} \ && rm -rf $(go env GOCACHE) \ && rm -rf $(go env GOMODCACHE) -WORKDIR ${GOPATH}/src/github.com/envoyproxy/protoc-gen-validate -COPY . . - # python must be on PATH for the execution of py_binary bazel targets, but # the distribution we installed doesn't provide this alias RUN ln -s /usr/bin/python3.8 /usr/bin/python +WORKDIR ${GOPATH}/src/github.com/envoyproxy/protoc-gen-validate + # python tooling for linting and uploading to PyPI +COPY requirements.txt . RUN python3.8 -m easy_install pip \ && python3.8 -m pip install -r requirements.txt +COPY . . + RUN make build ENTRYPOINT ["make"] diff --git a/Gopkg.lock b/Gopkg.lock deleted file mode 100644 index 199262294..000000000 --- a/Gopkg.lock +++ /dev/null @@ -1,107 +0,0 @@ -# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. - - -[[projects]] - digest = "1:6e73003ecd35f4487a5e88270d3ca0a81bc80dc88053ac7e4dcfec5fba30d918" - name = "github.com/gogo/protobuf" - packages = [ - "proto", - "sortkeys", - "types", - ] - pruneopts = "" - revision = "636bf0302bc95575d69441b25a2603156ffdddf1" - version = "v1.1.1" - -[[projects]] - digest = "1:529d738b7976c3848cae5cf3a8036440166835e389c1f617af701eeb12a0518d" - name = "github.com/golang/protobuf" - packages = [ - "proto", - "protoc-gen-go/descriptor", - "protoc-gen-go/generator", - "protoc-gen-go/generator/internal/remap", - "protoc-gen-go/plugin", - "ptypes", - "ptypes/any", - "ptypes/duration", - "ptypes/timestamp", - "ptypes/wrappers", - ] - pruneopts = "" - revision = "b5d812f8a3706043e23a9cd5babf2e5423744d30" - version = "v1.3.1" - -[[projects]] - branch = "master" - digest = "1:67bcf41677f33b22aee1b82e9460aa093a96cfe4877445dd19ea950c200a47ab" - name = "github.com/iancoleman/strcase" - packages = ["."] - pruneopts = "" - revision = "3605ed457bf7f8caa1371b4fafadadc026673479" - -[[projects]] - digest = "1:5c01d0bd862ce78445d0af823828c42b86830323dbfe6d338a8c55ebc09e7e7e" - name = "github.com/lyft/protoc-gen-star" - packages = [ - ".", - "lang/go", - ] - pruneopts = "" - revision = "bb8c3f89f33752b4c49c272d2ab2a0fef6f4b0cd" - version = "v0.4.10" - -[[projects]] - digest = "1:d0431c2fd72e39ee43ea7742322abbc200c3e704c9102c5c3c2e2e667095b0ca" - name = "github.com/spf13/afero" - packages = [ - ".", - "mem", - ] - pruneopts = "" - revision = "d40851caa0d747393da1ffb28f7f9d8b4eeffebd" - version = "v1.1.2" - -[[projects]] - branch = "master" - digest = "1:b001b57f383ecbebb54c3916ca6b8be129a6486d3dbf2dfc722cbb0a0b2a8679" - name = "golang.org/x/net" - packages = ["context"] - pruneopts = "" - revision = "9b4f9f5ad5197c79fd623a3638e70d8b26cef344" - -[[projects]] - digest = "1:5acd3512b047305d49e8763eef7ba423901e85d5dd2fd1e71778a0ea8de10bd4" - name = "golang.org/x/text" - packages = [ - "internal/gen", - "internal/triegen", - "internal/ucd", - "transform", - "unicode/cldr", - "unicode/norm", - ] - pruneopts = "" - revision = "f21a4dfb5e38f5895301dc265a8def02365cc3d0" - version = "v0.3.0" - -[solve-meta] - analyzer-name = "dep" - analyzer-version = 1 - input-imports = [ - "github.com/gogo/protobuf/proto", - "github.com/gogo/protobuf/types", - "github.com/golang/protobuf/proto", - "github.com/golang/protobuf/protoc-gen-go/descriptor", - "github.com/golang/protobuf/ptypes", - "github.com/golang/protobuf/ptypes/any", - "github.com/golang/protobuf/ptypes/duration", - "github.com/golang/protobuf/ptypes/timestamp", - "github.com/golang/protobuf/ptypes/wrappers", - "github.com/iancoleman/strcase", - "github.com/lyft/protoc-gen-star", - "github.com/lyft/protoc-gen-star/lang/go", - "golang.org/x/net/context", - ] - solver-name = "gps-cdcl" - solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml deleted file mode 100644 index ee9a95e8a..000000000 --- a/Gopkg.toml +++ /dev/null @@ -1,15 +0,0 @@ -[[constraint]] - name = "github.com/golang/protobuf" - version = "1.4.2" - -[[constraint]] - name = "github.com/lyft/protoc-gen-star" - version = "0.5.1" - -[[constraint]] - branch = "master" - name = "golang.org/x/net" - -[[constraint]] - branch = "master" - name = "github.com/iancoleman/strcase" diff --git a/Makefile b/Makefile index 283a91f86..23d863d44 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,7 @@ build: validate/validate.pb.go ## generates the PGV binary and installs it into .PHONY: bazel bazel: ## generate the PGV plugin with Bazel - bazel build //tests/... + bazel build //cmd/... //tests/... .PHONY: build_generation_tests build_generation_tests: @@ -53,7 +53,7 @@ bin/golint: GOBIN=$(shell pwd)/bin go install golang.org/x/lint/golint bin/protoc-gen-go: - GOBIN=$(shell pwd)/bin go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.27.1 + GOBIN=$(shell pwd)/bin go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28.1 bin/harness: cd tests && go build -o ../bin/harness ./harness/executor diff --git a/bazel/dependency_imports.bzl b/bazel/dependency_imports.bzl index 18da5bc92..7b5fac086 100644 --- a/bazel/dependency_imports.bzl +++ b/bazel/dependency_imports.bzl @@ -15,7 +15,7 @@ def _pgv_pip_dependencies(): def _pgv_go_dependencies(): go_rules_dependencies() go_register_toolchains( - version = "1.19.1", + version = "1.19.3", ) gazelle_dependencies() diff --git a/cmd/protoc-gen-validate-cpp/BUILD b/cmd/protoc-gen-validate-cpp/BUILD new file mode 100644 index 000000000..6af8ef57f --- /dev/null +++ b/cmd/protoc-gen-validate-cpp/BUILD @@ -0,0 +1,29 @@ +load("@bazel_gazelle//:def.bzl", "gazelle") +load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") + +# gazelle:prefix github.com/envoyproxy/protoc-gen-validate +# gazelle:exclude tests +# gazelle:exclude example-workspace +# gazelle:exclude validate/validate.h +# gazelle:go_naming_convention import_alias +gazelle(name = "gazelle") + +go_binary( + name = "protoc-gen-validate-cpp", + embed = [":protoc-gen-validate-cpp_lib"], + importpath = "github.com/envoyproxy/protoc-gen-validate/cmd/protoc-gen-validate-cpp", + visibility = ["//visibility:public"], +) + +go_library( + name = "protoc-gen-validate-cpp_lib", + srcs = ["main.go"], + importpath = "github.com/envoyproxy/protoc-gen-validate/cmd/protoc-gen-validate-cpp", + visibility = ["//visibility:private"], + deps = [ + "//module", + "@com_github_lyft_protoc_gen_star//:protoc-gen-star", + "@com_github_lyft_protoc_gen_star//lang/go", + "@org_golang_google_protobuf//types/pluginpb", + ], +) diff --git a/cmd/protoc-gen-validate-cpp/main.go b/cmd/protoc-gen-validate-cpp/main.go new file mode 100644 index 000000000..00e6b71d3 --- /dev/null +++ b/cmd/protoc-gen-validate-cpp/main.go @@ -0,0 +1,15 @@ +package main + +import ( + "github.com/envoyproxy/protoc-gen-validate/module" + pgs "github.com/lyft/protoc-gen-star" + "google.golang.org/protobuf/types/pluginpb" +) + +func main() { + optional := uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL) + pgs. + Init(pgs.DebugEnv("DEBUG_PGV"), pgs.SupportedFeatures(&optional)). + RegisterModule(module.ValidatorForLanguage("cc")). + Render() +} diff --git a/cmd/protoc-gen-validate-go/BUILD b/cmd/protoc-gen-validate-go/BUILD new file mode 100644 index 000000000..419306cb2 --- /dev/null +++ b/cmd/protoc-gen-validate-go/BUILD @@ -0,0 +1,29 @@ +load("@bazel_gazelle//:def.bzl", "gazelle") +load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") + +# gazelle:prefix github.com/envoyproxy/protoc-gen-validate +# gazelle:exclude tests +# gazelle:exclude example-workspace +# gazelle:exclude validate/validate.h +# gazelle:go_naming_convention import_alias +gazelle(name = "gazelle") + +go_binary( + name = "protoc-gen-validate-go", + embed = [":protoc-gen-validate-go_lib"], + importpath = "github.com/envoyproxy/protoc-gen-validate/cmd/protoc-gen-validate-go", + visibility = ["//visibility:public"], +) + +go_library( + name = "protoc-gen-validate-go_lib", + srcs = ["main.go"], + importpath = "github.com/envoyproxy/protoc-gen-validate/cmd/protoc-gen-validate-go", + visibility = ["//visibility:private"], + deps = [ + "//module", + "@com_github_lyft_protoc_gen_star//:protoc-gen-star", + "@com_github_lyft_protoc_gen_star//lang/go", + "@org_golang_google_protobuf//types/pluginpb", + ], +) diff --git a/cmd/protoc-gen-validate-go/main.go b/cmd/protoc-gen-validate-go/main.go new file mode 100644 index 000000000..24a49f5b7 --- /dev/null +++ b/cmd/protoc-gen-validate-go/main.go @@ -0,0 +1,17 @@ +package main + +import ( + "github.com/envoyproxy/protoc-gen-validate/module" + pgs "github.com/lyft/protoc-gen-star" + pgsgo "github.com/lyft/protoc-gen-star/lang/go" + "google.golang.org/protobuf/types/pluginpb" +) + +func main() { + optional := uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL) + pgs. + Init(pgs.DebugEnv("DEBUG_PGV"), pgs.SupportedFeatures(&optional)). + RegisterModule(module.ValidatorForLanguage("go")). + RegisterPostProcessor(pgsgo.GoFmt()). + Render() +} diff --git a/cmd/protoc-gen-validate-java/BUILD b/cmd/protoc-gen-validate-java/BUILD new file mode 100644 index 000000000..93feedb64 --- /dev/null +++ b/cmd/protoc-gen-validate-java/BUILD @@ -0,0 +1,29 @@ +load("@bazel_gazelle//:def.bzl", "gazelle") +load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") + +# gazelle:prefix github.com/envoyproxy/protoc-gen-validate +# gazelle:exclude tests +# gazelle:exclude example-workspace +# gazelle:exclude validate/validate.h +# gazelle:go_naming_convention import_alias +gazelle(name = "gazelle") + +go_binary( + name = "protoc-gen-validate-java", + embed = [":protoc-gen-validate-java_lib"], + importpath = "github.com/envoyproxy/protoc-gen-validate/cmd/protoc-gen-validate-java", + visibility = ["//visibility:public"], +) + +go_library( + name = "protoc-gen-validate-java_lib", + srcs = ["main.go"], + importpath = "github.com/envoyproxy/protoc-gen-validate/cmd/protoc-gen-validate-java", + visibility = ["//visibility:private"], + deps = [ + "//module", + "@com_github_lyft_protoc_gen_star//:protoc-gen-star", + "@com_github_lyft_protoc_gen_star//lang/go", + "@org_golang_google_protobuf//types/pluginpb", + ], +) diff --git a/cmd/protoc-gen-validate-java/main.go b/cmd/protoc-gen-validate-java/main.go new file mode 100644 index 000000000..2d71b641b --- /dev/null +++ b/cmd/protoc-gen-validate-java/main.go @@ -0,0 +1,15 @@ +package main + +import ( + "github.com/envoyproxy/protoc-gen-validate/module" + pgs "github.com/lyft/protoc-gen-star" + "google.golang.org/protobuf/types/pluginpb" +) + +func main() { + optional := uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL) + pgs. + Init(pgs.DebugEnv("DEBUG_PGV"), pgs.SupportedFeatures(&optional)). + RegisterModule(module.ValidatorForLanguage("java")). + Render() +} diff --git a/go.mod b/go.mod index 9bd461abc..b1cca2dd4 100644 --- a/go.mod +++ b/go.mod @@ -1,16 +1,23 @@ module github.com/envoyproxy/protoc-gen-validate -go 1.12 +go 1.17 require ( github.com/iancoleman/strcase v0.2.0 github.com/lyft/protoc-gen-star v0.6.1 - github.com/spf13/afero v1.9.2 // indirect golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 golang.org/x/net v0.1.0 golang.org/x/tools v0.2.0 - golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect google.golang.org/protobuf v1.28.1 ) +require ( + github.com/golang/protobuf v1.5.2 // indirect + github.com/spf13/afero v1.9.2 // indirect + golang.org/x/mod v0.6.0 // indirect + golang.org/x/sys v0.1.0 // indirect + golang.org/x/text v0.4.0 // indirect + golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect +) + retract [v0.6.9, v0.6.12] // Published accidentally diff --git a/go.sum b/go.sum index 78dba8ce5..61722da3b 100644 --- a/go.sum +++ b/go.sum @@ -126,10 +126,8 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1 github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= -github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/lyft/protoc-gen-star v0.6.1 h1:erE0rdztuaDq3bpGifD95wfoPrSZc95nGA6tbiNYh6M= github.com/lyft/protoc-gen-star v0.6.1/go.mod h1:TGAoBVkt8w7MPG72TrKIu85MIdXwDuzJYeZuUPFPNwA= @@ -465,7 +463,6 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w= google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/java/pom.xml b/java/pom.xml index 17545913c..e5059b8dc 100644 --- a/java/pom.xml +++ b/java/pom.xml @@ -47,6 +47,7 @@ 1.50.2 4.13.2 3.23.1 + 8 1.8 2.9.6 @@ -73,6 +74,7 @@ maven-compiler-plugin 3.10.1 + ${java.release} ${java.version} ${java.version} -Xlint:unchecked diff --git a/module/validate.go b/module/validate.go index 516ff65d9..61a77d228 100644 --- a/module/validate.go +++ b/module/validate.go @@ -1,12 +1,13 @@ package module 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" pgsgo "github.com/lyft/protoc-gen-star/lang/go" - "path/filepath" - "strings" ) const ( @@ -18,10 +19,18 @@ const ( type Module struct { *pgs.ModuleBase ctx pgsgo.Context + // lang contains the selected language (one of 'cc', 'go', 'java'). + // It is initialized in ValidatorForLanguage. + // If unset, it will be parsed as the 'lang' parameter. + lang string } func Validator() pgs.Module { return &Module{ModuleBase: &pgs.ModuleBase{}} } +func ValidatorForLanguage(lang string) pgs.Module { + return &Module{lang: lang, ModuleBase: &pgs.ModuleBase{}} +} + func (m *Module) InitContext(ctx pgs.BuildContext) { m.ModuleBase.InitContext(ctx) m.ctx = pgsgo.InitContext(ctx.Parameters()) @@ -30,8 +39,15 @@ func (m *Module) InitContext(ctx pgs.BuildContext) { func (m *Module) Name() string { return validatorName } func (m *Module) Execute(targets map[string]pgs.File, pkgs map[string]pgs.Package) []pgs.Artifact { - lang := m.Parameters().Str(langParam) - m.Assert(lang != "", "`lang` parameter must be set") + lang := m.lang + langParamValue := m.Parameters().Str(langParam) + if lang == "" { + lang = langParamValue + m.Assert(lang != "", "`lang` parameter must be set") + } else if langParamValue != "" { + m.Fail("unknown `lang` parameter") + } + module := m.Parameters().Str(moduleParam) // Process file-level templates diff --git a/validate/validate.pb.go b/validate/validate.pb.go index 61e19b95d..825774fe4 100644 --- a/validate/validate.pb.go +++ b/validate/validate.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.26.0 -// protoc v3.17.3 +// protoc-gen-go v1.28.1 +// protoc v3.21.9 // source: validate/validate.proto package validate @@ -94,6 +94,7 @@ type FieldRules struct { Message *MessageRules `protobuf:"bytes,17,opt,name=message" json:"message,omitempty"` // Types that are assignable to Type: + // // *FieldRules_Float // *FieldRules_Double // *FieldRules_Int32 @@ -1966,7 +1967,6 @@ type StringRules struct { // characters may differ from the number of bytes in the string. MaxLen *uint64 `protobuf:"varint,3,opt,name=max_len,json=maxLen" json:"max_len,omitempty"` // LenBytes specifies that this field must be the specified number of bytes - // at a minimum LenBytes *uint64 `protobuf:"varint,20,opt,name=len_bytes,json=lenBytes" json:"len_bytes,omitempty"` // MinBytes specifies that this field must be the specified number of bytes // at a minimum @@ -2000,6 +2000,7 @@ type StringRules struct { // patterns // // Types that are assignable to WellKnown: + // // *StringRules_Email // *StringRules_Hostname // *StringRules_Ip @@ -2371,6 +2372,7 @@ type BytesRules struct { // patterns // // Types that are assignable to WellKnown: + // // *BytesRules_Ip // *BytesRules_Ipv4 // *BytesRules_Ipv6