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
Document how to use module_pb2.Message.WhichOneof
#137
Comments
@software-dov. Is there a good way to do this? The workaround I used in googleapis/python-monitoring#91 is to convert the message into a from google.cloud import monitoring_v3
typed_value = "double_value"
value_type = monitoring_v3.TypedValue.pb(typed_value).WhichOneof("value") class TypedValue(proto.Message):
r"""A single strongly-typed value.
Attributes:
bool_value (bool):
A Boolean value: ``true`` or ``false``.
int64_value (int):
A 64-bit integer. Its range is approximately
±9.2x10<sup>18</sup>.
double_value (float):
A 64-bit double-precision floating-point
number. Its magnitude is approximately
±10<sup>±300</sup> and it has 16
significant digits of precision.
string_value (str):
A variable-length string value.
distribution_value (~.distribution.Distribution):
A distribution value.
"""
bool_value = proto.Field(proto.BOOL, number=1, oneof="value")
int64_value = proto.Field(proto.INT64, number=2, oneof="value")
double_value = proto.Field(proto.DOUBLE, number=3, oneof="value")
string_value = proto.Field(proto.STRING, number=4, oneof="value")
distribution_value = proto.Field(
proto.MESSAGE, number=5, oneof="value", message=distribution.Distribution,
) |
module_pb2.Message.WhichOneof
module_pb2.Message.WhichOneof
The best way that I've found to do this is, unfortunately, to poke through to the raw message._pb. WhichOneof("field_name") Is there a preferred way to do this? |
For future onlookers: |
The docs for
oneof
fields do not specify how to query which of a given set ofoneof
fields is present in a message.The workaround I have found is to use
"fieldname" in message
, with the proviso that some field names may be aliased (e.g.,"set"
->"set_"
). This workaround isn't adequate for using the selectedoneof
label as a key into a dict, for instance.The text was updated successfully, but these errors were encountered: