Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Amazon MSK event structure #316

Merged
merged 4 commits into from
Oct 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions events/msk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
ewbankkit marked this conversation as resolved.
Show resolved Hide resolved

package events

type MskEvent struct {
ewbankkit marked this conversation as resolved.
Show resolved Hide resolved
EventSource string `json:"eventSource"`
EventSourceARN string `json:"eventSourceArn"`
Records map[string][]MskRecord `json:"records"`
}

type MskRecord struct {
ewbankkit marked this conversation as resolved.
Show resolved Hide resolved
Topic string `json:"topic"`
Partition int64 `json:"partition"`
Offset int64 `json:"offset"`
Timestamp MilliSecondsEpochTime `json:"timestamp"`
TimestampType string `json:"timestampType"`
Key string `json:"key,omitempty"`
Value string `json:"value,omitempty"`
}
42 changes: 42 additions & 0 deletions events/msk_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
ewbankkit marked this conversation as resolved.
Show resolved Hide resolved
package events

import (
"encoding/json"
"testing"

"github.com/aws/aws-lambda-go/events/test"
"github.com/stretchr/testify/assert"
)

func TestMskEventMarshaling(t *testing.T) {

// 1. read JSON from file
inputJson := test.ReadJSONFromFile(t, "./testdata/msk-event.json")

// 2. de-serialize into Go object
var inputEvent MskEvent
if err := json.Unmarshal(inputJson, &inputEvent); err != nil {
t.Errorf("could not unmarshal event. details: %v", err)
}

for _, records := range inputEvent.Records {
for _, record := range records {
utc := record.Timestamp.UTC()
assert.Equal(t, 2020, utc.Year())
}
}

// 3. serialize to JSON
outputJson, err := json.Marshal(inputEvent)
if err != nil {
t.Errorf("could not marshal event. details: %v", err)
}

// 4. check result
assert.JSONEq(t, string(inputJson), string(outputJson))
}

func TestMskMarshalingMalformedJson(t *testing.T) {
test.TestMalformedJson(t, MskEvent{})
}
17 changes: 17 additions & 0 deletions events/testdata/msk-event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"eventSource": "aws:kafka",
"eventSourceArn": "arn:aws:kafka:us-west-2:012345678901:cluster/ExampleMSKCluster/e9f754c6-d29a-4430-a7db-958a19fd2c54-4",
"records": {
"AWSKafkaTopic-0": [
{
"topic": "AWSKafkaTopic",
"partition": 0,
"offset": 0,
"timestamp": 1595035749700,
"timestampType": "CREATE_TIME",
"key": "OGQ1NTk2YjQtMTgxMy00MjM4LWIyNGItNmRhZDhlM2QxYzBj",
"value": "OGQ1NTk2YjQtMTgxMy00MjM4LWIyNGItNmRhZDhlM2QxYzBj"
}
]
}
}