Skip to content

Commit

Permalink
Use Enum not Enum.name in Struct serialization/deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
roshanjrajan-zip committed Jun 30, 2023
1 parent b2c5805 commit d689e54
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 2 deletions.
30 changes: 28 additions & 2 deletions compiler/cpp/src/thrift/generate/t_py_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,32 @@ void t_py_generator::generate_py_struct_definition(ostream& out,
}

out << "))" << endl;
} else if (gen_enum_) {
bool has_enum = false;
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
t_type* type = (*m_iter)->get_type();
if (type->is_enum()) {
has_enum = true;
break;
}
}

if (has_enum) {
out << endl;
indent(out) << "def __setattr__(self, name, value):" << endl;
indent_up();
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
t_type* type = (*m_iter)->get_type();
if (type->is_enum()) {
out << indent() << "if name == \"" << (*m_iter)->get_name() << "\":" << endl
<< indent() << indent_str() << "super().__setattr__(name, value if hasattr(value, 'value') else "
<< type_name(type) << ".__members__.get(value))" << endl
<< indent() << indent_str() << "return" << endl;
}
}
indent(out) << "super().__setattr__(name, value)" << endl << endl;
indent_down();
}
}

if (!gen_dynamic_) {
Expand Down Expand Up @@ -2287,7 +2313,7 @@ void t_py_generator::generate_deserialize_field(ostream& out,
out << endl;
} else if (type->is_enum()) {
if (gen_enum_) {
indent(out) << name << " = " << type_name(type) << "(iprot.readI32()).name";
indent(out) << name << " = " << type_name(type) << "(iprot.readI32())";
} else {
indent(out) << name << " = iprot.readI32()";
}
Expand Down Expand Up @@ -2477,7 +2503,7 @@ void t_py_generator::generate_serialize_field(ostream& out, t_field* tfield, str
}
} else if (type->is_enum()) {
if (gen_enum_){
out << "writeI32(" << type_name(type) << "[" << name << "].value)";
out << "writeI32(" << name << ".value)";
} else {
out << "writeI32(" << name << ")";
}
Expand Down
45 changes: 45 additions & 0 deletions test/py/explicit_module/EnumSerializationTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

from test5.ttypes import TestEnum, TestStruct
from shared_types.ttypes import SharedEnum
from thrift.TSerialization import serialize, deserialize

def serialization_deserialization_enum_test():
test_obj = TestStruct(param1="test_string", param2=TestEnum.TestEnum1, param3=SharedEnum.SharedEnum1)
test_obj_serialized = serialize(test_obj)
test_obj2 = deserialize(TestStruct(), test_obj_serialized)
assert test_obj.param1 == test_obj2.param1
assert test_obj.param2 == test_obj2.param2
assert test_obj.param3 == test_obj2.param3

def serialization_deserialization_string_test():
test_obj = TestStruct(param1="test_string", param2=TestEnum.TestEnum1.name, param3=SharedEnum.SharedEnum1.name)
test_obj_serialized = serialize(test_obj)
test_obj2 = deserialize(TestStruct(), test_obj_serialized)
assert test_obj.param1 == test_obj2.param1
assert test_obj.param2 == test_obj2.param2
assert test_obj.param3 == test_obj2.param3


if __name__ == "__main__":
serialization_deserialization_enum_test()
serialization_deserialization_string_test()
2 changes: 2 additions & 0 deletions test/py/explicit_module/runtest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ rm -rf gen-py
../../../compiler/cpp/thrift --gen py test3.thrift && exit 1 # Fail since test3.thrift has python keywords
../../../compiler/cpp/thrift --gen py:enum shared_types.thrift || exit 1
../../../compiler/cpp/thrift --gen py:enum test4.thrift || exit 1
../../../compiler/cpp/thrift --gen py:enum test5.thrift || exit 1
PYTHONPATH=./gen-py python -c 'import foo.bar.baz' || exit 1
PYTHONPATH=./gen-py python -c 'import test2' || exit 1
PYTHONPATH=./gen-py python -c 'import test1' &>/dev/null && exit 1 # Should fail.
PYTHONPATH=./gen-py python -c 'import test4.constants' || exit 1
PYTHONPATH=./gen-py python EnumSerializationTest.py || exit 1
cp -r gen-py simple
../../../compiler/cpp/thrift -r --gen py test2.thrift || exit 1
PYTHONPATH=./gen-py python -c 'import test2' || exit 1
Expand Down
33 changes: 33 additions & 0 deletions test/py/explicit_module/test5.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

namespace py test5

include "shared_types.thrift"

enum TestEnum {
TestEnum0 = 0,
TestEnum1 = 1,
}

struct TestStruct {
1: optional string param1
2: optional TestEnum param2
3: optional shared_types.SharedEnum param3
}

0 comments on commit d689e54

Please sign in to comment.