Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add protobuf into z_bytes example #333

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading