go - 使用 go lang 在 protobuf 中创建数组指针

标签 go protocol-buffers grpc

我是 grpc 的新手,我已经使用名为 Accounts 的服务创建了 proto 文件,方法名称为 GetValidators,我想在使用 (gogoproto.customtype) 的消息中创建指向 BondedValidators 和 UnbondingValidators 变量的指针 []*。

syntax = 'proto3';
package grpc;

import "github.com/gogo/protobuf/gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "google/protobuf/struct.proto";

option (gogoproto.marshaler_all) = false;
option (gogoproto.unmarshaler_all) = false;
option (gogoproto.sizer_all) = false;
option (gogoproto.goproto_registration) = true;
option (gogoproto.messagename_all) = true;
option (gogoproto.protosizer_all) =false;

service Accounts {
rpc GetValidators(Empty) returns (ValidatorOutput);
}

message ValidatorOutput {
uint64 BlockHeight  = 1 ;       
repeated google.protobuf.ListValue  BondedValidators  = 2   [(gogoproto.customtype) = "github.com/gallactic/gallactic/core/validator.Validator", (gogoproto.nullable) = false];
repeated google.protobuf.ListValue  UnbondingValidators  = 3 [(gogoproto.customtype) = "github.com/gallactic/gallactic/core/validator.Validator", (gogoproto.nullable) = false];
}

每当使用以下代码时:

protoc -I/usr/local/include -I. -I$GOPATH/src-I$GOPATH/src/github.com/grpcecosystem/grpc-gateway/third_party/googleapis --gofast_out=plugins=grpc:./ ./protobuf/account.proto

它在 .pb.go 文件中生成输出,带有三个额外的变量

type ValidatorOutput struct {
BlockHeight          uint64                        `protobuf:"varint,1,opt,name=BlockHeight,proto3" json:"BlockHeight,omitempty"`
BondedValidators     []github_com_gallactic_gallactic_core_validator.Validator `protobuf:"bytes,2,rep,name=BondedValidators,customtype=github.com/gallactic/gallactic/core/validator.Validator" json:"BondedValidators"`
UnbondingValidators  []github_com_gallactic_gallactic_core_validator.Validator `protobuf:"bytes,3,rep,name=UnbondingValidators,customtype=github.com/gallactic/gallactic/core/validator.Validator" json:"UnbondingValidators"`
XXX_NoUnkeyedLiteral struct{}                                                   `json:"-"`
XXX_unrecognized     []byte                                                     `json:"-"`
XXX_sizecache        int32                                                      `json:"-"`
 }

输出应该是

type ValidatorOutput struct {
BlockHeight          uint64                                                     `protobuf:"varint,1,opt,name=BlockHeight,proto3" json:"BlockHeight,omitempty"`
BondedValidators     []*github_com_gallactic_gallactic_core_validator.Validator `protobuf:"bytes,2,rep,name=BondedValidators,customtype=github.com/gallactic/gallactic/core/validator.Validator" json:"BondedValidators"`
UnbondingValidators  []*github_com_gallactic_gallactic_core_validator.Validator `protobuf:"bytes,3,rep,name=UnbondingValidators,customtype=github.com/gallactic/gallactic/core/validator.Validator" json:"UnbondingValidators"`
}  

最佳答案

gogo proto 将在您使用 nullable=false 选项时删除指针,因此您需要删除 (gogoproto.nullable) = false 选项proto 定义让它生成 []*foo

关于go - 使用 go lang 在 protobuf 中创建数组指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52724781/

相关文章:

go - 如何使用连字符访问命令行参数但在 Go Lang 中没有值

go - emersion/go-imap - 如何检索和列出看不见的消息

protocol-buffers - 如何从 grpc Context.current() 访问属性?

import - 在 .proto 文件中使用导入时出错

戈朗 : what is atomic read used for?

select - golang : goroute with select doesn't stop unless I added a fmt. 打印()

go - 无法解析 .proto 文件中的导入

go - Go 程序的主 goroutine 和派生 goroutine 之间的区别

java - 如何管理 gRPC-java 服务器方法内的阻塞代码?

go - 如何确认来自 Go 客户端的 gRPC 流量是 TLS 加密的