Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only put .proto protocol buffer file in a repository?

I wonder what is the best practice for protocol buffer regarding source repository (e.g. git) :

Do I have to put ONLY the .proto file in the repository and let anyone else who uses the source code to regenerate classes code with protoc compiler ? or is it a best pratice to put both .proto files AND source code generated by protoc compiler ?

like image 989
kondor Avatar asked Dec 16 '16 14:12

kondor


People also ask

Where do you store proto files?

I would suggest storing the . proto files in a separate project. These are the contract between your two projects, and they are not necessarily "owned" by either one.

How do I send protobuf over HTTP?

You can certainly send even a binary payload with an HTTP request, or in an HTTP response. Just write the bytes of the protocol buffer directly into the request/response, and make sure to set the content type to "application/octet-stream". The client, and server, should be able to take care of the rest easily.

What is a protocol buffer file?

Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler.


2 Answers

You should never check in generated code if you can avoid it.

If you check in generated code, you take on multiple risks, such as:

  • You risk losing the knowledge of how to correctly regenerate that code. If it's not automated as part of the build, it's too easy to forget to document, or to have the documentation be wrong.
  • You risk the generated code getting out-of-sync with the schema. For example, someone could make a change to the .proto file but forget to update the generated code. Their changes won't actually "take effect" until someone else later on regenerates the generated code for some other reason -- and then all of the sudden they see side effects they weren't expecting.
  • Your generated code might be for a different version of protocol buffers than what the builder has installed. In this case it won't work correctly, since it's necessary to use the exact same version of the compiler and runtime library.

If for some reason you absolutely have to check in generated code, I highly recommend creating an automated test that checks if the checked-in code matches what protoc would generate if run fresh. (For example, the protobuf repository itself contains checked-in copies of generated code for descriptor.proto because this code is needed to compile protoc, creating a circular dependency. But there is a unit test that checks that the checked-in code matches what protoc would generate.)

like image 145
Kenton Varda Avatar answered Oct 04 '22 15:10

Kenton Varda


If your project is commonly used in its source code form (e.g. a library or a program every user is supposed to compile himself), I would make available release packages that have the generated files.

But I wouldn't put the generated files into the repository directly. And if most users will use a compiled binary, it is not that important to provide easy-to-compile source packages either. The protobuf generator then becomes just another build dependency.

like image 42
jpa Avatar answered Oct 04 '22 15:10

jpa