Skip to content
This repository was archived by the owner on Oct 7, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/kafka/encode_decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (

"sync"

"github.com/inloco/kafka-elasticsearch-injector/src/models"
"github.com/inloco/kafka-elasticsearch-injector/src/schema_registry"
"github.com/Shopify/sarama"
"github.com/inloco/goavro"
"github.com/inloco/kafka-elasticsearch-injector/src/models"
"github.com/inloco/kafka-elasticsearch-injector/src/schema_registry"
)

// DecodeMessageFunc extracts a user-domain request object from an Kafka
Expand Down Expand Up @@ -93,12 +93,13 @@ func makeTimestamp(timestamp time.Time) int64 {
func (d *Decoder) JsonMessageToRecord(context context.Context, msg *sarama.ConsumerMessage) (*models.Record, error) {
var jsonValue map[string]interface{}
err := json.Unmarshal(msg.Value, &jsonValue)
jsonValue[kafkaTimestampKey] = makeTimestamp(msg.Timestamp)

if err != nil {
return nil, err
}

jsonValue[kafkaTimestampKey] = makeTimestamp(msg.Timestamp)

return &models.Record{
Topic: msg.Topic,
Partition: msg.Partition,
Expand Down
14 changes: 14 additions & 0 deletions src/kafka/encode_decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,17 @@ func TestDecoder_JsonMessageToRecord(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, val, returnedVal)
}

func TestDecoder_JsonMessageToRecord_MalformedJson(t *testing.T) {
d := &Decoder{CodecCache: sync.Map{}}
jsonBytes := []byte(`{"alo": 60"`)
record, err := d.JsonMessageToRecord(context.Background(), &sarama.ConsumerMessage{
Value: jsonBytes,
Topic: "test",
Partition: 1,
Offset: 54,
Timestamp: time.Now(),
})
assert.Nil(t, record)
assert.NotNil(t, err)
}