how to Introduce other proto files?

Viewed 53

My question: The dependency could not be found in the generated file. result

This is my project structure:

./api
├── auth
│   └── service
│       └── v1
│           ├── auth.pb.go
│           └── auth.proto
└── ws
    └── v1
        └── ws.proto

auth.proto:

syntax = "proto3";
package api.auth.service.v1;
import "api/ws/v1/ws.proto";
option go_package = "api/auth/service/v1;v1";

service Auth {
  rpc UserRegister(UserRegisterReq) returns(UserRegisterResp);
}

message CommonResp{
  int32   errCode = 1;
  string  errMsg = 2;
}

message UserRegisterReq {
  api.ws.v1.UserInfo userInfo = 1;
  string operationID = 2;
}

message UserRegisterResp {
  CommonResp CommonResp = 1;
}

ws.proto:

syntax = "proto3";
package api.ws.v1;
option go_package = "api/ws/v1;v1";

message UserInfo{
  string userID = 1;
  string nickname = 2;
  string faceURL = 3;
  int32 gender = 4;
  string phoneNumber = 5;
  uint32 birth = 6;
  string email = 7;
  string ex = 8;
  string createIp = 9;
  uint32 createTime = 10;
  string LastLoginIp =11;
  uint32 LastLoginTime = 12;
  int32  LoginTimes = 13;
  int32 LoginLimit = 14;
  int32 appMangerLevel = 15;
  int32  globalRecvMsgOpt = 16;
  string  invitationCode = 17;
}

go.mod:

module Janna-IM

go 1.17

require (
    github.com/go-kratos/kratos/v2 v2.5.0
    github.com/google/wire v0.5.0
    google.golang.org/genproto v0.0.0-20220524023933-508584e28198
    google.golang.org/grpc v1.46.2
    google.golang.org/protobuf v1.28.0
)

require (
    github.com/fsnotify/fsnotify v1.5.4 // indirect
    github.com/go-logr/logr v1.2.3 // indirect
    github.com/go-logr/stdr v1.2.2 // indirect
    github.com/go-playground/form/v4 v4.2.0 // indirect
    github.com/golang/protobuf v1.5.2 // indirect
    github.com/google/uuid v1.3.0 // indirect
    github.com/gorilla/mux v1.8.0 // indirect
    github.com/imdario/mergo v0.3.12 // indirect
    go.opentelemetry.io/otel v1.9.0 // indirect
    go.opentelemetry.io/otel/sdk v1.9.0 // indirect
    go.opentelemetry.io/otel/trace v1.9.0 // indirect
    golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 // indirect
    golang.org/x/sync v0.0.0-20220513210516-0976fa681c29 // indirect
    golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
    golang.org/x/text v0.3.7 // indirect
    gopkg.in/yaml.v3 v3.0.0 // indirect
)

I executed this command $ protoc --proto_path=. --go_out=. ./api/auth/service/v1/auth.proto at the root of the project.

After executing this command, you will get a new file auth.pb.go, The expected error will occur in this file.

Thank you for your help!

1 Answers

Execute it at the root directory.

$cd Janna-IM

$protoc -I=. --go_out=../../ ws/v1/ws.proto

$protoc -I=. --go_out=. auth/service/v1/auth.proto

auth.proto:

syntax = "proto3";

package pbAuth;
option go_package = "auth/service/v1;pbAuth";

import "ws/v1/ws.proto";

service Auth {
  rpc UserRegister(UserRegisterReq) returns(UserRegisterResp);
}

message CommonResp{
  int32   errCode = 1;
  string  errMsg = 2;
}

message UserRegisterReq {
  server_api_params.UserInfo userInfo = 1;
  string operationID = 2;
}

message UserRegisterResp {
  CommonResp CommonResp = 1;
}

ws.proto:

syntax = "proto3";
package server_api_params;
option go_package = "Janna-IM/api/ws/v1;server_api_params";

message UserInfo{
  string userID = 1;
  string nickname = 2;
}

How import is written depends on where in terminal the protoc command.

Related