Skip to content

Commit a1169f1

Browse files
authored
doc: add a py_proto_library example with well-known-types (bazel-contrib#1571)
This behaviour should be in the latest release (0.27.0), so just documenting it for the future. Fixes bazel-contrib#1005
1 parent bc5a0b0 commit a1169f1

4 files changed

Lines changed: 12 additions & 1 deletion

File tree

examples/py_proto_library/MODULE.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ use_repo(python, "python_3_9")
2121

2222
# We are using rules_proto to define rules_proto targets to be consumed by py_proto_library.
2323
bazel_dep(name = "rules_proto", version = "5.3.0-21.7")
24+
25+
# Add the protobuf library for well-known types (e.g. `Any`, `Timestamp`, etc)
26+
bazel_dep(name = "protobuf", version = "21.7", repo_name = "com_google_protobuf")

examples/py_proto_library/example.com/proto/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ proto_library(
1313
# https://bazel.build/reference/be/protocol-buffer#proto_library.strip_import_prefix
1414
strip_import_prefix = "/example.com",
1515
visibility = ["//visibility:public"],
16+
deps = ["@com_google_protobuf//:any_proto"],
1617
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
syntax = "proto3";
22

3+
import "google/protobuf/any.proto";
4+
35
package rules_python;
46

57
message PriceTag {
68
string name = 2;
79
double cost = 1;
10+
google.protobuf.Any metadata = 3;
811
}

examples/py_proto_library/test.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import sys
1+
import json
22
import unittest
33

44
from proto import pricetag_pb2
@@ -10,6 +10,10 @@ def test_pricetag(self):
1010
name="dollar",
1111
cost=5.00,
1212
)
13+
14+
metadata = {"description": "some text..."}
15+
got.metadata.value = json.dumps(metadata).encode("utf-8")
16+
1317
self.assertIsNotNone(got)
1418

1519

0 commit comments

Comments
 (0)