Better JSON 64-bit int handling, add way to determine whether a message was sent on the wire, various fixes

This commit is contained in:
Daniel G. Taylor
2019-10-17 23:36:18 -07:00
parent bbceff9341
commit 811b54cabb
11 changed files with 134 additions and 57 deletions

View File

@@ -1,19 +1,21 @@
#!/usr/bin/env python
import importlib
import json
import os # isort: skip
import subprocess
import sys
from typing import Generator, Tuple
from google.protobuf import symbol_database
from google.protobuf.descriptor_pool import DescriptorPool
from google.protobuf.json_format import MessageToJson, Parse
# Force pure-python implementation instead of C++, otherwise imports
# break things because we can't properly reset the symbol database.
os.environ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION"] = "python"
import subprocess
import importlib
import sys
from typing import Generator, Tuple
from google.protobuf.json_format import Parse
from google.protobuf import symbol_database
from google.protobuf.descriptor_pool import DescriptorPool
root = os.path.dirname(os.path.realpath(__file__))
@@ -68,5 +70,10 @@ if __name__ == "__main__":
print(f"Using {parts[0]}_pb2 to generate {os.path.basename(out)}")
imported = importlib.import_module(f"{parts[0]}_pb2")
serialized = Parse(open(filename).read(), imported.Test()).SerializeToString()
parsed = Parse(open(filename).read(), imported.Test())
serialized = parsed.SerializeToString()
serialized_json = MessageToJson(
parsed, preserving_proto_field_name=True, use_integers_for_enums=True
)
assert json.loads(serialized_json) == json.load(open(filename))
open(out, "wb").write(serialized)

View File

@@ -1,5 +1,6 @@
{
"nested": {
"count": 150
}
},
"sibling": {}
}

View File

@@ -10,8 +10,9 @@ message Test {
Nested nested = 1;
Sibling sibling = 2;
Sibling sibling2 = 3;
}
message Sibling {
int32 foo = 1;
}
}

View File

@@ -1,5 +1,5 @@
{
"counts": [1, 2, -1, -2],
"signed": [1, 2, -1, -2],
"signed": ["1", "2", "-1", "-2"],
"fixed": [1.0, 2.7, 3.4]
}

View File

@@ -1,4 +1,4 @@
{
"signed_32": -150,
"signed_64": -150
"signed_64": "-150"
}

View File

@@ -1,4 +1,4 @@
{
"signed_32": 150,
"signed_64": 150
"signed_64": "150"
}

View File

@@ -0,0 +1,32 @@
import betterproto
from dataclasses import dataclass
def test_has_field():
@dataclass
class Bar(betterproto.Message):
baz: int = betterproto.int32_field(1)
@dataclass
class Foo(betterproto.Message):
bar: Bar = betterproto.message_field(1)
# Unset by default
foo = Foo()
assert foo.bar.serialized_on_wire == False
# Serialized after setting something
foo.bar.baz = 1
assert foo.bar.serialized_on_wire == True
# Still has it after setting the default value
foo.bar.baz = 0
assert foo.bar.serialized_on_wire == True
# Manual override
foo.bar.serialized_on_wire = False
assert foo.bar.serialized_on_wire == False
# Can manually set it but defaults to false
foo.bar = Bar()
assert foo.bar.serialized_on_wire == False

View File

@@ -1,8 +1,9 @@
import importlib
import pytest
import json
from .generate import get_files, get_base
import pytest
from .generate import get_base, get_files
inputs = get_files(".bin")