Sobes.tech
Senior

How are protobuf contracts organized: where are they stored, how do you edit and compile them, where is the generated code stored?

sobes.tech AI

Answer from AI

In our practice, protobuf contracts are organized as follows:

  • Where they are stored: All .proto files are stored in a separate repository or in a dedicated directory within the monorepo to ensure a single source of truth for all services.

  • How we modify: Changes to .proto files go through code review and approval, as they affect multiple services. We use versioning of contracts to avoid incompatibilities.

  • How we compile: To generate code, we use official protoc tools with plugins for Go (protoc-gen-go). Compilation is integrated into the CI/CD pipeline to automatically update the generated code when .proto files change.

  • Where the generated code is stored: The generated Go code is stored alongside the source .proto files or in separate directories within the services that use them. In some cases, the generated code is published as a separate artifact or library so that other services can include it as a dependency.

Example of a generation command:

protoc --go_out=./gen --go_opt=paths=source_relative \
       --go-grpc_out=./gen --go-grpc_opt=paths=source_relative \
       path/to/file.proto

This approach ensures consistency, ease of maintenance, and minimizes errors when working with protobuf contracts.