Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gogo support #66

Merged
merged 16 commits into from
Mar 27, 2018
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
11 changes: 9 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@ bazel-*

!vendor/*

/gogofast

/tests/harness/cases/go
/tests/harness/cases/gogo
/tests/harness/cases/other_package/go
/tests/harness/harness.pb.go
/tests/harness/go/go-harness
/tests/harness/cases/other_package/gogo
/tests/harness/go/harness.pb.go
/tests/harness/go/main/go-harness
/tests/harness/gogo/harness.pb.go
/tests/harness/gogo/main/go-harness
/tests/harness/cc/cc-harness

/tests/kitchensink/go/
/tests/kitchensink/gogo/
14 changes: 13 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@
[[constraint]]
branch = "master"
name = "golang.org/x/net"

[[constraint]]
name = "github.com/gogo/protobuf"
version = "0.5.0"
78 changes: 67 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
empty :=
space := $(empty) $(empty)
PACKAGE := github.com/lyft/protoc-gen-validate

# protoc-gen-go parameters for properly generating the import path for PGV
VALIDATE_IMPORT="Mvalidate/validate.proto=github.com/lyft/protoc-gen-validate/validate"
VALIDATE_IMPORT := Mvalidate/validate.proto=${PACKAGE}/validate
GO_IMPORT_SPACES := ${VALIDATE_IMPORT},\
Mgoogle/protobuf/any.proto=github.com/golang/protobuf/ptypes/any,\
Mgoogle/protobuf/duration.proto=github.com/golang/protobuf/ptypes/duration,\
Mgoogle/protobuf/struct.proto=github.com/golang/protobuf/ptypes/struct,\
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
Expand Down Expand Up @@ -44,8 +68,11 @@ cover:
go tool cover -html cover.out -o cover.html
open cover.html

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

.PHONY: harness
harness: tests/harness/harness.pb.go tests/harness/go/go-harness tests/harness/cc/cc-harness
harness: 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
# runs the test harness, validating a series of test cases in all supported languages
go run ./tests/harness/executor/*.go

Expand All @@ -55,40 +82,56 @@ bazel-harness:
bazel run //tests/harness/executor:executor

.PHONY: kitchensink
kitchensink:
kitchensink: gogofast
# generates the kitchensink test protos
rm -r tests/kitchensink/go || true
mkdir -p tests/kitchensink/go
rm -r tests/kitchensink/gogo || true
mkdir -p tests/kitchensink/gogo
cd tests/kitchensink && \
protoc \
-I . \
-I ../.. \
--go_out="${VALIDATE_IMPORT}:./go" \
--go_out="${GO_IMPORT}:./go" \
--validate_out="lang=go:./go" \
--plugin=protoc-gen-gogofast=$(shell pwd)/gogofast \
--gogofast_out="${GOGO_IMPORT}:./gogo" \
--validate_out="lang=gogo:./gogo" \
`find . -name "*.proto"`
cd tests/kitchensink/go && go build .
cd tests/kitchensink/gogo && go build .

.PHONY: testcases
testcases:
testcases: gogofast
# 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="${VALIDATE_IMPORT}:./go" \
--go_out="${GO_IMPORT}:./go" \
--plugin=protoc-gen-gogofast=$(shell pwd)/gogofast \
--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=github.com/lyft/protoc-gen-validate/tests/harness/cases/other_package/go,${VALIDATE_IMPORT}:./go" \
--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)/gogofast \
--gogofast_out="Mtests/harness/cases/other_package/embed.proto=${PACKAGE}/tests/harness/cases/other_package/gogo,${GOGO_IMPORT}:./gogo" \
--validate_out="lang=go:./go" \
--validate_out="lang=gogo:./gogo" \
./*.proto

.PHONY: update-vendor
Expand All @@ -97,13 +140,26 @@ update-vendor:
dep ensure -update
$(MAKE) gazelle

tests/harness/harness.pb.go:
tests/harness/go/harness.pb.go:
# generates the test harness protos
cd tests/harness && protoc -I . --go_out=. harness.proto
cd tests/harness && protoc -I . \
--go_out="${GO_IMPORT}:./go" harness.proto

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

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

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

tests/harness/cc/cc-harness: tests/harness/cc/harness.cc
# generates the C++-specific test harness
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,28 @@ protoc \

All messages generated include the new `Validate() error` method. PGV requires no additional runtime dependencies from the existing generated code.

#### 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 \
--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;


## Constraint Rules

[The provided constraints](validate/validate.proto) are modeled largerly after those in JSON Schema. PGV rules can be mixed for the same field; the plugin ensures the rules applied to a field cannot contradict before code generation.
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//proto:def.bzl", "go_proto_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
embed = [":gogoproto_go_proto"],
importpath = "github.com/lyft/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/lyft/protoc-gen-validate/gogoproto",
proto = ":gogoproto_proto",
visibility = ["//visibility:public"],
deps = ["//vendor/github.com/golang/protobuf/protoc-gen-go/descriptor:go_default_library"],
)
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