如何引入其他的proto文件?

huangapple go评论97阅读模式
英文:

how to Introduce other proto files?

问题

我的问题:在生成的文件中找不到依赖项。
如何引入其他的proto文件?

这是我的项目结构:

./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
)

我在项目的根目录下执行了这个命令 $ protoc --proto_path=. --go_out=. ./api/auth/service/v1/auth.proto

执行这个命令后,你将会得到一个新的文件 auth.pb.go,预期的错误将会出现在这个文件中。

谢谢你的帮助!

英文:

My question: The dependency could not be found in the generated file.
如何引入其他的proto文件?

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

得分: 1

在终端中执行以下命令,位于根目录下:

$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;
}

如何导入取决于在终端中的protoc命令的位置。

英文:

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.

huangapple
  • 本文由 发表于 2022年9月7日 21:17:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/73636235.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定