Protobuf and Golang: How to generate a map string interface using proto3?

9,452 views
Skip to first unread message

je...@tune.com

unread,
Dec 19, 2017, 1:01:58 PM12/19/17
to Protocol Buffers

Hi, I am new in using Protobuf for golang.

https://github.com/golang/protobuf


Using golang Protobuf, within package foo, I created message TaskJobEntity which is a combination of other messages:


message TaskJobEntity {
   
JobEntity job = 1;
   
TaskEntity task = 2;
    map
<int64, JobEntity> dependent_jobs = 3;

}


Which is used within package foo response as part a map:


message ListTaskJobsResponse {
   
map<string, TaskJobEntity> task_jobs = 1;
   
ServiceStatus status = 2;
}

Now within another Protobuf package bar, I would like to define response that includes message TaskJobEntity defined in package foo, however, as far as I can determine, a proto3 package cannot reference another proto3 package (is this determination correct?).


My goal is to add map<string, foo.TaskJobEntity> task_jobs to package bar defined response.


Here is how I approaching to get to this goal. I have found that map of interface{} is not supported in Protobuf. So, is using google.protobuf.Any the next best solution?


message ImportStatsResponse {
   
string task_id = 1;
   
// map<string, mvregistrypb.TaskJobEntity> task_jobs = 2;
   
// map<string, interface{}> task_jobs = 2;
    map
<string, google.protobuf.Any> task_jobs = 2;
   
ServiceStatus status = 3;
}


Recommendations?


Feng Xiao

unread,
Dec 19, 2017, 1:27:08 PM12/19/17
to je...@tune.com, Protocol Buffers
On Tue, Dec 19, 2017 at 10:01 AM, <je...@tune.com> wrote:

Hi, I am new in using Protobuf for golang.

https://github.com/golang/protobuf


Using golang Protobuf, within package foo, I created message TaskJobEntity which is a combination of other messages:


message TaskJobEntity {
   
JobEntity job = 1;
   
TaskEntity task = 2;
    map
<int64, JobEntity> dependent_jobs = 3;

}


Which is used within package foo response as part a map:


message ListTaskJobsResponse {
   
map<string, TaskJobEntity> task_jobs = 1;
   
ServiceStatus status = 2;
}

Now within another Protobuf package bar, I would like to define response that includes message TaskJobEntity defined in package foo, however, as far as I can determine, a proto3 package cannot reference another proto3 package (is this determination correct?).

What makes you think that? You can certainly import and use other proto3 packages.
 


My goal is to add map<string, foo.TaskJobEntity> task_jobs to package bar defined response.


Here is how I approaching to get to this goal. I have found that map of interface{} is not supported in Protobuf. So, is using google.protobuf.Any the next best solution?


message ImportStatsResponse {
   
string task_id = 1;
   
// map<string, mvregistrypb.TaskJobEntity> task_jobs = 2;
   
// map<string, interface{}> task_jobs = 2;
    map
<string, google.protobuf.Any> task_jobs = 2;
   
ServiceStatus status = 3;
}


Recommendations?


--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+unsubscribe@googlegroups.com.
To post to this group, send email to prot...@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

je...@tune.com

unread,
Dec 19, 2017, 1:50:07 PM12/19/17
to Protocol Buffers
To be clear, what I meant by: "a proto3 package cannot reference another proto3 package"

To complete that statement, "within the same repository".

So, I am not sure if I can do a local import of a proto3 package.

There are multiple Protobuf packages within this repository, and each package is associated with a specific golang microservice.

protobuf

├── foo

│   ├── task_jobs.pb.go

│   ├── task_jobs.proto

│   ├──service.pb.go

│   └──service.proto

└── bar

      ├── service.pb.go

      └──  service.proto

services

├── foo

│   └──microservice Go code

└── bar

      └── microservice Go code
Reply all
Reply to author
Forward
0 new messages