Protobuf struct field numbering

1,723 views
Skip to first unread message

Sarvsav Sharma

unread,
Oct 18, 2021, 11:55:34 PM10/18/21
to Protocol Buffers
Hi Team,

I have a query related to assigning field numbers to my proto struct. My service is used by other different services like testing service, data service, and others. To assign field numbers I have 2 options:

Assumption: 1-15 fields are reserved

1. I should keep adding the new fields and numbers without leaving any gaps. Example,
test_service_name = 16
test_service_email = 17
data_service_info = 18
user_service_id = 19
shared_service_data = 20
....
The problem here, it is very difficult to distinguish the type of field consumed by what service. The benefit I will get is, that there will be no gaps in the fields.

2. I should keep reserving the field numbers for different services, for example, fields used by testing service should be range from 21-40, user service, 41-60, and data-service 61-80, shared service 100-200. Then the above same struct will look like this:
test_service_name = 21
test_service_email = 22
user_service_id = 41
data_service_info = 61
shared_service_data = 100

It is following the pattern we have in linux passwd file for ranging the accounts.

Which option should be consider as a better option and why? Kindly share your inputs.

Adam Cozzette

unread,
Oct 19, 2021, 11:25:28 AM10/19/21
to Sarvsav Sharma, Protocol Buffers
I would recommend continuing to add new fields with sequential numbering, since you can always reorder the fields in your file to group together related fields. There is no requirement for fields to appear in field number order in the file, so you could write something like this for example:

// User service fields
optional string user_id = 16;
optional string user_xyz = 19;

// Data service fields
optional int32 data_whatever = 17;
optional string data_bar = 18;
optional bool use_data = 20;

Just to be clear, it's never safe to change field numbers on fields, but it is perfectly safe to change the order that fields appear in the message.

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/protobuf/27eaee6d-9705-4ae5-917a-f04d968c6a43n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages