ololo
ololo

Reputation: 2076

Importing Custom Proto Files other than google's

Importing proto files from google such as Struct is pretty much straightforward as shown below:

syntax = "proto3";
package messages;

import "google/protobuf/struct.proto";

message UnaryRequest{
    google.protobuf.Struct data = 1;
}

I would like to replicate same flow with my team such that instead of import "google/protobuf/struct.proto" we will have:

syntax = "proto3";
package messages;

import "myorg/protobuf/unary.proto"; //Notice difference here

message UnaryRequest{
    myorg.protobuf.UnaryData data = 1; //Notice difference here
}

Where import "myorg/protobuf/unary.proto" is expected to be retrieved from my orgs utility npm package which is reusable across internal microservices.

How can this be done?

Any ideas would be really appreciated.

Upvotes: 1

Views: 646

Answers (1)

jeanluc
jeanluc

Reputation: 1708

I don't think there's an automatic way for protoc to pull the npm package for you, but if you know where your orgs protos are gonna be after you pull them with npm, then you can include that path via --proto_path <path> option on protoc. The import ... statements will then resolve relative to all paths specified with that flag.

Not sure what build system you're using, but I bet you can automate this set up in most of them.

Upvotes: 2

Related Questions