Protobuf: Convert .proto from "proto2" to "proto3"

Created on 11 Apr 2017  ·  12Comments  ·  Source: protocolbuffers/protobuf

I just wish to know if there's any tool that converts proto files from "proto2" to proto3". At this point I don't really care about backward compatibility.

c++ enhancement

Most helpful comment

Just added one protoc plugin target (#4699) for changing proto2 to proto3 in benchmark directory. To use that plugin:

$ cd benchmarks
$ make protoc-gen-proto2_to_proto3
$ protoc                                                        \
$    --plugin=protoc-gen-proto2_to_proto3                       \
$    -I$(your_input_proto_root_directory)                       \
$    --proto2_to_proto3_out=$(your_output_proto_root_directory) \
$    $(input_proto_files)

The output proto3 proto file name is the same as input proto2 file, So the output directory should be different with input one

All 12 comments

I don't know of any tool for this, but in practice it is usually fairly easy to convert a .proto file if you don't care about backward compatibility. You can basically just update the syntax line to say syntax = "proto3" and remove all optional and required keywords. The other main things are that proto3 doesn't support extensions (so you would have to remove those), and it requires every enum definition to have a 0 value as its first element. I may be forgetting something but I think those are all the main changes to make to the .proto file.

Yeah, that makes sense. It'll be great if we could have such a tool; will be ready to contribute code if necessary.

If you want to write a tool for this we can add it to our list of third-party add-ons here: https://github.com/google/protobuf/blob/master/docs/third_party.md I'm not sure if we would want to accept it as a contribution to the main protobuf codebase, though, because I would worry that it would be hard to explain to users how it works. It might work well for simple cases or for when backward compatibility is not required, but in other cases an automatic conversion could cause problems and you would really have to think carefully about how to do the migration.

4407 Otherwise, we're SOL in C#.

Yilun is working on something that can convert our proto2 benchmark dataset to proto3 so we can run the dataset against proto3 only languages. It might end up in the benchmarks directory since it will be used for benchmarking purpose but should be available for general use as well.

Just added one protoc plugin target (#4699) for changing proto2 to proto3 in benchmark directory. To use that plugin:

$ cd benchmarks
$ make protoc-gen-proto2_to_proto3
$ protoc                                                        \
$    --plugin=protoc-gen-proto2_to_proto3                       \
$    -I$(your_input_proto_root_directory)                       \
$    --proto2_to_proto3_out=$(your_output_proto_root_directory) \
$    $(input_proto_files)

The output proto3 proto file name is the same as input proto2 file, So the output directory should be different with input one

hi, i am excutting command:

$  cd  protobuf-3.6.1/
$   cd benchmarks/
$ make protoc-gen-proto2_to_proto3


[root@localhost benchmarks]# make protoc-gen-proto2_to_proto3
make: *** No rule to make target `protoc-gen-proto2_to_proto3'.  Stop.

so what should i do? @BSBandme

hi, i am excutting command:

$  cd  protobuf-3.6.1/
$   cd benchmarks/
$ make protoc-gen-proto2_to_proto3


[root@localhost benchmarks]# make protoc-gen-proto2_to_proto3
make: *** No rule to make target `protoc-gen-proto2_to_proto3'.  Stop.

so what should i do? @BSBandme

If you have followed instructions in "C++ Installation Instructions", you should have not problems to make it.

@BSBandme @rongguodong same issue here even I can make protobuf successfully:

make: * No rule to make target `protoc-gen-proto2_to_proto3'. Stop.

Meet other error after change build branch from 3.6.x to master:

make: * 没有规则可以创建“protoc-gen-proto2_to_proto3”需要的目标“../src/libprotobuf.la”。 停止。

./autogen.sh
./configure
make

and well be ok
image

@bravekingzhang
Thanks

Was this page helpful?
0 / 5 - 0 ratings