Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions clarifai/rest/grpc/custom_converters/custom_dict_to_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,22 @@
except ImportError:
from inspect import getargspec as get_args

DEFAULT_MAX_RECURSION_DEPTH = 64


def dict_to_protobuf(protobuf_class, js_dict, ignore_unknown_fields=False):
# type: (type(Message), dict, bool) -> Message
message = protobuf_class()

# Protobuf versions 3.6.* and 3.7.0 require a different number of parameters in the _Parser's
# constructor. In the case of 3.6.*, we pass only the argument ignore_unknown_fields, but in
# the case of 3.7.0, we pass in one additional None parameter. To be future proof(ish), pass in
# None to any subsequent parameter.
num_of_args = len(get_args(_Parser.__init__).args)
none_args = [None] * (num_of_args - 2) # Subtract 2 for self and ignore_unknown_fields.
parser = _CustomParser(ignore_unknown_fields, *none_args)
parser = _CustomParser(ignore_unknown_fields, None, DEFAULT_MAX_RECURSION_DEPTH)

parser.ConvertMessage(js_dict, message)
parser.ConvertMessage(js_dict, message, None)
return message


class _CustomParser(_Parser):

def _ConvertFieldValuePair(self, js, message):
def _ConvertFieldValuePair(self, js, message, path=None):
"""
Because of fields with custom extensions such as cl_default_float, we need
to adjust the original's method's JSON object parameter by setting them explicitly to the
Expand All @@ -42,4 +38,4 @@ def _ConvertFieldValuePair(self, js, message):
if f.name not in js:
js[f.name] = default_float

super(_CustomParser, self)._ConvertFieldValuePair(js, message)
super(_CustomParser, self)._ConvertFieldValuePair(js, message, path)