func()

in datahub/requestmodel.go [519:585]


func (pr *PutPBRecordsRequest) requestBodyEncode() ([]byte, error) {
	res := make([]*pbmodel.RecordEntry, len(pr.Records))
	for idx, val := range pr.Records {
		bRecord := val.GetBaseRecord()
		data := val.GetData()

		fds := make([]*pbmodel.FieldData, 0)
		switch val := data.(type) {
		case []byte:
			fd := &pbmodel.FieldData{
				Value: val,
			}
			fds = append(fds, fd)
		default:
			v, ok := data.([]interface{})
			if !ok {
				return nil, fmt.Errorf("data format is invalid")
			}
			for _, str := range v {
				fd := &pbmodel.FieldData{}
				if str == nil {
					fd.Value = nil
				} else {
					fd.Value = []byte(fmt.Sprintf("%s", str))
				}
				fds = append(fds, fd)
			}
		}
		rd := &pbmodel.RecordData{
			Data: fds,
		}

		recordEntry := &pbmodel.RecordEntry{
			ShardId: proto.String(bRecord.ShardId),
			Data:    rd,
		}

		if len(bRecord.Attributes) > 0 {
			sps := make([]*pbmodel.StringPair, len(bRecord.Attributes))
			index := 0
			for k, v := range bRecord.Attributes {
				strv := fmt.Sprintf("%v", v)
				sp := &pbmodel.StringPair{
					Key:   proto.String(k),
					Value: proto.String(strv),
				}
				sps[index] = sp
				index++
			}
			ra := &pbmodel.RecordAttributes{
				Attributes: sps,
			}
			recordEntry.Attributes = ra
		}
		res[idx] = recordEntry
	}

	prr := &pbmodel.PutRecordsRequest{
		Records: res,
	}
	buf, err := proto.Marshal(prr)
	if err != nil {
		return nil, err
	}
	x := util.WrapMessage(buf)
	return x, nil
}