roshanjrajan-zip opened a new pull request, #2825:
URL: https://github.com/apache/thrift/pull/2825

   <!-- Explain the changes in the pull request below: -->
   When serializing and deserializing Enums for Structs, we convert the Enum to 
a string instead of keeping it as an Enum. What this means is that the thrift 
file shows that an Enum is passed in the struct, when actually a string is 
passed in. This is very confusing can arbitrarily lead to KeyError and in the 
deserialization logic.
   
   However, changing this behavior is potentially breaking. To avoid breaking 
existing users, check if the type is an Enum by using the `hasattr` check to 
see if type has attribute value. If so, parse as an enum. Otherwise parse as a 
string.
   
   Given a thrift file like the following:
   ```python
   enum TestEnum {
     TestEnum0 = 0,
     TestEnum1 = 1,
   }
   
   struct TestStruct {
       1: optional string param1
       2: optional TestEnum param2
   }
   ```
   
   this is how you would create a struct before and after the change but both 
are valid.
   ```python
   # Before
   TestStruct(param1="test_string", param2=TestEnum.TestEnum1.name) # or pass 
in "TestEnum1"
   
   # After
   TestStruct(param1="test_string", param2=TestEnum.TestEnum1)
   ```
   
   Serialization/Deserialization Code Change Example- 
   ```python
   class TestStruct(object):
       """
       Attributes:
        - param1
        - param2
   
       """
   
   
       def __init__(self, param1=None, param2=None,):
           self.param1 = param1
           self.param2 = param2
   
   ++    def __setattr__(self, name, value):
   ++        if name == "param2":
   ++            self.__dict__[name] = value if hasattr(value, 'value') else 
TestEnum.__members__.get(value)
   ++            return
   ++        self.__dict__[name] = value
   
       def read(self, iprot):
           if iprot._fast_decode is not None and isinstance(iprot.trans, 
TTransport.CReadableTransport) and self.thrift_spec is not None:
               iprot._fast_decode(self, iprot, [self.__class__, 
self.thrift_spec])
               return
           iprot.readStructBegin()
           while True:
               (fname, ftype, fid) = iprot.readFieldBegin()
               if ftype == TType.STOP:
                   break
               if fid == 1:
                   if ftype == TType.STRING:
                       self.param1 = iprot.readString().decode('utf-8', 
errors='replace') if sys.version_info[0] == 2 else iprot.readString()
                   else:
                       iprot.skip(ftype)
               elif fid == 2:
                   if ftype == TType.I32:
   --                    self.param2 = TestEnum(iprot.readI32()).name
   ++                    self.param2 = TestEnum(iprot.readI32())
                   else:
                       iprot.skip(ftype)
               else:
                   iprot.skip(ftype)
               iprot.readFieldEnd()
           iprot.readStructEnd()
   
       def write(self, oprot):
           if oprot._fast_encode is not None and self.thrift_spec is not None:
               oprot.trans.write(oprot._fast_encode(self, [self.__class__, 
self.thrift_spec]))
               return
           oprot.writeStructBegin('TestStruct')
           if self.param1 is not None:
               oprot.writeFieldBegin('param1', TType.STRING, 1)
               oprot.writeString(self.param1.encode('utf-8') if 
sys.version_info[0] == 2 else self.param1)
               oprot.writeFieldEnd()
           if self.param2 is not None:
               oprot.writeFieldBegin('param2', TType.I32, 2)
   --            oprot.writeI32(TestEnum[self.param2].value)
   ++           oprot.writeI32(self.param2.value)
               oprot.writeFieldEnd()
           oprot.writeFieldStop()
           oprot.writeStructEnd()
   
       def validate(self):
           return
   
       def __repr__(self):
           L = ['%s=%r' % (key, value)
                for key, value in self.__dict__.items()]
           return '%s(%s)' % (self.__class__.__name__, ', '.join(L))
   
       def __eq__(self, other):
           return isinstance(other, self.__class__) and self.__dict__ == 
other.__dict__
   
       def __ne__(self, other):
           return not (self == other)
   all_structs.append(TestStruct)
   TestStruct.thrift_spec = (
       None,  # 0
       (1, TType.STRING, 'param1', 'UTF8', None, ),  # 1
       (2, TType.I32, 'param2', None, None, ),  # 2
   )
   fix_spec(all_structs)
   del all_structs
   ```
   
     
   
   <!-- We recommend you review the checklist/tips before submitting a pull 
request. -->
   
   - [ ] Did you create an [Apache 
Jira](https://issues.apache.org/jira/projects/THRIFT/issues/) ticket?  
([Request account here](https://selfserve.apache.org/jira-account.html), not 
required for trivial changes)
   - [ ] If a ticket exists: Does your pull request title follow the pattern 
"THRIFT-NNNN: describe my issue"?
   - [x] Did you squash your changes to a single commit?  (not required, but 
preferred)
   - [x] Did you do your best to avoid breaking changes?  If one was needed, 
did you label the Jira ticket with "Breaking-Change"?
   - [ ] If your change does not involve any code, include `[skip ci]` anywhere 
in the commit message to free up build resources.
   
   <!--
     The Contributing Guide at:
     https://github.com/apache/thrift/blob/master/CONTRIBUTING.md
     has more details and tips for committing properly.
   -->
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@thrift.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to