How to organize multiple protocol buffer files in git submodule?
Asked Answered
V

0

7

I'm sharing .proto files across multiple projects using git-submodules.

Now my question is how to setup the imports properly?

In a microservice environment all service-repositories have a git-submodule containing the shared .proto files:

  • git.dev/fooservice
    • proto (contains git.dev/proto as submodule)
    • ... some java code
  • git.dev/barservice
    • proto (contains git.dev/proto as submodule)
    • ... some go code
  • git.dev/bazservice
    • proto (contains git.dev/proto as submodule)
    • ... some javascript code
  • git.dev/proto
    • shared
      • shared.proto
    • fooservice
      • fooservice.proto (imports shared/shared.proto)
    • barservice
      • barservice.proto (imports shared/shared.proto)

The typical header of a service proto file currently looks like this:

git.dev/proto/fooservice/fooservice.proto

syntax = "proto3";

package abc.foo;

import "shared/shared.proto";

option go_package = "foopb";
option java_multiple_files = true;
option java_outer_classname = "FooProto";
option java_package  = "com.abc.foo";

Question 1: Import path

  • Having it import "shared/shared.proto"; works for linting of the git.dev/proto repo, but causes import-problems during code generation.
  • Having it import "proto/shared/shared.proto"; works for code generation in the service-repos but of course causes linting problems in the git.dev/proto repo
  • Should the import statement contain the proto folder?

Question 2: go_package Option

  • to build the go stubs we currently use prototool to and set the go_options.import_path to e.g. git.dev/fooservice which generates correct golang import paths
  • another option would be to run something like
    • for x in proto/**/*.proto; do protoc -Iproto --go_out=plugins=grpc,paths=source_relative:src/proto $x; done but this does not result in valid golang import paths as long as I don't add the full git path to the go_package option. Doing that of course conflicts with the git-submodule approach (which git repo path to choose, /proto or /*service.git ?).
  • What's best practice using go_package in combination with git-submodules?

Question 3: submodules and 3rd-party tools

  • Is using git-submodules and 3rd-party tools like prototool the right way to work with proto files across multiple projects?

Thanks!

Vaughnvaught answered 24/6, 2019 at 6:34 Comment(3)
In regards to submodules, we have run into problems with submodules: transitive dependencies you maybe have a scenario where 2 different stubs use the same shared submodule but with different versions... We are moving away from Submodules and going with a metadata file that describes git dependencies and their versions along with a CLI to resolve them locally.Silhouette
Thanks for the input - did you work with golang - if so, how did you resolve the import problem?Vaughnvaught
using github.com/jacebrowning/gitman so that we can organize any dependency in a directory structure we need for the protosSilhouette

© 2022 - 2024 — McMap. All rights reserved.