-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds example of importing protobuf well-known-types in golang.
- Partially addresses issue #22.
- Loading branch information
Showing
6 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters