Adrian
Adrian

Reputation: 2113

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?

Upvotes: 18

Views: 24430

Answers (7)

mourya venkat
mourya venkat

Reputation: 275

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.

Now you can simply use this command

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

Upvotes: 3

code
code

Reputation: 111

In 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

Upvotes: 0

chaitan94
chaitan94

Reputation: 2232

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.

Upvotes: 2

Dzintars
Dzintars

Reputation: 1571

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;
}

Upvotes: 7

distributed
distributed

Reputation: 386

I work around the problem by passing a Mgoogle/protobuf/timestamp.proto=github.com/golang/protobuf/ptypes/timestamp option 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

It's a bit of a hack. "Fortunately" I'm already using lots of Mprotofile=go/pkg/import/path parameters in my build setup, so it was easy to add.

Upvotes: 0

Seonggi Yang
Seonggi Yang

Reputation: 134

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;
    ...
}

Upvotes: 8

Marsel Novy
Marsel Novy

Reputation: 1817

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"

Upvotes: 5

Related Questions