本文共 1071 字,大约阅读时间需要 3 分钟。
enum ReturnCode {Success,Failed,}struct ValueGetResult { required ReturnCode code, optional string value, } service Find_SomeThing { ValueGetResult GetValue(1: required string key) }
在实践的时候,我发现它给ValueGetResult 多生成了一个变量 bool __isset.value ,所以thrift 并不是直接帮我们把查找到的Value返回,它需要我们自己判断什么时候返回,那么我们就可以根据得到的ReturnCode 来进行判断如果Success ,我们需要手动去置 __isset.value 为True。这个时候Thrift 的逻辑对于Server端来说,当它给Client返回数据的时候会进行判断,如果为True就把这个value返回了回去
有的时候我们想把thrift结构的内容输出到日志上,那么可以使用下面这个函数,这个函数是官方头文件推荐的做法。
// TODO(dreiss): Move (part of) ThriftDebugString into a .cpp file and remove this.#includenamespace apache {namespace thrift {template std::string ThriftDebugString(const ThriftStruct& ts) { using namespace apache::thrift::transport; using namespace apache::thrift::protocol; TMemoryBuffer* buffer = new TMemoryBuffer; boost::shared_ptr trans(buffer); TDebugProtocol protocol(trans); ts.write(&protocol); uint8_t* buf; uint32_t size; buffer->getBuffer(&buf, &size); return std::string((char*)buf, (unsigned int)size);}
转载地址:http://vmjdi.baihongyu.com/