From afe3d487b1e485b575f6e6d26fac670e00d5fa16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 14 Aug 2023 11:44:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=94=9F=E4=BA=A7=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E7=9A=84=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define.go | 4 ++-- producer.go | 12 ++++-------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/define.go b/define.go index 21d3863..da708f5 100644 --- a/define.go +++ b/define.go @@ -13,8 +13,8 @@ package kafka // // Date : 4:13 下午 2021/9/21 type ProducerData struct { - Data interface{} // 发送的数据 - Key string // 分区key + Data map[interface{}]interface{} // 发送的数据 + Key string // 分区key } // ProducerResult 数据发送后的结果 diff --git a/producer.go b/producer.go index 138e748..e8f2eea 100644 --- a/producer.go +++ b/producer.go @@ -141,14 +141,10 @@ func (p *Producer) buildMessage(data ProducerData) *sarama.ProducerMessage { Key: sarama.StringEncoder(data.Key), Timestamp: time.Now(), } - switch v := data.Data.(type) { - case string: - mes.Value = sarama.StringEncoder(v) - case []byte: - mes.Value = sarama.ByteEncoder(v) - default: - byteData, _ := json.Marshal(data.Data) - mes.Value = sarama.ByteEncoder(byteData) + if nil == data.Data { + data.Data = make(map[interface{}]interface{}) } + byteData, _ := json.Marshal(data.Data) + mes.Value = sarama.ByteEncoder(byteData) return mes }