-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How can I convert a dict object(with Map Type) to a thrift object? #152
Comments
DO we need this format?
|
That particular function is intended to convert person = enum_thrift.Person(**person_dict) This only works for simple thrift structs (ie. ones that aren't nested). Otherwise you'll have to write something recursive that reads the thrift spec. It might look like: def dict_to_thrift(thrift_cls, data, ignore_missing=True):
"""
Convert a dict to a thrift object
:param thrift_cls: the thrift class
:param data: the data to be encoded
:param ignore_missing: fail if we are missing a value
:return:
"""
result = {}
if isinstance(data, (str, int, float, bool, bytes)):
return data
if isinstance(thrift_cls, tuple):
container_type = thrift_cls[0]
item_type = thrift_cls[1]
if container_type == TType.STRUCT:
return dict_to_thrift(item_type, data, ignore_missing)
elif container_type in (TType.LIST, TType.SET):
return [dict_to_thrift(item_type, v, ignore_missing) for v in data]
elif container_type == TType.MAP:
return {
dict_to_thrift(item_type[0],k, ignore_missing):
dict_to_thrift(item_type[1], v, ignore_missing) for k, v in data.items()
}
for field_idx, spec in thrift_cls.thrift_spec.items():
thrift_type, field_name = spec[0], spec[1]
if field_name not in data:
if ignore_missing:
continue
else:
raise ValueError(f"Missing non-optional field {field_name}")
dict_data = data[field_name]
# handle each type here
if thrift_type in (TType.LIST, TType.SET):
result[field_name] = [dict_to_thrift(spec[2], x, ignore_missing) for x in dict_data]
elif thrift_type == TType.STRUCT:
result[field_name] = dict_to_thrift(spec[2], dict_data, ignore_missing)
elif thrift_type == TType.MAP:
result[field_name] = {
dict_to_thrift(spec[2][0], k, ignore_missing):
dict_to_thrift(spec[2][1], v, ignore_missing) for k, v in
dict_data.items()}
else:
result[field_name] = dict_data
if hasattr(thrift_cls, '__call__'):
return thrift_cls(**result)
else:
for k, v in result.items():
setattr(thrift_cls, k, v)
return thrift_cls This is adapted from apache_json.py Note that there's no way to tell if a field is marked as |
@JonnoFTW I have the same issue and thanks for the # handle each type here
if thrift_type in (TType.LIST, TType.SET):
result[field_name] = [dict_to_thrift(spec[2], x, ignore_missing) for x in dict_data]
if thrift_type == TType.STRUCT:
result[field_name] = dict_to_thrift(spec[2], dict_data, ignore_missing)
if thrift_type == TType.MAP:
result[field_name] = {
dict_to_thrift(spec[2][0], k, ignore_missing):
dict_to_thrift(spec[2][1], v, ignore_missing) for k, v in
dict_data.items()}
else:
result[field_name] = dict_data should be # handle each type here
if thrift_type in (TType.LIST, TType.SET):
result[field_name] = [dict_to_thrift(spec[2], x, ignore_missing) for x in dict_data]
elif thrift_type == TType.STRUCT:
result[field_name] = dict_to_thrift(spec[2], dict_data, ignore_missing)
elif thrift_type == TType.MAP:
result[field_name] = {
dict_to_thrift(spec[2][0], k, ignore_missing):
dict_to_thrift(spec[2][1], v, ignore_missing) for k, v in
dict_data.items()}
else:
result[field_name] = dict_data |
@ms300 thanks, I've updated my code |
Thanks all guy`s useful code. |
@liuweiming1997 please keep in mind that thriftpy2 doesn't currently support optional fields since this information is excluded from the |
HI guys.
I am facing a bug that can`t parse python dict to the thrift object. Here is the simple.
Can anyone help me? Thanks a lot.
enum.thrift
main.py
Error:
The text was updated successfully, but these errors were encountered: