Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use predifined protobuf type (i.e. "google/protobuf/timestamp.proto") with gRPC

I'm trying to use google/protobuf/timestamp.proto in with gRPC plugin and Go. 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'm seeing 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 this is not yet supported?

like image 814
Adrian Avatar asked Oct 13 '16 15:10

Adrian


5 Answers

Add /usr/local/include to include paths to use /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 package google.protobuf, so you have to modify to use Timestamp like this:

message Foo {
    google.protobuf.Timestamp modifiedTime = 1;
    ...
}
like image 72
Seonggi Yang Avatar answered Oct 11 '22 11:10

Seonggi Yang


In my case problem was in my Fedora 29 setup.

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

This was my bad setup. So i fixed it with following steps. Pay attention to grayed out command lines too.

# 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 this I am able to use:

import "google/protobuf/timestamp.proto";

message Session {
    google.protobuf.Timestamp create_time = 1;
}
like image 42
Dzintars Avatar answered Oct 11 '22 11:10

Dzintars


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

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

and by fixing generated file import

import google_protobuf "google/protobuf/timestamp.pb"

to

import google_protobuf "github.com/golang/protobuf/ptypes/timestamp"
like image 27
Marsel Novy Avatar answered Oct 11 '22 09:10

Marsel Novy


If you are facing this inside an alpine docker image, make sure you do an apk add protobuf-dev before generating your files using protoc.

like image 30
chaitan94 Avatar answered Oct 11 '22 10:10

chaitan94


After scratching my head for hours, I've found the issue.

My /usr/local/include directory doesn't have /google/protobuf files and without that once can't use predefined types. To solve this issue.

  • curl -OL https://github.com/google/protobuf/releases/download/v3.2.0/protoc-3.2.0-linux-x86_64.zip

  • unzip protoc-3.2.0-linux-x86_64.zip -d protoc3

  • sudo mv protoc3/bin/* /usr/local/bin/

  • sudo mv protoc3/include/* /usr/local/include/

Now you can simply use this command

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

like image 22
mourya venkat Avatar answered Oct 11 '22 09:10

mourya venkat