Skip to content

Commit

Permalink
Merge pull request #164 from JoshYuJump/main
Browse files Browse the repository at this point in the history
Fixed ParseDict/MessageToDict utils compatible in protobuf>=3.20
  • Loading branch information
JoshYuJump authored Dec 24, 2022
2 parents 47596f4 + ae4d9bd commit 45c2008
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
2 changes: 1 addition & 1 deletion bali/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from bali.decorators import event_handler, init_handler
from bali.resources import Resource, ModelResource

__version__ = '3.4.0'
__version__ = '3.4.1'


class Schema(BaseModel):
Expand Down
36 changes: 28 additions & 8 deletions bali/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
from enum import Enum
from datetime import datetime, date
from decimal import Decimal
from enum import Enum

import pytz
from google.protobuf import json_format
Expand All @@ -11,17 +11,25 @@


class ProtobufParser(json_format._Parser): # noqa
def _ConvertValueMessage(self, value, message):
def _ConvertValueMessage(self, value, message, path=''):
"""Convert a JSON representation into Value message."""
if isinstance(value, dict):
self._ConvertStructMessage(value, message.struct_value)
try:
self._ConvertStructMessage(value, message.struct_value, path)
except TypeError:
# Compatible protobuf < 3.20
self._ConvertStructMessage(value, message.struct_value)
elif isinstance(value, list):
self._ConvertListValueMessage(value, message.list_value)
try:
self._ConvertListValueMessage(value, message.list_value, path)
except TypeError:
# Compatible protobuf < 3.20
self._ConvertListValueMessage(value, message.list_value)
elif value is None:
message.null_value = 0
elif isinstance(value, bool):
message.bool_value = value
elif isinstance(value, json_format.six.string_types):
elif isinstance(value, str):
message.string_value = value
elif isinstance(value, json_format._INT_OR_FLOAT): # noqa
message.number_value = value
Expand All @@ -37,7 +45,9 @@ def _ConvertValueMessage(self, value, message):
message.string_value = str(value)
else:
raise json_format.ParseError(
'Value {0} has unexpected type {1}.'.format(value, type(value))
'Value {0} has unexpected type {1}.'.format(
value, type(value)
)
)


Expand Down Expand Up @@ -119,8 +129,18 @@ def ParseDict( # noqa
ignore_unknown_fields=False,
descriptor_pool=None,
):
parser = ProtobufParser(ignore_unknown_fields, descriptor_pool)
parser.ConvertMessage(js_dict, message)
# json_format._Parser introduced `max_recursion_depth` args
# from `protobuf 3.20`
try:
parser = ProtobufParser(
ignore_unknown_fields, descriptor_pool, max_recursion_depth=100
)
parser.ConvertMessage(js_dict, message, '')
except TypeError:
# Compatible protobuf < 3.20
parser = ProtobufParser(ignore_unknown_fields, descriptor_pool)
parser.ConvertMessage(js_dict, message)

return message


Expand Down
22 changes: 21 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
from datetime import datetime

from bali.utils import timezone, dateparser, get_beginning_datetime
from bali.utils import (
ParseDict,
MessageToDict,
timezone,
dateparser,
get_beginning_datetime,
)
from tests.test_services.protos import helloworld_pb2 as pb2


class TestProtobufConverter:
def test_parse_dict(self):
d = {'name': 'protobuf'}
message = ParseDict(d, pb2.HelloRequest())
assert message

def test_message_to_dict(self):
message = pb2.HelloRequest(name='protobuf')
d = MessageToDict(message)
assert 'name' in d
assert d['name'] == 'protobuf'


def test_parse_dmy_date():
Expand Down

0 comments on commit 45c2008

Please sign in to comment.