Skip to content

Commit 6b0e1ae

Browse files
committed
Adds example of importing protobuf well-known-types in golang.
- Partially addresses issue #22.
1 parent 8136407 commit 6b0e1ae

File tree

6 files changed

+150
-0
lines changed

6 files changed

+150
-0
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ script:
3535
examples/helloworld/go/... \
3636
examples/helloworld/java/... \
3737
examples/helloworld/closure/... \
38+
examples/wkt/... \
3839
$FLAGS
3940
4041
notifications:

examples/wkt/go/BUILD

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_test")
4+
5+
load("//go:rules.bzl", "go_proto_library")
6+
7+
#
8+
# Demonstration of importing the protobuf well-known types.
9+
#
10+
11+
go_proto_library(
12+
name = "protolib",
13+
protos = ["wkt.proto"],
14+
imports = [
15+
'external/com_github_google_protobuf/src',
16+
],
17+
importmap = {
18+
'google/protobuf/descriptor.proto': 'github.com/golang/protobuf/protoc-gen-go/descriptor',
19+
'google/protobuf/compiler/plugin.proto': 'github.com/golang/protobuf/protoc-gen-go/plugin',
20+
},
21+
deps = [
22+
"@com_github_golang_protobuf//:ptypes/any",
23+
"@com_github_golang_protobuf//:ptypes/duration",
24+
"@com_github_golang_protobuf//:ptypes/timestamp",
25+
"@com_github_golang_protobuf//:ptypes/empty",
26+
"@com_github_golang_protobuf//:ptypes/struct",
27+
"@com_github_golang_protobuf//:ptypes/wrappers",
28+
"@com_github_golang_protobuf//:protoc-gen-go/descriptor",
29+
"@com_github_golang_protobuf//:protoc-gen-go/plugin",
30+
],
31+
)
32+
33+
go_test(
34+
name = "wkt_test",
35+
size = "small",
36+
srcs = ["wkt_test.go"],
37+
deps = [
38+
"protolib",
39+
"@com_github_golang_protobuf//:protoc-gen-go/descriptor",
40+
],
41+
)

examples/wkt/go/wkt.proto

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
syntax = "proto3";
2+
3+
import "google/protobuf/any.proto";
4+
import "google/protobuf/duration.proto";
5+
import "google/protobuf/timestamp.proto";
6+
import "google/protobuf/empty.proto";
7+
import "google/protobuf/struct.proto";
8+
import "google/protobuf/wrappers.proto";
9+
import "google/protobuf/descriptor.proto";
10+
import "google/protobuf/compiler/plugin.proto";
11+
12+
message Message {
13+
string id = 1;
14+
google.protobuf.FileDescriptorProto file = 2;
15+
google.protobuf.Any any = 3;
16+
google.protobuf.Duration duration = 4;
17+
google.protobuf.Timestamp ts = 5;
18+
google.protobuf.Empty empty = 6;
19+
google.protobuf.Struct struct = 7;
20+
google.protobuf.BytesValue bytes_wrapper = 8;
21+
google.protobuf.compiler.CodeGeneratorRequest code_generator_request = 9;
22+
}
23+
24+
extend google.protobuf.MessageOptions {
25+
string my_option = 10001;
26+
}

examples/wkt/go/wkt_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package wkt_test
2+
3+
import (
4+
"testing"
5+
wkt "github.com/pubref/rules_protobuf/examples/wkt/go/protolib"
6+
fd "github.com/golang/protobuf/protoc-gen-go/descriptor"
7+
)
8+
9+
10+
func TestWkt(t *testing.T) {
11+
filename := "wkt.proto"
12+
13+
message := &wkt.Message{
14+
Id: "foo",
15+
File: &fd.FileDescriptorProto{
16+
Name: &filename,
17+
},
18+
}
19+
20+
if message.Id != "foo" {
21+
t.Error("Expected foo, got ", message.Id)
22+
}
23+
24+
if *message.File.Name != filename {
25+
t.Error("Expected %s, got %s", filename, message.File.Name)
26+
}
27+
}

go/README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,56 @@ When this name is chosen, part 3 is not needed and should be omitted.
134134
Consult source files in the
135135
[examples/helloworld/go](../examples/helloworld/go) directory
136136
for additional information.
137+
138+
---
139+
140+
## Well-Known Protobuf Import Mappings
141+
142+
This demonstrates one way to incorporate the so-called *well-known*
143+
protos in your proto definitions. Protoc will look to find the proto
144+
file in the `google/protobuf/src` directory in the external workspace.
145+
The `protoc-gen-go` plugin then performs the importmapping according
146+
to the `importmap` attribute. Pregenerated protobuf dependencies are
147+
provided by the `@com_github_golang_protobuf` workspace. A minimally
148+
functional example is provided in
149+
[examples/wkt/go](https://github.com/pubref/rules_protobuf/tree/master/examples/wkt/go).
150+
151+
```c
152+
// foo.proto
153+
syntax = "proto3";
154+
import "{A}";
155+
...
156+
```
157+
158+
```python
159+
# BUILD
160+
go_proto_library(
161+
name = "protolib",
162+
protos = ["foo.proto"],
163+
importmap = {
164+
"{A}": "github.com/golang/protobuf/{B}",
165+
},
166+
imports = ["external/com_github_google_protobuf/src"],
167+
deps = [
168+
"@com_github_golang_protobuf//:{B}",
169+
],
170+
)
171+
```
172+
173+
```go
174+
// foo.go
175+
import (
176+
"github.com/golang/protobuf/{B}"
177+
)
178+
```
179+
180+
| Proto Filename (A) | Import Name Suffix and Target Name (B) |
181+
| --- | --- | ----- |
182+
| `google/protobuf/any.proto` | `ptypes/any` |
183+
| `google/protobuf/duration.proto` | `ptypes/duration` |
184+
| `google/protobuf/timestamp.proto` | `ptypes/timestamp` |
185+
| `google/protobuf/empty.proto` | `ptypes/empty` |
186+
| `google/protobuf/struct.proto` | `ptypes/struct` |
187+
| `google/protobuf/wrappers.proto` | `ptypes/wrappers` |
188+
| `google/protobuf/descriptor.proto` | `protoc-gen-go/descriptor` |
189+
| `google/protobuf/compiler/plugin.proto` | `protoc-gen-go/plugin` |

go/rules.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def go_proto_library(
3131
name,
3232
langs = [str(Label("//go"))],
3333
protos = [],
34+
importmap = {},
3435
imports = [],
3536
inputs = [],
3637
proto_deps = [],
@@ -62,6 +63,7 @@ def go_proto_library(
6263
"protos": protos,
6364
"deps": [dep + ".pb" for dep in proto_deps],
6465
"langs": langs,
66+
"importmap": importmap,
6567
"imports": imports,
6668
"inputs": inputs,
6769
"pb_options": pb_options,

0 commit comments

Comments
 (0)