Skip to content

Commit

Permalink
Revert "Remove gogo support (bufbuild#340)"
Browse files Browse the repository at this point in the history
This reverts commit c7c8be4.
  • Loading branch information
rvolosatovs committed Aug 7, 2020
1 parent beba99e commit 6783039
Show file tree
Hide file tree
Showing 32 changed files with 1,424 additions and 7 deletions.
4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
branch = "master"
name = "golang.org/x/net"

[[constraint]]
name = "github.com/gogo/protobuf"
version = "1.1.1"

[[constraint]]
branch = "master"
name = "github.com/iancoleman/strcase"
53 changes: 48 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,20 @@ GO_IMPORT_SPACES := ${VALIDATE_IMPORT},\
Mgoogle/protobuf/timestamp.proto=github.com/golang/protobuf/ptypes/timestamp,\
Mgoogle/protobuf/wrappers.proto=github.com/golang/protobuf/ptypes/wrappers,\
Mgoogle/protobuf/descriptor.proto=github.com/golang/protobuf/protoc-gen-go/descriptor,\
Mgogoproto/gogo.proto=${PACKAGE}/gogoproto
GO_IMPORT:=$(subst $(space),,$(GO_IMPORT_SPACES))

# protoc-gen-gogo parameters
GOGO_IMPORT_SPACES := ${VALIDATE_IMPORT},\
Mgoogle/protobuf/any.proto=github.com/gogo/protobuf/types,\
Mgoogle/protobuf/duration.proto=github.com/gogo/protobuf/types,\
Mgoogle/protobuf/struct.proto=github.com/gogo/protobuf/types,\
Mgoogle/protobuf/timestamp.proto=github.com/gogo/protobuf/types,\
Mgoogle/protobuf/wrappers.proto=github.com/gogo/protobuf/types,\
Mgoogle/protobuf/descriptor.proto=github.com/gogo/protobuf/types,\
Mgogoproto/gogo.proto=github.com/gogo/protobuf/gogoproto
GOGO_IMPORT:=$(subst $(space),,$(GOGO_IMPORT_SPACES))

.PHONY: build
build: validate/validate.pb.go
# generates the PGV binary and installs it into $$GOPATH/bin
Expand All @@ -35,6 +47,8 @@ gazelle:
buildozer 'replace deps @com_github_golang_protobuf//ptypes:go_default_library_gen @com_github_golang_protobuf//ptypes:go_default_library' '//...:%go_library'
buildozer 'replace deps @io_bazel_rules_go//proto/wkt:duration_go_proto @com_github_golang_protobuf//ptypes/duration:go_default_library' '//...:%go_library'
buildozer 'replace deps @io_bazel_rules_go//proto/wkt:timestamp_go_proto @com_github_golang_protobuf//ptypes/timestamp:go_default_library' '//...:%go_library'
buildozer 'replace deps //vendor/github.com/gogo/protobuf/proto:go_default_library @com_github_gogo_protobuf//proto:go_default_library' '//...:%go_library'
buildozer 'replace deps //vendor/github.com/gogo/protobuf/types:go_default_library @com_github_gogo_protobuf//types:go_default_library' '//...:%go_library'

.PHONY: lint
lint: bin/golint bin/shadow
Expand All @@ -50,45 +64,58 @@ bin/shadow:
bin/golint:
GOBIN=$(shell pwd)/bin go install golang.org/x/lint/golint

bin/gogofast:
go build -o $@ ./vendor/github.com/gogo/protobuf/protoc-gen-gogofast

bin/protoc-gen-go:
GOBIN=$(shell pwd)/bin go install github.com/golang/protobuf/protoc-gen-go

bin/harness:
cd tests && go build -o ../bin/harness ./harness/executor

.PHONY: harness
harness: testcases tests/harness/go/harness.pb.go tests/harness/go/main/go-harness tests/harness/cc/cc-harness bin/harness
harness: testcases tests/harness/go/harness.pb.go tests/harness/gogo/harness.pb.go tests/harness/go/main/go-harness tests/harness/gogo/main/go-harness tests/harness/cc/cc-harness bin/harness
# runs the test harness, validating a series of test cases in all supported languages
./bin/harness -go -cc
./bin/harness -go -gogo -cc

.PHONY: bazel-harness
bazel-harness:
# runs the test harness via bazel
bazel run //tests/harness/executor:executor --incompatible_new_actions_api=false -- -go -cc -java -python
bazel run //tests/harness/executor:executor --incompatible_new_actions_api=false -- -go -gogo -cc -java -python

