go-zero icon indicating copy to clipboard operation
go-zero copied to clipboard

returning empty []int64 became nil in rpc

Open zekecc opened this issue 3 years ago • 5 comments

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

zekecc avatar Jul 29 '22 08:07 zekecc

A packed repeated field containing zero elements does not appear in the encoded message

GPRC协议规定的

szpnygo avatar Aug 16 '22 06:08 szpnygo

Example code?

kevwan avatar Aug 18 '22 00:08 kevwan

Is it controlled by the logic code in API service?

Would you please show me the full code on this issue?

kevwan avatar Aug 26 '22 12:08 kevwan

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

image

grpcui image

expect to return an empty slice as grpcui result { "pong": "1111", "Ids": [] }

zekecc avatar Aug 30 '22 04:08 zekecc

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Mar 19 '24 01:03 github-actions[bot]