Skip to content

Commit

Permalink
docs: add documentation on how to query the current oneof in a given …
Browse files Browse the repository at this point in the history
…message (#408)
  • Loading branch information
parthea authored Nov 21, 2023
1 parent 06a5267 commit d89d811
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions docs/fields.rst
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,23 @@ a string (which should match for all fields within the oneof):
s = Song(database_id="e6aa708c7e", name="Little Fugue")
assert "name" in s and "database_id" not in s
To query which ``oneof`` is present in a given message, use ``proto.Message._pb("oneof")``.

Example:

.. code-block:: python
import proto
class Song(proto.Message):
name = proto.Field(proto.STRING, number=1, oneof="identifier")
database_id = proto.Field(proto.STRING, number=2, oneof="identifier")
s = Song(name="Canon in D minor")
assert s._pb.WhichOneof("identifier") == "name"
s = Song(database_id="e6aa708c7e")
assert s._pb.WhichOneof("identifier") == "database_id"
Optional fields
Expand Down

0 comments on commit d89d811

Please sign in to comment.