How to use the predefined protobuf type (ie "google / protobuf / timestamp.proto") with gRPC

I am trying to use google/protobuf/timestamp.proto with the gRPC and Go plugin. This is how I run protoc :

 protoc -I ./ ./*.proto --go_out=plugins=grpc:. 

And this is my .proto :

 #domain.proto syntax = "proto3"; option java_multiple_files = true; option java_package = "com.viant.xyz"; option java_outer_classname = "domain"; import "google/protobuf/timestamp.proto"; message Foo { Timestamp modifiedTime = 1; ... } 

I see the following errors:

 domain.proto: Import "google/protobuf/timestamp.proto" was not found or had errors. domain.proto:44:5: "Timestamp" is not defined. 

Am I missing something or is it not yet supported?

+12
source share
7 answers

It is not yet fully supported, but you can make it work by changing

 message Foo { google.protobuf.Timestamp modifiedTime = 1; ... } 

and by fixing the generated file import

 import google_protobuf "google/protobuf/timestamp.pb" 

to

 import google_protobuf "github.com/golang/protobuf/ptypes/timestamp" 
+5
source

Add /usr/local/include to include paths for using /usr/local/include/google/api/timestamp.proto :

 protoc -I/usr/local/include -I. --go_out=plugins=grpc:. *.proto 

As you can see in timestamp.proto , Timestamp exists in the google.protobuf package, so you need to change to use Timestamp as follows:

 message Foo { google.protobuf.Timestamp modifiedTime = 1; ... } 
+8
source

In my case, the problem was in my Fedora 29 setup.

 # Install Protoc compiler. By default it is 3.5.0 version sudo dnf -y install protoc 

It was my bad setup. So I fixed it with the following steps. Also note the gray command lines.

 # Uninstall old 3.5.0 version sudo dnf remove protobuf # Make sure you grab the latest version curl -OL https://github.com/protocolbuffers/protobuf/releases/download/v3.6.1/protoc-3.6.1-linux-x86_64.zip # Unzip unzip protoc-3.6.1-linux-x86_64.zip -d protoc3 # Move protoc to /usr/local/bin/ sudo mv protoc3/bin/* /usr/local/bin/ # Move protoc3/include to /usr/local/include/ sudo mv protoc3/include/* /usr/local/include/ # Optional: change owner sudo chown $USER /usr/local/bin/protoc sudo chown -R $USER /usr/local/include/google 

After that I can use:

 import "google/protobuf/timestamp.proto"; message Session { google.protobuf.Timestamp create_time = 1; } 
+3
source

I am working on a problem by passing the parameter Mgoogle/protobuf/timestamp.proto=github.com/golang/protobuf/ptypes/timestamp to the Go grpc plugin.

In other words, I'm calling

 protoc --go_out=plugins=grpc,Mgoogle/protobuf/timestamp.proto=github.com/golang/protobuf/ptypes/timestamp:outputdir input.proto 

This is a little hack. "Fortunately, I already used a lot of the parameters Mprotofile=go/pkg/import/path in my installation setup, so it was easy to add.

0
source

If you encounter this as an alpine docker, be sure to make apk add protobuf-dev before generating your files with protoc .

0
source

On Windows, clone the repository: protobuf .

And run the command

 protoc -I=$SRC_DIR -I=$YOUR_CLONE_LOCATION/protobuf/src --go_out=$DST_DIR $SRC_DIR/$SRC_FILE 
0
source

After hours of scratching my head, I found a problem.

There are no / google / protobuf files in my / usr / local / include directory, and predefined types cannot be used without it. To solve this problem.

Now you can just use this command

protoc -I / usr / local / include -I. --go_out = {output_directory_path} {proto_file_path}

0
source

Source: https://habr.com/ru/post/1258158/


All Articles