Skip to content

Instantly share code, notes, and snippets.

@var23rav
Last active May 4, 2022 07:49
Show Gist options
  • Save var23rav/c71cccc649fc00c19fad08aaee52aad0 to your computer and use it in GitHub Desktop.
Save var23rav/c71cccc649fc00c19fad08aaee52aad0 to your computer and use it in GitHub Desktop.
Generate Go file for protos with import dependencies using Protoc, a Reader service added to the proto
syntax = "proto3";
package car;
import "common/vehicle_info.proto";
message Car {
vehicle.Vehicle info = 1;
string carType = 2;
}
service Reader {
rpc GetPriceInfo(GetPriceInfoRequest) returns (GetPriceInfoResponse);
}
message GetPriceInfoRequest {
string manufacturer = 1;
string model = 2;
}
message GetPriceInfoResponse {
vehicle.PurchaseEntity info = 1;
}
syntax = "proto3";
package vehicle;
message PurchaseEntity {
string manufacturer = 1;
string model = 2;
int32 price = 3;
}
message Vehicle {
PurchaseEntity info = 1;
string engineType = 2;
int32 wheelCount = 3;
int32 registrationNumber = 4;
}
@var23rav
Copy link
Author

var23rav commented Apr 13, 2022

Step by step action can be found in the medium.

Note that i was using windows for my development thus the use of ^ for multi line command replace it with \ for linux based os.

@var23rav
Copy link
Author

var23rav commented May 4, 2022

The above method will only generate the message part of the proto.

If you want to generate you service code too, you have to do one more step.

protoc ^
--proto_path=./proto_folder ^
--go-grpc_out=./generated_codes ^
--go-grpc_opt=Mcar/car_info.proto=generated_code_with_go_opt/car_pkg ^
car/car_info.proto

They have separated these step into two to make the code/release more maintainable.

@var23rav
Copy link
Author

var23rav commented May 4, 2022

The difference,
generation of message uses go_out with go_opt.
but for service part we use go-grpc_out with go-grpc_opt

@var23rav
Copy link
Author

var23rav commented May 4, 2022

You can generate them together using..

protoc ^
    --proto_path=./proto_folder     ^
                                    ^
    --go_out=./generated_codes      ^
    --go_opt=Mcommon/vehicle_info.proto=generated_code_with_go_opt/vehicle_pkg ^
    --go_opt=Mcar/car_info.proto=generated_code_with_go_opt/car_pkg ^
                                    ^
    --go-grpc_out=./generated_codes ^
    --go-grpc_opt=Mcar/car_info.proto=generated_code_with_go_opt/car_pkg ^
                                    ^
    common/vehicle_info.proto       ^
    car/car_info.proto

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment