-
Notifications
You must be signed in to change notification settings - Fork 10
MessageToDict and ParseDict fail on Instance #19
Comments
Hi, proto-plus adds a wrapper around the raw protobuf types. If you want to convert a message to the protobuf dict you can use the |
Thanks @busunkim96 ! Exactly same thing is tried to be done here with |
It looks like the first file is written for the 1.x release of https://proto-plus-python.readthedocs.io/en/latest/messages.html#usage Creating an from google.cloud import memcache
# via kwargs
instance = memcache.Instance(name="projects/project/locations/location/instances/instance")
# from dict
instance = memcache.Instance({"name": "projects/project/locations/location/instances/instance"})
# from a JSON string
instance = memcache.Instance.from_json('{"name":"projects/project/locations/location/instances/instance"}') Converting into other formats import json
from google.cloud import memcache
# to json
instance = memcache.Instance(name="projects/project/locations/location/instances/instance")
instance_json = memcache.Instance.to_json(instance)
# to dict
instance_dict = json.loads(instance_json)
# to bytes
instance_bytes = memcache.Instance.serialize(instance) |
It makes sense. I made helper function which seems to solve my problem with conversion to dictionary: import json
import proto
def proto_message_to_dict(message: proto.Message) -> dict:
"""Helper method to parse protobuf message to dictionary."""
return json.loads(message.__class__.to_json(message)) Thank you for comprehensive answer @busunkim96! |
@TobKed Good to hear! I also opened googleapis/proto-plus-python#151 to see if we can add a |
For anyone looking at this now, there is a
song_dict = Song.to_dict(song)
new_song = Song(song_dict) |
google-cloud-memcache
version: 0.2.0Other relate packages:
The text was updated successfully, but these errors were encountered: