英文:
gRPC generated file doesn't have json definition
问题
我正在尝试从proto文件生成Go文件,但方法的输入定义中没有json定义。我应该自己添加json定义,还是我的脚本有问题?谢谢,非常感谢你的帮助。
Proto文件
message RateRequest {
string Base = 1;
string Destination = 2;
}
生成的文件
type RateRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
//这里没有json定义
Base string `protobuf:"bytes,1,opt,name=Base,proto3" json:"Base,omitempty"`
Destination string `protobuf:"bytes,2,opt,name=Destination,proto3" json:"Destination,omitempty"`
}
Protoc脚本
protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
protos/currency.proto
grpcurl
grpcurl --plaintext -d '{Base: "GBP", Destination: "USD"}' localhost:9092 Currency.GetRate
// 调用方法"Currency.GetRate"时出错:获取请求数据时出错:消息类型RateRequest没有名为base的已知字段
英文:
I'm trying to generate Go file from proto file but it doesn't have json definition in the method's input definition. Should I add the json definition by myself or there were something wrong with my script. Thank you, I sincerely appreciate your help.
Proto file
message RateRequest {
string Base = 1;
string Destination = 2;
}
Generated file
type RateRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
//No json definition here
Base string `protobuf:"bytes,1,opt,name=Base,proto3" json:"Base,omitempty"`
Destination string `protobuf:"bytes,2,opt,name=Destination,proto3" json:"Destination,omitempty"`
}
Protoc script
protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
protos/currency.proto
grpcurl
grpcurl --plaintext -d '{Base: "GBP", Destination: "USD"}' localhost:9092 Currency.GetRate
// Error invoking method "Currency.GetRate": error getting request data: message type RateRequest has no known field named base
答案1
得分: 0
由于错误是
> error getting request data: message type RateRequest has no known field named base
而 json
是
json:"Base,omitempty"`
那么看起来它正在寻找错误的字段,应该是
json:"base,omitempty"`
英文:
Since the error is
> error getting request data: message type RateRequest has no known field named base
And the json
is
json:"Base,omitempty"`
Then it seems it's looking for the wrong field, it should be
json:"base,omitempty"`
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论