You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Removed the parts of the example that showed accessing an unset value, as it now raises an `AttributeError`, and added an example of the `match` way of accessing the attributes.
Related to #510 and #358.
Copy file name to clipboardExpand all lines: README.md
+16-7Lines changed: 16 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -277,7 +277,22 @@ message Test {
277
277
}
278
278
```
279
279
280
-
You can use `betterproto.which_one_of(message, group_name)` to determine which of the fields was set. It returns a tuple of the field name and value, or a blank string and `None` if unset.
280
+
On Python 3.10 and later, you can use a `match` statement to access the provided one-of field, which supports type-checking:
281
+
282
+
```py
283
+
test = Test()
284
+
match test:
285
+
case Test(on=value):
286
+
print(value) # value: bool
287
+
case Test(count=value):
288
+
print(value) # value: int
289
+
case Test(name=value):
290
+
print(value) # value: str
291
+
case _:
292
+
print("No value provided")
293
+
```
294
+
295
+
You can also use `betterproto.which_one_of(message, group_name)` to determine which of the fields was set. It returns a tuple of the field name and value, or a blank string and `None` if unset.
281
296
282
297
```py
283
298
>>> test = Test()
@@ -292,17 +307,11 @@ You can use `betterproto.which_one_of(message, group_name)` to determine which o
292
307
>>> test.count =57
293
308
>>> betterproto.which_one_of(test, "foo")
294
309
["count", 57]
295
-
>>> test.on
296
-
False
297
310
298
311
# Default (zero) values also work.
299
312
>>> test.name =""
300
313
>>> betterproto.which_one_of(test, "foo")
301
314
["name", ""]
302
-
>>> test.count
303
-
0
304
-
>>> test.on
305
-
False
306
315
```
307
316
308
317
Again this is a little different than the official Google code generator:
0 commit comments