Dan Jenson
Dan Jenson

Reputation: 1041

golang-grpc service - import types from another file

In my project I have a service_x.proto file for each service and a types.proto file for types shared across all services. However, when I compile for go, it puts them in separate packages; I also can't put go_package = 'service_x' at the top of the types.proto file because it will change for every service x where it is used. What is the cleanest way to compile my service and have access to the message types from types.proto in service_a.pb.go? Here is an example setup:

service_a.proto:

syntax = "proto3";
package service_a;

import "types.proto";

service ServiceA {
  rpc SomeRPC (SomeRPCRequest) returns (types.Result)
}

message SomeRPCRequest {
  string x = 1;
}

types.proto:

syntax = "proto3";
package types;

message Result {
  bool success = 1;
}

Upvotes: 0

Views: 1356

Answers (2)

Dan Jenson
Dan Jenson

Reputation: 1041

This was solved by making both proto files part of the 'pb' package; they can then be compiled to the same output directory (i.e. pb) and have access to symbols defined in both.

Upvotes: 0

Ravi Chandak
Ravi Chandak

Reputation: 113

Well you can import the types.proto in the service_a.proto file and use it

for example

syntax = "proto3";
package service_a;

//add this line 
import "types.proto"

service ServiceA {
  rpc SomeRPC (SomeRPCRequest) returns (types.Result)
}

message SomeRPCRequest {
  string x = 1;
}

you can read more here https://developers.google.com/protocol-buffers/docs/proto#importing-definitions

Upvotes: 1

Related Questions