Thrift converting optional to default or required

末鹿安然 提交于 2020-02-25 06:04:58

问题


I have a thrift struct

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"?


回答1:


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

  • 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 )

To answer the question(s) asked:

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

  2. 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.

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



来源:https://stackoverflow.com/questions/53357745/thrift-converting-optional-to-default-or-required

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!