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

Document how to use module_pb2.Message.WhichOneof #137

Closed
tseaver opened this issue Sep 25, 2020 · 3 comments · Fixed by #408
Closed

Document how to use module_pb2.Message.WhichOneof #137

tseaver opened this issue Sep 25, 2020 · 3 comments · Fixed by #408
Labels
type: docs Improvement to the documentation for an API.

Comments

@tseaver
Copy link
Contributor

tseaver commented Sep 25, 2020

The docs for oneof fields do not specify how to query which of a given set of oneof 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 selected oneof label as a key into a dict, for instance.

@tseaver tseaver added the type: docs Improvement to the documentation for an API. label Sep 25, 2020
@busunkim96
Copy link
Contributor

@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 pb. However, this has the same issue that it won't work if any field names are aliased.

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
            &plusmn;9.2x10<sup>18</sup>.
        double_value (float):
            A 64-bit double-precision floating-point
            number. Its magnitude is approximately
            &plusmn;10<sup>&plusmn;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,
    )

@vchudnov-g vchudnov-g changed the title Document upgrade path for module_pb2.Message.WhichOneof Document how to use module_pb2.Message.WhichOneof Apr 27, 2023
@achew22
Copy link

achew22 commented Jul 11, 2023

The best way that I've found to do this is, unfortunately, to poke through to the raw _pb.

message._pb. WhichOneof("field_name")

Is there a preferred way to do this?

@dpowley
Copy link

dpowley commented Jul 31, 2023

The best way that I've found to do this is, unfortunately, to poke through to the raw _pb.

message._pb.WhichOneOf("field_name")

Is there a preferred way to do this?

For future onlookers: message._pb.WhichOneof("field_name"). Note for some reason WhichOneof is not actually PascalCase

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: docs Improvement to the documentation for an API.
Projects
None yet
4 participants