Skip to content

Commit 81eab5c

Browse files
Switch to new Go protobuf generators
Switch from github.com/golang/protobuf/protoc-gen-go to google.golang.org/protobuf/cmd/protoc-gen-go and google.golang.org/grpc/cmd/protoc-gen-go-grpc. Add APIv2 protoc-gen-go and protoc-gen-go-grpc bazel rules
1 parent f0cca7d commit 81eab5c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+3475
-3342
lines changed

BUILD

+57-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
load("@bazel_gazelle//:def.bzl", "gazelle")
22
load("@com_github_bazelbuild_buildtools//buildifier:def.bzl", "buildifier")
3+
load("@io_bazel_rules_go//proto:compiler.bzl", "go_proto_compiler")
34

45
buildifier(
56
name = "buildifier",
@@ -11,9 +12,10 @@ buildifier(
1112
)
1213

1314
# gazelle:exclude third_party
14-
# gazelle:exclude vendor
1515
# gazelle:exclude _output
1616
# gazelle:prefix github.com/grpc-ecosystem/grpc-gateway/v2
17+
# gazelle:go_proto_compilers //:go_apiv2
18+
# gazelle:go_grpc_compilers //:go_apiv2, //:go_grpc
1719

1820
gazelle(name = "gazelle")
1921

@@ -24,3 +26,57 @@ package_group(
2426
"//protoc-gen-openapiv2/...",
2527
],
2628
)
29+
30+
go_proto_compiler(
31+
name = "go_apiv2",
32+
options = [
33+
"paths=source_relative",
34+
],
35+
plugin = "@org_golang_google_protobuf//cmd/protoc-gen-go",
36+
suffix = ".pb.go",
37+
visibility = ["//visibility:public"],
38+
deps = [
39+
"@com_github_golang_protobuf//proto:go_default_library",
40+
"@io_bazel_rules_go//proto/wkt:any_go_proto",
41+
"@io_bazel_rules_go//proto/wkt:api_go_proto",
42+
"@io_bazel_rules_go//proto/wkt:compiler_plugin_go_proto",
43+
"@io_bazel_rules_go//proto/wkt:descriptor_go_proto",
44+
"@io_bazel_rules_go//proto/wkt:duration_go_proto",
45+
"@io_bazel_rules_go//proto/wkt:empty_go_proto",
46+
"@io_bazel_rules_go//proto/wkt:field_mask_go_proto",
47+
"@io_bazel_rules_go//proto/wkt:source_context_go_proto",
48+
"@io_bazel_rules_go//proto/wkt:struct_go_proto",
49+
"@io_bazel_rules_go//proto/wkt:timestamp_go_proto",
50+
"@io_bazel_rules_go//proto/wkt:type_go_proto",
51+
"@io_bazel_rules_go//proto/wkt:wrappers_go_proto",
52+
"@org_golang_google_protobuf//reflect/protoreflect:go_default_library",
53+
"@org_golang_google_protobuf//runtime/protoimpl:go_default_library",
54+
],
55+
)
56+
57+
go_proto_compiler(
58+
name = "go_grpc",
59+
options = [
60+
"paths=source_relative",
61+
],
62+
plugin = "@org_golang_google_grpc_cmd_protoc_gen_go_grpc//:protoc-gen-go-grpc",
63+
suffix = "_grpc.pb.go",
64+
visibility = ["//visibility:public"],
65+
deps = [
66+
"@io_bazel_rules_go//proto/wkt:any_go_proto",
67+
"@io_bazel_rules_go//proto/wkt:api_go_proto",
68+
"@io_bazel_rules_go//proto/wkt:compiler_plugin_go_proto",
69+
"@io_bazel_rules_go//proto/wkt:descriptor_go_proto",
70+
"@io_bazel_rules_go//proto/wkt:duration_go_proto",
71+
"@io_bazel_rules_go//proto/wkt:empty_go_proto",
72+
"@io_bazel_rules_go//proto/wkt:field_mask_go_proto",
73+
"@io_bazel_rules_go//proto/wkt:source_context_go_proto",
74+
"@io_bazel_rules_go//proto/wkt:struct_go_proto",
75+
"@io_bazel_rules_go//proto/wkt:timestamp_go_proto",
76+
"@io_bazel_rules_go//proto/wkt:type_go_proto",
77+
"@io_bazel_rules_go//proto/wkt:wrappers_go_proto",
78+
"@org_golang_google_grpc//:go_default_library",
79+
"@org_golang_google_grpc//codes:go_default_library",
80+
"@org_golang_google_grpc//status:go_default_library",
81+
],
82+
)

Makefile

+14-11
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
# grpc-gateway itself.
55

66
GO_PLUGIN=bin/protoc-gen-go
7-
GO_PROTOBUF_REPO=github.com/golang/protobuf
8-
GO_PLUGIN_PKG=$(GO_PROTOBUF_REPO)/protoc-gen-go
9-
GO_PTYPES_ANY_PKG=$(GO_PROTOBUF_REPO)/ptypes/any
7+
GO_PLUGIN_PKG=google.golang.org/protobuf/cmd/protoc-gen-go
8+
GO_GRPC_PLUGIN=bin/protoc-gen-go-grpc
9+
GO_GRPC_PLUGIN_PKG=google.golang.org/grpc/cmd/protoc-gen-go-grpc
1010
OPENAPI_PLUGIN=bin/protoc-gen-openapiv2
1111
OPENAPI_PLUGIN_SRC= ./utilities/doc.go \
1212
./utilities/pattern.go \
@@ -147,6 +147,9 @@ PROTOC_INC_PATH=$(dir $(shell which protoc))/../include
147147
$(GO_PLUGIN):
148148
go build -o $(GO_PLUGIN) $(GO_PLUGIN_PKG)
149149

150+
$(GO_GRPC_PLUGIN):
151+
go build -o $(GO_GRPC_PLUGIN) $(GO_GRPC_PLUGIN_PKG)
152+
150153
$(OPENAPIV2_GO): $(OPENAPIV2_PROTO) $(GO_PLUGIN)
151154
protoc -I $(PROTOC_INC_PATH) --plugin=$(GO_PLUGIN) -I. --go_out=paths=source_relative:. $(OPENAPIV2_PROTO)
152155

@@ -156,15 +159,15 @@ $(GATEWAY_PLUGIN): $(GATEWAY_PLUGIN_SRC)
156159
$(OPENAPI_PLUGIN): $(OPENAPI_PLUGIN_SRC) $(OPENAPIV2_GO)
157160
go build -o $@ $(OPENAPI_PLUGIN_PKG)
158161

159-
$(EXAMPLE_SVCSRCS): $(GO_PLUGIN) $(EXAMPLES)
160-
protoc -I $(PROTOC_INC_PATH) -I. -I$(GOOGLEAPIS_DIR) --plugin=$(GO_PLUGIN) --go_out=plugins=grpc,paths=source_relative:. $(EXAMPLES)
161-
$(EXAMPLE_DEPSRCS): $(GO_PLUGIN) $(EXAMPLE_DEPS)
162+
$(EXAMPLE_SVCSRCS): $(GO_PLUGIN) $(GO_GRPC_PLUGIN) $(EXAMPLES)
163+
protoc -I $(PROTOC_INC_PATH) -I. -I$(GOOGLEAPIS_DIR) --plugin=$(GO_PLUGIN) --plugin=$(GO_GRPC_PLUGIN) --go_out=paths=source_relative:. --go-grpc_out=paths=source_relative:. $(EXAMPLES)
164+
$(EXAMPLE_DEPSRCS): $(GO_PLUGIN) $(GO_GRPC_PLUGIN) $(EXAMPLE_DEPS)
162165
mkdir -p $(OUTPUT_DIR)
163-
protoc -I $(PROTOC_INC_PATH) -I. --plugin=$(GO_PLUGIN) --go_out=plugins=grpc,paths=source_relative:$(OUTPUT_DIR) $(@:.pb.go=.proto)
166+
protoc -I $(PROTOC_INC_PATH) -I. --plugin=$(GO_PLUGIN) --plugin=$(GO_GRPC_PLUGIN) --go_out=paths=source_relative:$(OUTPUT_DIR) --go-grpc_out=paths=source_relative:$(OUTPUT_DIR) $(@:.pb.go=.proto)
164167
cp $(OUTPUT_DIR)/$@ $@ || cp $(OUTPUT_DIR)/$@ $@
165168

166-
$(RUNTIME_TEST_SRCS): $(GO_PLUGIN) $(RUNTIME_TEST_PROTO)
167-
protoc -I $(PROTOC_INC_PATH) -I. -I$(GOOGLEAPIS_DIR) --plugin=$(GO_PLUGIN) --go_out=plugins=grpc,paths=source_relative:. $(RUNTIME_TEST_PROTO)
169+
$(RUNTIME_TEST_SRCS): $(GO_PLUGIN) $(GO_GRPC_PLUGIN) $(RUNTIME_TEST_PROTO)
170+
protoc -I $(PROTOC_INC_PATH) -I. -I$(GOOGLEAPIS_DIR) --plugin=$(GO_PLUGIN) --plugin=$(GO_GRPC_PLUGIN) --go_out=paths=source_relative:. --go-grpc_out=paths=source_relative:. $(RUNTIME_TEST_PROTO)
168171

