Support wrapper types
This commit is contained in:
5
betterproto/tests/googletypes.json
Normal file
5
betterproto/tests/googletypes.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"maybe": false,
|
||||
"ts": "1972-01-01T10:00:20.021Z",
|
||||
"duration": "1.200s"
|
||||
}
|
||||
12
betterproto/tests/googletypes.proto
Normal file
12
betterproto/tests/googletypes.proto
Normal file
@@ -0,0 +1,12 @@
|
||||
syntax = "proto3";
|
||||
|
||||
import "google/protobuf/duration.proto";
|
||||
import "google/protobuf/timestamp.proto";
|
||||
import "google/protobuf/wrappers.proto";
|
||||
|
||||
message Test {
|
||||
google.protobuf.BoolValue maybe = 1;
|
||||
google.protobuf.Timestamp ts = 2;
|
||||
google.protobuf.Duration duration = 3;
|
||||
google.protobuf.Int32Value important = 4;
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import betterproto
|
||||
from dataclasses import dataclass
|
||||
from typing import Optional
|
||||
|
||||
|
||||
def test_has_field():
|
||||
@@ -146,3 +147,18 @@ def test_json_casing():
|
||||
"snake_case": 3,
|
||||
"kabob_case": 4,
|
||||
}
|
||||
|
||||
|
||||
def test_optional_flag():
|
||||
@dataclass
|
||||
class Request(betterproto.Message):
|
||||
flag: Optional[bool] = betterproto.message_field(1, wraps=betterproto.TYPE_BOOL)
|
||||
|
||||
# Serialization of not passed vs. set vs. zero-value.
|
||||
assert bytes(Request()) == b""
|
||||
assert bytes(Request(flag=True)) == b"\n\x02\x08\x01"
|
||||
assert bytes(Request(flag=False)) == b"\n\x00"
|
||||
|
||||
# Differentiate between not passed and the zero-value.
|
||||
assert Request().parse(b"").flag == None
|
||||
assert Request().parse(b"\n\x00").flag == False
|
||||
|
||||
Reference in New Issue
Block a user