Skip to content

Commit

Permalink
Adds example of importing protobuf well-known-types in golang.
Browse files Browse the repository at this point in the history
- Partially addresses issue #22.
  • Loading branch information
pcj committed Oct 5, 2016
1 parent 8136407 commit 6b0e1ae
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 0 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ script:
examples/helloworld/go/... \
examples/helloworld/java/... \
examples/helloworld/closure/... \
examples/wkt/... \
$FLAGS
notifications:
Expand Down
41 changes: 41 additions & 0 deletions examples/wkt/go/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package(default_visibility = ["//visibility:public"])

load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_test")

load("//go:rules.bzl", "go_proto_library")

#
# Demonstration of importing the protobuf well-known types.
#

go_proto_library(
name = "protolib",
protos = ["wkt.proto"],
imports = [
'external/com_github_google_protobuf/src',
],
importmap = {
'google/protobuf/descriptor.proto': 'github.com/golang/protobuf/protoc-gen-go/descriptor',
'google/protobuf/compiler/plugin.proto': 'github.com/golang/protobuf/protoc-gen-go/plugin',
},
deps = [
"@com_github_golang_protobuf//:ptypes/any",
"@com_github_golang_protobuf//:ptypes/duration",
"@com_github_golang_protobuf//:ptypes/timestamp",
"@com_github_golang_protobuf//:ptypes/empty",
"@com_github_golang_protobuf//:ptypes/struct",
"@com_github_golang_protobuf//:ptypes/wrappers",
"@com_github_golang_protobuf//:protoc-gen-go/descriptor",
"@com_github_golang_protobuf//:protoc-gen-go/plugin",
],
)

go_test(
name = "wkt_test",
size = "small",
srcs = ["wkt_test.go"],
deps = [
"protolib",
"@com_github_golang_protobuf//:protoc-gen-go/descriptor",
],
)
26 changes: 26 additions & 0 deletions examples/wkt/go/wkt.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
syntax = "proto3";

import "google/protobuf/any.proto";
import "google/protobuf/duration.proto";
import "google/protobuf/timestamp.proto";
import "google/protobuf/empty.proto";
import "google/protobuf/struct.proto";
import "google/protobuf/wrappers.proto";
import "google/protobuf/descriptor.proto";
import "google/protobuf/compiler/plugin.proto";

message Message {
string id = 1;
google.protobuf.FileDescriptorProto file = 2;
google.protobuf.Any any = 3;
google.protobuf.Duration duration = 4;
google.protobuf.Timestamp ts = 5;
google.protobuf.Empty empty = 6;
google.protobuf.Struct struct = 7;
google.protobuf.BytesValue bytes_wrapper = 8;
google.protobuf.compiler.CodeGeneratorRequest code_generator_request = 9;
}

extend google.protobuf.MessageOptions {
string my_option = 10001;
}
27 changes: 27 additions & 0 deletions examples/wkt/go/wkt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package wkt_test

import (
"testing"
wkt "github.com/pubref/rules_protobuf/examples/wkt/go/protolib"
fd "github.com/golang/protobuf/protoc-gen-go/descriptor"
)


func TestWkt(t *testing.T) {
filename := "wkt.proto"

message := &wkt.Message{
Id: "foo",
File: &fd.FileDescriptorProto{
Name: &filename,
},
}

if message.Id != "foo" {
t.Error("Expected foo, got ", message.Id)
}

if *message.File.Name != filename {
t.Error("Expected %s, got %s", filename, message.File.Name)
}
}
53 changes: 53 additions & 0 deletions go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,56 @@ When this name is chosen, part 3 is not needed and should be omitted.
Consult source files in the
[examples/helloworld/go](../examples/helloworld/go) directory
for additional information.

---

## Well-Known Protobuf Import Mappings

This demonstrates one way to incorporate the so-called *well-known*
protos in your proto definitions. Protoc will look to find the proto
file in the `google/protobuf/src` directory in the external workspace.
The `protoc-gen-go` plugin then performs the importmapping according
to the `importmap` attribute. Pregenerated protobuf dependencies are
provided by the `@com_github_golang_protobuf` workspace. A minimally
functional example is provided in
[examples/wkt/go](https://github.com/pubref/rules_protobuf/tree/master/examples/wkt/go).

```c
// foo.proto
syntax = "proto3";
import "{A}";
...
```

```python
# BUILD
go_proto_library(
name = "protolib",
protos = ["foo.proto"],
importmap = {
"{A}": "github.com/golang/protobuf/{B}",
},
imports = ["external/com_github_google_protobuf/src"],
deps = [
"@com_github_golang_protobuf//:{B}",
],
)
```

```go
// foo.go
import (
"github.com/golang/protobuf/{B}"
)
```

| Proto Filename (A) | Import Name Suffix and Target Name (B) |
| --- | --- | ----- |
| `google/protobuf/any.proto` | `ptypes/any` |
| `google/protobuf/duration.proto` | `ptypes/duration` |
| `google/protobuf/timestamp.proto` | `ptypes/timestamp` |
| `google/protobuf/empty.proto` | `ptypes/empty` |
| `google/protobuf/struct.proto` | `ptypes/struct` |
| `google/protobuf/wrappers.proto` | `ptypes/wrappers` |
| `google/protobuf/descriptor.proto` | `protoc-gen-go/descriptor` |
| `google/protobuf/compiler/plugin.proto` | `protoc-gen-go/plugin` |
2 changes: 2 additions & 0 deletions go/rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def go_proto_library(
name,
langs = [str(Label("//go"))],
protos = [],
importmap = {},
imports = [],
inputs = [],
proto_deps = [],
Expand Down Expand Up @@ -62,6 +63,7 @@ def go_proto_library(
"protos": protos,
"deps": [dep + ".pb" for dep in proto_deps],
"langs": langs,
"importmap": importmap,
"imports": imports,
"inputs": inputs,
"pb_options": pb_options,
Expand Down

0 comments on commit 6b0e1ae

Please sign in to comment.