.PHONY: testcases
testcases: bin/protoc-gen-go
testcases: bin/gogofast bin/protoc-gen-go
# generate the test harness case protos
rm -r tests/harness/cases/go || true
mkdir tests/harness/cases/go
rm -r tests/harness/cases/other_package/go || true
mkdir tests/harness/cases/other_package/go
rm -r tests/harness/cases/gogo || true
mkdir tests/harness/cases/gogo
rm -r tests/harness/cases/other_package/gogo || true
mkdir tests/harness/cases/other_package/gogo
# protoc-gen-go makes us go a package at a time
cd tests/harness/cases/other_package && \
protoc \
-I . \
-I ../../../.. \
--go_out="${GO_IMPORT}:./go" \
--plugin=protoc-gen-gogofast=$(shell pwd)/bin/gogofast \
--plugin=protoc-gen-go=$(shell pwd)/bin/protoc-gen-go \
--gogofast_out="${GOGO_IMPORT}:./gogo" \
--validate_out="lang=go:./go" \
--validate_out="lang=gogo:./gogo" \
./*.proto
cd tests/harness/cases && \
protoc \
-I . \
-I ../../.. \
--go_out="Mtests/harness/cases/other_package/embed.proto=${PACKAGE}/tests/harness/cases/other_package/go,${GO_IMPORT}:./go" \
--plugin=protoc-gen-gogofast=$(shell pwd)/bin/gogofast \
--plugin=protoc-gen-go=$(shell pwd)/bin/protoc-gen-go \
--gogofast_out="Mtests/harness/cases/other_package/embed.proto=${PACKAGE}/tests/harness/cases/other_package/gogo,${GOGO_IMPORT}:./gogo" \
--validate_out="lang=go,Mtests/harness/cases/other_package/embed.proto=${PACKAGE}/tests/harness/cases/other_package/go:./go" \
--validate_out="lang=gogo,Mtests/harness/cases/other_package/embed.proto=${PACKAGE}/tests/harness/cases/other_package/gogo:./gogo" \
./*.proto

tests/harness/go/harness.pb.go: bin/protoc-gen-go
Expand All @@ -97,10 +124,20 @@ tests/harness/go/harness.pb.go: bin/protoc-gen-go
--plugin=protoc-gen-go=$(shell pwd)/bin/protoc-gen-go \
--go_out="${GO_IMPORT}:./go" harness.proto

tests/harness/gogo/harness.pb.go: bin/gogofast
# generates the test harness protos
cd tests/harness && protoc -I . \
--plugin=protoc-gen-gogofast=$(shell pwd)/bin/gogofast \
--gogofast_out="${GOGO_IMPORT}:./gogo" harness.proto

tests/harness/go/main/go-harness:
# generates the go-specific test harness
cd tests && go build -o ./harness/go/main/go-harness ./harness/go/main

tests/harness/gogo/main/go-harness:
# generates the gogo-specific test harness
cd tests && go build -o ./harness/gogo/main/go-harness ./harness/gogo/main

tests/harness/cc/cc-harness: tests/harness/cc/harness.cc
# generates the C++-specific test harness
# use bazel which knows how to pull in the C++ common proto libraries
Expand All @@ -119,11 +156,17 @@ ci: lint bazel testcases bazel-harness build_generation_tests
clean:
(which bazel && bazel clean) || true
rm -f \
bin/gogofast \
bin/protoc-gen-go \
bin/harness \
tests/harness/cc/cc-harness \
tests/harness/go/main/go-harness \
tests/harness/gogo/main/go-harness \
tests/harness/gogo/harness.pb.go \
tests/harness/gogo/harness.pb.go \
tests/harness/go/harness.pb.go
rm -rf \
tests/harness/cases/go \
tests/harness/cases/other_package/go
tests/harness/cases/other_package/go \
tests/harness/cases/gogo \
tests/harness/cases/other_package/gogo \
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ make build

- **`lang`**: specify the target language to generate. Currently, the only supported options are:
- `go`
- `gogo` for [gogo proto](https://github.com/gogo/protobuf) (experimental)
- `cc` for c++ (partially implemented)
- `java`
- `python`
Expand All @@ -105,6 +106,30 @@ All messages generated include the new `Validate() error` method. PGV requires n

**Note**: by default **example.pb.validate.go** is nested in a directory structure that matches your `option go_package` name. You can change this using the protoc parameter `paths=source_relative:.`. Then `--validate_out` will output the file where it is expected. See Google's protobuf documenation or [packages and input paths](https://github.com/golang/protobuf#packages-and-input-paths) or [parameters](https://github.com/golang/protobuf#parameters) for more information.

#### Gogo

There is an experimental support for [gogo
protobuf](https://github.com/gogo/protobuf) plugin for `go`. Use the following
command to generate `gogo`-compatible validation code:

```sh
protoc \
-I . \
-I ${GOPATH}/src \
-I ${GOPATH}/src/github.com/envoyproxy/protoc-gen-validate \
--gogofast_out=":../generated"\
--validate_out="lang=gogo:../generated" \ example.proto
```

Gogo support has the following limitations:
- only `gogofast` plugin is supported and tested, meaning that the fields
should be properly annotated with `gogoproto` annotations;
- `gogoproto.nullable` is supported on fields;
- `gogoproto.stdduration` is supported on fields;
- `gogoproto.stdtime` is supported on fields;

**Note**: by default **example.pb.validate.go** is nested in a directory structure that matches your `option go_package` name. You can change this using the protoc parameter `paths=source_relative:.`. Then `--validate_out` will output the file where it is expected. See Google's protobuf documenation or [packages and input paths](https://github.com/golang/protobuf#packages-and-input-paths) or [parameters](https://github.com/golang/protobuf#parameters) for more information.

#### Java

Java generation is integrated with the existing protobuf toolchain for java projects. For Maven projects, add the
Expand Down
18 changes: 18 additions & 0 deletions bazel/pgv_proto_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@ def pgv_go_proto_library(name, proto = None, deps = [], **kwargs):
**kwargs
)

def pgv_gogo_proto_library(name, proto = None, deps = [], **kwargs):
go_proto_compiler(
name = "pgv_plugin_gogo",
suffix = ".pb.validate.go",
valid_archive = False,
plugin = "//:protoc-gen-validate",
options = ["lang=gogo"],
)

go_proto_library(
name = name,
proto = proto,
deps = ["//validate:go_default_library"] + deps,
compilers = ["@io_bazel_rules_go//proto:gogo_proto", "pgv_plugin_gogo"],
visibility = ["//visibility:public"],
**kwargs
)

def pgv_cc_proto_library(
name,
deps = [],
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/envoyproxy/protoc-gen-validate
go 1.14

require (
github.com/gogo/protobuf v1.3.1
github.com/golang/protobuf v1.3.1
github.com/iancoleman/strcase v0.0.0-20180726023541-3605ed457bf7
github.com/lyft/protoc-gen-star v0.4.10
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gogo/protobuf v1.3.1 h1:DqDEcV5aeaTmdFBePNpYsp3FlcVH/2ISVVM9Qf8PSls=
github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o=
github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/iancoleman/strcase v0.0.0-20180726023541-3605ed457bf7 h1:ux/56T2xqZO/3cP1I2F86qpeoYPCOzk+KF/UH/Ar+lk=
github.com/iancoleman/strcase v0.0.0-20180726023541-3605ed457bf7/go.mod h1:SK73tn/9oHe+/Y0h39VT4UCxmurVJkR5NA7kMEAOgSE=
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/lyft/protoc-gen-star v0.4.10 h1:yekjh68Yp+AF+IXd/0vLd+DQv+eb6joCTA6qxRmtE2A=
github.com/lyft/protoc-gen-star v0.4.10/go.mod h1:mE8fbna26u7aEA2QCVvvfBU/ZrPgocG1206xAFPcs94=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down Expand Up @@ -31,6 +35,7 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
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/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/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.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.0.0-20200522201501-cb1345f3a375 h1:SjQ2+AKWgZLc1xej6WSzL+Dfs5Uyd5xcZH1mGC411IA=
Expand Down
24 changes: 24 additions & 0 deletions gogoproto/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
load("@rules_proto//proto:defs.bzl", "proto_library")

go_library(
name = "go_default_library",
embed = [":gogoproto_go_proto"],
importpath = "github.com/envoyproxy/protoc-gen-validate/gogoproto",
visibility = ["//visibility:public"],
)

proto_library(
name = "gogoproto_proto",
srcs = ["gogo.proto"],
visibility = ["//visibility:public"],
deps = ["@com_google_protobuf//:descriptor_proto"],
)

go_proto_library(
name = "gogoproto_go_proto",
importpath = "github.com/envoyproxy/protoc-gen-validate/gogoproto",
proto = ":gogoproto_proto",
visibility = ["//visibility:public"],
)
4 changes: 4 additions & 0 deletions gogoproto/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This directory contains a copy of the
[gogoproto](https://github.com/gogo/protobuf) annotations processed using the
regular protoc golang plugin. The annotations are needed to import the protobuf
descriptor declarations into the validation plugin.
Loading

0 comments on commit 6783039

Please sign in to comment.