Thrift 将可选转换为默认或必需 [英] Thrift converting optional to default or required

查看:34
本文介绍了Thrift 将可选转换为默认或必需的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个节俭的结构

struct Message {
    1: optional int userID;
    ...
} 

将其更改为默认要求是否安全操作?

Is it a safe operation to change it to default require-ness?

struct Message {
    1: int userID;
    ...
} 

如果我知道它总是设置的?必需"呢?

If I know it's always set? What about "required"?

推荐答案

这个答案,Thrift 有三个程度的(所谓的)必要性:

As outlined in this answer, there are three degrees of (so-called) requiredness in Thrift:

  • required:读取时必须存在,写入时必须设置
  • optional:可以设置也可以不设置,完全可选
  • "default": 读取时可能不存在,始终写入(除非它是 null 指针)
  • required: must exist on read, must be set on write
  • optional: may or may not be set, entirely optional
  • "default": may not exist on read, always written (unless it is a null pointer )

回答问题:

  1. optional 更改为默认值是安全的(即删除 optional 关键字).

  1. It is safe to change optional to default (i.e. remove the optional keyword).

optional 更改为 required 可能会破坏兼容性.除非您确保所有客户端/服务器都相应地更新,否则旧端可能不会为此类字段提供值.在这种情况下,另一端将拒绝传入的请求或响应,因为它不完整,因为接收到的数据中缺少 required 字段.

Changing optional to required may break compatibility. Unless you make sure all clients/servers are updated accordingly, it may happen that the older side does not supply a value for such a field. In that case, the other end will reject the incoming request or response as incomplete, because that required field is missing from the received data.

有关该主题的进一步阅读,您可能需要咨询 Diwaker Gupta 的强烈推荐缺少指南".

For further reading on the subject you may want to consult Diwaker Gupta's highly recommendable "Missing Guide".

这篇关于Thrift 将可选转换为默认或必需的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