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 }