【问题标题】:Create array pointer in protobuf using go lang使用 go lang 在 protobuf 中创建数组指针
【发布时间】:2018-10-09 15:42:19
【问题描述】:

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

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

【问题讨论】:

标签: go protocol-buffers grpc


【解决方案1】:

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

【讨论】:

  • 我去掉"(gogoproto.nullable) = false" 还是一样的,它生成BondedValidators []github_com_gallactic_gallactic_core_validator.Validator
  • 在这个conversation 中,您可以找到gogoproto.nullable 选项在proto3 语法中不起作用的原因。
猜你喜欢
  • 2014-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-12
  • 1970-01-01
  • 1970-01-01
  • 2022-06-23
相关资源
最近更新 更多