go-zero
go-zero copied to clipboard
returning empty []int64 became nil in rpc
hi 我在rpc定义的了一个方法 返回了一个adIds []int64,在通过api调用接收后, 结构体被当作空值 未被解析,返回adIds 返回nil, 通过grpcui,是能够正确获取到 adIds的值为 [ ] , 去掉pb里面的 omitempty 也是一样为nil
type MediaMaterial struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Title string `protobuf:"bytes,1,opt,name=title,proto3" json:"title,omitempty"`
MatUrl string `protobuf:"bytes,2,opt,name=mat_url,json=matUrl,proto3" json:"mat_url,omitempty"`
Mid string `protobuf:"bytes,3,opt,name=mid,proto3" json:"mid,omitempty"`
Designer string `protobuf:"bytes,4,opt,name=designer,proto3" json:"designer,omitempty"`
AdNumber int64 `protobuf:"varint,5,opt,name=ad_number,json=adNumber,proto3" json:"ad_number,omitempty"`
CreateTime string `protobuf:"bytes,6,opt,name=create_time,json=createTime,proto3" json:"create_time,omitempty"`
Type int64 `protobuf:"varint,7,opt,name=type,proto3" json:"type,omitempty"`
AdIds []int64 `protobuf:"varint,8,rep,packed,name=ad_ids,json=adIds,proto3" json:"ad_ids,omitempty"`
}
A packed repeated field containing zero elements does not appear in the encoded message
GPRC协议规定的
Example code?
Is it controlled by the logic code in API service?
Would you please show me the full code on this issue?
sorry. I didn't see it until now
rpc logic return
func (l *PingLogic) Ping(in *pb.Request) (*pb.Response, error) {
// todo: add your logic here and delete this line
//Ids := make([]string, 0)
return &pb.Response{Pong: in.Ping, Ids: []string{}}, nil
}
api get rpc response
paymentResp, err := l.svcCtx.PaymentRpc.Ping(l.ctx, &pb.Request{
Ping: "11111",
})
if err != nil {
return nil, err
}
fmt.Printf("print === %+v \n", paymentResp)`
console
grpcui

expect to return an empty slice as grpcui result { "pong": "1111", "Ids": [] }
This issue is stale because it has been open for 30 days with no activity.