2
topic.thrift 파일에 공용체 유형을 정의하고 gen-py를 생성합니다. 이 같은 :python thrift union 유형을 직렬화 할 수 없습니까?
union Generic{
1: string s,
2: bool b,
3: i64 i,
4: double d}
struct Article{
1: string title,
2: string content,
3: Generic test}
이 같은 직렬화 코드 :
parse_item = Article()
parse_item.test = 1
상관없이 str을, INT, BOOL, 또는 더블 :
transport_out = TTransport.TMemoryBuffer()
protocol_out = TBinaryProtocol.TBinaryProtocol(transport_out)
parse_item.write(protocol_out)
bytes = transport_out.getvalue()
가 parse_item이 기사의 목적은 값을 parse_item.test에 할당하면 다음과 같은 오류가 발생합니다.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "gen-py/topic/ttypes.py", line 189, in write
self.test.write(oprot)
AttributeError: 'str' object has no attribute 'write'
정말 이유를 모르겠습니까? 누구나 아이디어가 있습니까?
아, 그래! 감사! 나는 유니온 구조가 힘들다고 생각한다! 선택적 어쩌면 대체 선택 사용할 수 있습니다. – Nan