Skip to content

Commit

Permalink
feat: add protobuf into z_bytes example (#333)
Browse files Browse the repository at this point in the history
  • Loading branch information
wyfo authored Sep 19, 2024
1 parent b51f3c6 commit fb432da
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
6 changes: 6 additions & 0 deletions examples/entity.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
syntax = "proto3";

message Entity {
uint32 id = 1;
string name = 2;
}
19 changes: 16 additions & 3 deletions examples/z_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,22 @@ def main():
# Corresponding encoding to be used in operations like `.put()`, `.reply()`, etc.
# encoding = Encoding.APPLICATION_JSON;

# Other formats like protobuf can be used the same way as JSON, i.e. dumps to
# bytes/str before serializing to ZBytes, and loads from ZBytes after deserializing
# to str/bytes.
# Protobuf
try:
import entity_pb2

input = entity_pb2.Entity(id=1234, name="John Doe")
payload = ZBytes.serialize(input.SerializeToString())
output = entity_pb2.Entity()
output.ParseFromString(payload.deserialize(bytes))
assert input == output
# Corresponding encoding to be used in operations like `.put()`, `.reply()`, etc.
# encoding = Encoding.APPLICATION_PROTOBUF;
except ImportError:
# You must install protobuf and generate the protobuf classes from the schema with
# $ pip install protobuf
# $ protoc --python_out=. --pyi_out=. examples/entity.proto
pass

# arbitrary type
import struct
Expand Down

0 comments on commit fb432da

Please sign in to comment.