gRPC生成的文件没有JSON定义。

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

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"`

huangapple
  • 本文由 发表于 2021年12月20日 00:28:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/70413269.html
匿名

发表评论

匿名网友

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

确定