169172
$(APICONFIG_SRCS): $(GO_PLUGIN) $(APICONFIG_PROTO)
170173
protoc -I $(PROTOC_INC_PATH) -I. -I$(GOOGLEAPIS_DIR) --plugin=$(GO_PLUGIN) --go_out=paths=source_relative:. $(APICONFIG_PROTO)
@@ -181,8 +184,8 @@ $(EXAMPLE_OPENAPISRCS): ADDITIONAL_SWG_FLAGS:=$(ADDITIONAL_SWG_FLAGS),grpc_api_c
181184
$(EXAMPLE_OPENAPISRCS): $(OPENAPI_PLUGIN) $(OPENAPI_EXAMPLES)
182185
protoc -I $(PROTOC_INC_PATH) -I. -I$(GOOGLEAPIS_DIR) --plugin=$(OPENAPI_PLUGIN) --openapiv2_out=logtostderr=true,allow_repeated_fields_in_body=true,use_go_templates=true$(ADDITIONAL_SWG_FLAGS):. $(OPENAPI_EXAMPLES)
183186

184-
$(HELLOWORLD_SVCSRCS): $(GO_PLUGIN) $(HELLOWORLD)
185-
protoc -I $(PROTOC_INC_PATH) -I. -I$(GOOGLEAPIS_DIR) --plugin=$(GO_PLUGIN) --go_out=plugins=grpc,paths=source_relative:. $(HELLOWORLD)
187+
$(HELLOWORLD_SVCSRCS): $(GO_PLUGIN) $(GO_GRPC_PLUGIN) $(HELLOWORLD)
188+
protoc -I $(PROTOC_INC_PATH) -I. -I$(GOOGLEAPIS_DIR) --plugin=$(GO_PLUGIN) --plugin=$(GO_GRPC_PLUGIN) --go_out=paths=source_relative:. --go-grpc_out=paths=source_relative:. $(HELLOWORLD)
186189

187190
$(HELLOWORLD_GWSRCS):
188191
$(HELLOWORLD_GWSRCS): $(GATEWAY_PLUGIN) $(HELLOWORLD)

bin/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/protoc-gen-go
2+
/protoc-gen-go-grpc
23
/protoc-gen-grpc-gateway
34
/protoc-gen-openapiv2

examples/internal/helloworld/BUILD.bazel

+14-13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ load("@rules_proto//proto:defs.bzl", "proto_library")
22
load("@io_bazel_rules_go//go:def.bzl", "go_library")
33
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
44

5+
# gazelle:exclude helloworld.pb.gw.go
6+
# gazelle:exclude helloworld_grpc.pb.go
7+
# gazelle:go_grpc_compilers //:go_apiv2, //:go_grpc, //protoc-gen-grpc-gateway:go_gen_grpc_gateway
8+
59
proto_library(
610
name = "helloworld_proto",
711
srcs = ["helloworld.proto"],
@@ -14,26 +18,23 @@ proto_library(
1418

1519
go_proto_library(
1620
name = "helloworld_go_proto",
17-
compilers = ["@io_bazel_rules_go//proto:go_grpc"],
21+
compilers = [
22+
"//:go_apiv2",
23+
"//:go_grpc",
24+
"//protoc-gen-grpc-gateway:go_gen_grpc_gateway",
25+
],
1826
importpath = "github.com/grpc-ecosystem/grpc-gateway/v2/examples/internal/helloworld",
1927
proto = ":helloworld_proto",
2028
visibility = ["//visibility:public"],
21-
deps = ["@go_googleapis//google/api:annotations_go_proto"],
29+
deps = [
30+
"@go_googleapis//google/api:annotations_go_proto",
31+
"@org_golang_google_protobuf//proto:go_default_library", #keep
32+
],
2233
)
2334

2435
go_library(
2536
name = "go_default_library",
26-
srcs = ["helloworld.pb.gw.go"],
2737
embed = [":helloworld_go_proto"],
2838
importpath = "github.com/grpc-ecosystem/grpc-gateway/v2/examples/internal/helloworld",
29-
visibility = ["//visibility:public"],
30-
deps = [
31-
"//runtime:go_default_library",
32-
"//utilities:go_default_library",
33-
"@org_golang_google_grpc//:go_default_library",
34-
"@org_golang_google_grpc//codes:go_default_library",
35-
"@org_golang_google_grpc//grpclog:go_default_library",
36-
"@org_golang_google_grpc//status:go_default_library",
37-
"@org_golang_google_protobuf//proto:go_default_library",
38-
],
39+
visibility = ["//examples:__subpackages__"],
3940
)

examples/internal/helloworld/helloworld.pb.go

-84
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/internal/helloworld/helloworld_grpc.pb.go

+86
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)