C++项目中使用thrift对于optional中存在的坑

1、required字段没有__isset属性, 而默认的(就是既没有required,也没有optional)和optional的属性有该方法。
2、创建对象的时候,optional和默认的__isset属性为false,同样不设置该属性,而在经过thrift rpc传输之后,server端的默认的__isset属性为true。
3、有默认值的属性,不赋值的话,其值就是thrift中的默认值。
4、optional的如果没有设置__isset为true,则经过rpc传输(或者说是经过序列化再反序列化)之后,其值会丢失。

struct Student{
   
    1: required i32  sno,
    2: optional string  name,
    3: i64 age,
}
service Serv{
   
    void put(1: Student s),
}

一、

typedef struct _Student__isset {
   
  _Student__isset() : name(false), age(false) {
   }
  bool name :1;
  bool age :1;
} _Student__isset;

可有看到,该结构体重只包含了非required的属性。因此只有非required属性才有__isset的属性。

write

uint32_t Student::write(::apache::thrift::protocol::TProtocol* oprot) const {
   
  uint32_t xfer = 0;
  ::apache::thrift::protocol::TOutputRecursionTracker tracker(*oprot);
  xfer += oprot->writeStructBegin("Student");

  xfer += oprot-&g
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值