当前位置: 首页 > 工具软件 > Proto.Menu > 使用案例 >

go krotos,proto文件引用自定义proto(例如validate.proto)

干弘深
2023-12-01

1、proto文件中引用自定义proto(例如validate.proto)
helloworld.proto中引用validate.proto(Goland中,此时"validate/validate.proto"会显示红色),对http请求的入参做校验

syntax = "proto3";

package api.helloworld.v1;

import "google/api/annotations.proto";
import "validate/validate.proto";

option go_package = "kratosDemo/api/helloworld/v1;v1";
option java_multiple_files = true;
option java_package = "api.helloworld.v1";

service Helloworld {
	rpc CreateHelloworld (CreateHelloworldRequest) returns (CreateHelloworldReply){
		option (google.api.http) = {
			get: "/hw/{name}"
		};
	};
}

message CreateHelloworldRequest {
	string name = 1[(validate.rules).string = {min_len: 5, max_len: 100}];
}
message CreateHelloworldReply {
	string message=1;
}

2、kratos proto client api/helloworld/v1/helloworld.proto(生成相关pb.go文件)
此时,出现如下错误,无法定位自定义proto文件

xxx@xxx:~/go/src/kratosDemo$ kratos proto client api/helloworld/v1/helloworld.proto
google/protobuf/duration.proto: File not found.
google/protobuf/timestamp.proto: File not found.
validate/validate.proto:8:1: Import "google/protobuf/duration.proto" was not found or had errors.
validate/validate.proto:9:1: Import "google/protobuf/timestamp.proto" was not found or had errors.

解决方案:
1.File | Settings | Plugins,添加Protobuf组件,OK-Apply
2.File | Settings | Languages & Frameworks | Protobuf,添加当前项目的third_party目录,OK-Apply

3、重新使用kratos proto client api/helloworld/v1/helloworld.proto命令生成相关pb.go文件,1中import自定义proto的红色提示也会消失

注意:缺失的proto文件,需用户自行编写或下载,存放在krotos项目的third_party目录(框架定义的第三方proto存放路径)

 类似资料: