问题
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 writeoptional
: 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:
It is safe to change
optional
to default (i.e. remove theoptional
keyword).Changing
optional
torequired
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 thatrequired
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