Removing a field in protobuf

7.9k views Asked by At

I have a protobuf message like this:

message MyMessage{
    string foo = 1;
    int toBeRemovedBar = 2 [deprecated = true];
    string zag = 3;
}

toBeRemovedBar attribute is no longer required and needs to be removed. As per the guidelines here and here, I could reserve either the field name or the number. What is the best practice here?

  • Reserve either the field number or name
  • Reserve both field number and name

Reserving field number would prevent only the reuse of the field number. Reuse of the field name could not be prevented. Similar argument holds good for reserving only the field name.

So, the right way to remove an attribute is to reserve both field name and number as given below?

message MyMessage{
    string foo = 1;

    reserved 2;
    reserved "toBeRemovedBar";

    string zag = 3;
}
2

There are 2 answers

0
LeonFibuck On

It seems good to me.

In this way you prevent both the field number and the name from being used in future. I did the same in a work project because I had to be sure that both the field number and the name wouldn't be used again.

0
Doug Fawley On

You should reserve the field number. This will prevent new fields from being added with the same tag, which would result in a wire incompatibility for any users running an old build that has the legacy field compiled in. Reusing the name is fine from a wire-compatibility standpoint, although doing so could cause some confusion.