import date.proto in other proto file

Viewed 756

I am new to protocol buffer. I have some java pojo class which has a field of type Date including year, month and day only.

For timestamps type, we can import "google/protobuf/timestamp.proto";.

But for Date type, we cant do the same since date.proto is in https://github.com/googleapis/googleapis/blob/master/google/type/date.proto.

So how can I import this date.proto?

2 Answers

For timestamps type, we can import "google/protobuf/timestamp.proto";.

Because timestamp.proto is some kinds of built-in message type included in Protobuf.

But for Date type, we cant do the same since date.proto is in https://github.com/googleapis/googleapis/blob/master/google/type/date.proto.

  1. Firstly, you should clone the whole project or simply download whatever proto file you need (in this example, the date.proto file).
  2. Secondly, put date.proto into your project, you can create a directory for imports, for example: google/type/date.proto
  3. Finally, generating code with protoc; You have to use the --proto_path command-line flag (aka -I) to tell protoc where to look for .proto files. If you don't provide a path, by default it will only search the current directory:
protoc -I.  --cpp_out=. usersvr.proto

This is the structure of my demo project:

╰─$ tree                                                                                
.
├── google
│   └── type
│       └── date.proto
├── usersvr.pb.cc
├── usersvr.pb.h
└── usersvr.proto

usersvr.proto:

syntax = "proto3";
package srv.user;
import "google/protobuf/timestamp.proto";
import "google/type/date.proto";

option cc_generic_services = true;
message BatchGetUserInfosRequest {
  uint64 my_uid = 1;
  repeated uint64 peer_uids = 2;
  map<string, string> infos = 3;
  google.protobuf.Timestamp timestamp = 4;
  google.type.Date date = 5;
}

date.proto is downloaded from https://github.com/googleapis/googleapis/blob/master/google/type/date.proto

Assuming that you have your Gradle (or Maven) setup working properly and generating code, you'll have to copy the .proto file into you protobuf source file (eg: project/src/main/proto). You only need to copy this (if you care only about using it with Java):

syntax = "proto3";

package google.type;

option java_multiple_files = true;
option java_outer_classname = "DateProto";
option java_package = "com.google.type";

message Date {
  int32 year = 1;
  int32 month = 2;
  int32 day = 3;
}

Then you'll generate the Java code with your Gradle task (or maven) and you will have access to Date, by importing like this:

import com.google.type.Date;

After that, it's recommended to map properties of the com.google.type.Date to whatever type you are using as Date, it is better to not store the com.google.type.Date generated type directly. This will give you more control on your type whereas if you stored the generated one, you could not have the same amount of freedom as a Java object.

note: if you are willing to work directly with Timestamp, there are ways to transform them to Date

Related