Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing external protos like google/rpc/status.proto

I'm wondering how to properly reference external proto files. Say I've got a .proto file which references standard protobuf types such as Timestamp:

syntax = "proto3";
package api;

import "google/protobuf/timestamp.proto";

message ServerTimeResponse {
  google.protobuf.Timestamp ts = 1;
}

Easy. Timestamp is automatically available when compiling.

Now I add an external type, say google.rpc.Status:

syntax = "proto3";
package api;

import "google/protobuf/timestamp.proto";
import "google/rpc/status.proto";

message ServerTimeResponse {
  google.protobuf.Timestamp ts = 1;
  google.rpc.Status status = 2;
}

Of course we have to tell protoc how to find this file where it is via -I/--proto_path.

My question is this: What is the best practice for actually referencing this file, in particular to make version control happy? There appears not to be a go mod equivalent for protobufs. I've seen it copied verbatim into projects (such as in grpc-gateway) or just referenced from the local filesystem.

like image 959
alkalinity Avatar asked Mar 27 '20 00:03

alkalinity


People also ask

What is proto file used for?

A . proto file is similar to a JSON file in that it represents structured data, but you can run a compiler on the . proto file to generate code that can read and write the data in the programming language of your choice. For more information about protocol buffers, see Protocol Buffer Developer Guide on Google's site.

What does proto version mean?

a combining form meaning “first,” “foremost,” “earliest form of,” used in the formation of compound words (protomartyr; protolithic; protoplasm), specialized in chemical terminology to denote the first of a series of compounds, or the one containing the minimum amount of an element.

How do I use Google protobuf?

To use the Any type, you must import the google/protobuf/any. proto definition. In the C# code, the Any class provides methods for setting the field, extracting the message, and checking the type. Protobuf's internal reflection code uses the Descriptor static field on each generated type to resolve Any field types.

What is gRPC proto file?

proto file gRPC uses a contract-first approach to API development. Protocol buffers (protobuf) are used as the Interface Definition Language (IDL) by default. The . proto file contains: The definition of the gRPC service.


1 Answers

I think you sort of answered your own question here. I've done both successfully: manually copied the necessary files in verbatim (from https://github.com/googleapis/googleapis/tree/master/google and https://github.com/protocolbuffers/protobuf/tree/master/src/google/protobuf), and referenced local copies of the files.

If you want to do this and make version control happy, you could add these two repositories as git submodules inside your repository. Just make sure to pass the right locations to protoc using -I. E.g.:

cd $PROJECT_DIR
mkdir third_party && cd third_party
git submodule add https://github.com/googleapis/googleapis/tree/master/google
cd $PROJECT_DIR
<git commit the change>
protoc -I third_party/google <the rest of your protoc command>

As for referencing local copies of the files, and making sure they're present before attempting to build, you may find that adding something like the following to your Makefile will help (this is in a Go build environment):

go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
go get -u github.com/golang/protobuf/protoc-gen-go
grpc_gateway_path=$(go list -m -f '{{.Dir}}' github.com/grpc-ecosystem/grpc-gateway)
googleapis_path="$grpc_gateway_path/third_party/googleapis"
protoc -I $googleapis_path --go_out=. <list of input files>
like image 72
Lane Rettig Avatar answered Nov 11 '22 22:11

Lane Rettig