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

Provide a from_dict method #152

Closed
tswast opened this issue Oct 19, 2020 · 5 comments
Closed

Provide a from_dict method #152

tswast opened this issue Oct 19, 2020 · 5 comments
Assignees

Comments

@tswast
Copy link

tswast commented Oct 19, 2020

Similar to #151, it would be quite useful to be able to parse a message from a JSON-representation dictionary.

Currently, in https://github.com/googleapis/python-bigquery/pull/332/files we need to deserialize the response to a dictionary because of how the handwritten clients are structured, but then immediately re-serialize the response to a JSON string so that we can call from_json.

@software-dov
Copy link
Contributor

Effective duplicate of #151 lumping in under that.

@software-dov
Copy link
Contributor

The message constructor allows just passing in a dict; it already does all the heavy lifting.
E.g.

class Squid(proto.message):
    mass_kg = proto.Field(proto.INT32, number=1)

s = Squid({"mass_kg": 20})

@tswast
Copy link
Author

tswast commented Oct 19, 2020

Does this work with nested message? I recall in the monolith it didn't work the same as the parse from dict method.

@busunkim96
Copy link
Contributor

@tswast It should work. This is what allows request to be passed as a dict to the service methods. This KMS sample has some nesting: https://github.com/googleapis/python-kms/blob/c349e3efa0b6cf5dbb9043043310ef5fac0fccc5/samples/snippets/update_key_add_rotation.py#L43-L57

@Ark-kun
Copy link

Ark-kun commented Dec 5, 2022

The message constructor allows just passing in a dict; it already does all the heavy lifting.

That constructor is incompatible with message.to_dict and the actual REST API payloads. Meanwhile, .from_json is compatible with .to_json

This works:

batch_v1.Job.from_json('{"taskGroups": [{}]}')

This does not work:

batch_v1.Job({"taskGroups": [{}]})
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-145-58ddc394e1ed> in <module>
----> 1 batch_v1.Job({"taskGroups": [{}]})

/opt/conda/lib/python3.7/site-packages/proto/message.py in __init__(self, mapping, ignore_unknown_fields, **kwargs)
    564 
    565                 raise ValueError(
--> 566                     "Unknown field for {}: {}".format(self.__class__.__name__, key)
    567                 )
    568 

ValueError: Unknown field for Job: taskGroups

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants