Skip to content

Commit 9310f92

Browse files
committed
consume: distinguishes nil and empty slice. #67
1 parent 1abcd68 commit 9310f92

File tree

1 file changed

+23
-30
lines changed

1 file changed

+23
-30
lines changed

consume.go

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -361,43 +361,36 @@ type consumedMessage struct {
361361
}
362362

363363
func newConsumedMessage(m *sarama.ConsumerMessage, encodeKey, encodeValue string) consumedMessage {
364-
result := consumedMessage{Partition: m.Partition, Offset: m.Offset}
364+
result := consumedMessage{
365+
Partition: m.Partition,
366+
Offset: m.Offset,
367+
Key: encodeBytes(m.Key, encodeKey),
368+
Value: encodeBytes(m.Value, encodeValue),
369+
}
365370

366371
if !m.Timestamp.IsZero() {
367372
result.Timestamp = &m.Timestamp
368373
}
369374

370-
if len(m.Value) == 0 {
371-
result.Value = nil
372-
} else {
373-
var str string
374-
switch encodeValue {
375-
case "hex":
376-
str = hex.EncodeToString(m.Value)
377-
case "base64":
378-
str = base64.StdEncoding.EncodeToString(m.Value)
379-
default:
380-
str = string(m.Value)
381-
}
382-
result.Value = &str
383-
}
384-
385-
if len(m.Key) == 0 {
386-
result.Key = nil
387-
} else {
388-
var str string
389-
switch encodeKey {
390-
case "hex":
391-
str = hex.EncodeToString(m.Key)
392-
case "base64":
393-
str = base64.StdEncoding.EncodeToString(m.Key)
394-
default:
395-
str = string(m.Key)
396-
}
397-
result.Key = &str
375+
return result
376+
}
377+
378+
func encodeBytes(data []byte, encoding string) *string {
379+
if data == nil {
380+
return nil
398381
}
399382

400-
return result
383+
var str string
384+
switch encoding {
385+
case "hex":
386+
str = hex.EncodeToString(data)
387+
case "base64":
388+
str = base64.StdEncoding.EncodeToString(data)
389+
default:
390+
str = string(data)
391+
}
392+
393+
return &str
401394
}
402395

403396
func (cmd *consumeCmd) partitionLoop(out chan printContext, pc sarama.PartitionConsumer, p int32, end int64) {

0 commit comments

Comments
 (0)