Replacing 'extensions' in proto3

4,873 views
Skip to first unread message

R.C.

unread,
Jun 5, 2017, 2:48:43 PM6/5/17
to Protocol Buffers
Hi,

I am looking to move an application written with proto2 to use proto3. I understand 'extensions' is disabled in proto3 and am looking for some guidance to change the below to proto3:


========================
common.proto:

message Vehicles {
    extensions 1 to max
}

========================
car.proto:

import "common.proto";

message Car {
    string type = 1;
    string color = 2;
}

extend Vehicles {
    optional Car CarExt  = 100;
}

========================

bike.proto:

import "common.proto";

message Bike {
    string type = 1;
    string color = 2;
}

extend Vehicles {
    optional Bike BikeExt = 200;
}
========================

Since 'optional' is default i can remove the 'optional' keyword but I am not sure how 'Any' can be used in such scenario in the place of 'extensions'.

Thanks,
rc

Bo Yang

unread,
Jun 5, 2017, 4:21:03 PM6/5/17
to R.C., Protocol Buffers
message Vehicles {
  Any extending = 1;
}

// cpp code ==================
Car car = new Car();
UnpackAny(vehicles.extending, &car);

--
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 post to this group, send email to prot...@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

R.C.

unread,
Jun 5, 2017, 4:48:08 PM6/5/17
to Protocol Buffers, karsh...@gmail.com
Hi Bo,

Thanks for your response. Would using 'Any' in this fashion change the format on the wire? How can I edit the proto file to maintain the format on the wire (maintaining backward compatibility with older message decoders)?

Thanks,
rc

Josh Humphries

unread,
Jun 5, 2017, 5:23:09 PM6/5/17
to R.C., Protocol Buffers
On Mon, Jun 5, 2017 at 4:48 PM, R.C. <karsh...@gmail.com> wrote:
Hi Bo,

Thanks for your response. Would using 'Any' in this fashion change the format on the wire? How can I edit the proto file to maintain the format on the wire (maintaining backward compatibility with older message decoders)?

Yes, using an `Any` field changes the wire representation. If you are trying to maintain binary compatibility in your move to proto3, you will have to replace extensions with actual fields with the same type and tag on the Vehicles message:

message Vehicles {
  optional Car car = 100;
  optional Bike bike = 200;
}

You would also then need to re-structure your files, like moving the definition of Vehicle into its own file, so you don't accidentally introduce any import cycles.

 
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+unsubscribe@googlegroups.com.

L Jun

unread,
Mar 9, 2020, 4:53:40 AM3/9/20
to Protocol Buffers
Hi, did you solve it ?  how?

Karsh R.C.

unread,
Feb 20, 2021, 12:20:33 PM2/20/21
to L Jun, Protocol Buffers
L Jun,

I did not change the proto format. Continuing to use proto2 :|

--rc

--
You received this message because you are subscribed to a topic in the Google Groups "Protocol Buffers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/protobuf/49chFOx06nU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to protobuf+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/protobuf/b0844c33-b51a-4bc5-9a4b-a7d40b33892a%40googlegroups.com.


--
The Difference between Dreams and Accomplishment is Purely Desire
Reply all
Reply to author
Forward
0 new messages