问题
What is the best process for making a required
field optional
in thrift. For example, I have a struct...
struct Message {
1: required double userID;
2: required string content;
...
}
... but I want to make content
optional.
EDIT: To clarify, I already have consumers that use this struct, so I would need to update it without breaking those consumers. A staged upgrade is fine (ie - add a new optional
field, update the downstream clients, then remove--or stop using--the old required
field).
回答1:
You can't, because as the saying goes required is forever. The following is a quote from Diwaker Gupta's highly recommendable "Missing Guide". It pretty much nails it down why one should think twice (at least) before using required
:
Required Is Forever
You should be very careful about marking fields as required. If at some point you wish to stop writing or sending a required field, it will be problematic to change the field to an optional field — old readers will consider messages without this field to be incomplete and may reject or drop them unintentionally. You should consider writing application-specific custom validation routines for your buffers instead. Some have come the conclusion that using required does more harm than good; they prefer to use only optional. However, this view is not universal.
I'm afraid that the only option is to deprecate the entire struct and create a new one.
In addition to that, there are in fact three degrees of requiredness, only two of them have keywords:
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 )
The "default" requiredness is applied implicitly when neither required
nor optional
are specified.
As one clearly sees, the restrictions for required
are rather strict if we look at the compatibility site of things. Even adding a new required
field to a struct may lead to incompatibility, e.g. if a new client is reading data from an old server (or vice versa), because the new required
field is not in the data written by the old impl, but expected by the new impl.
来源:https://stackoverflow.com/questions/37558302/how-do-i-make-a-required-thrift-field-optional