forked from rewardStyle/kinetic
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmessage.go
67 lines (56 loc) · 1.76 KB
/
message.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package kinetic
import (
"encoding/json"
"time"
"github.com/aws/aws-sdk-go/service/firehose"
"github.com/aws/aws-sdk-go/service/kinesis"
)
// Message represents an item on the Kinesis stream
type Message struct {
// For kinesis.Record
ApproximateArrivalTimestamp *time.Time
Data []byte
PartitionKey *string
SequenceNumber *string
// For kinesis.PutRecordRequestEntry
ExplicitHashKey *string
// For kinesis.PutRecordResultEntry
ErrorCode *string
ErrorMessage *string
ShardID *string
// For firehose.PutRecordBatchResponseEntry
RecordID *string
FailCount int
}
// FromRecord creates a message from the kinesis.Record returned from GetRecords
func FromRecord(record *kinesis.Record) *Message {
return &Message{
ApproximateArrivalTimestamp: record.ApproximateArrivalTimestamp,
Data: record.Data,
PartitionKey: record.PartitionKey,
SequenceNumber: record.SequenceNumber,
}
}
// RequestEntrySize calculates what the size (in bytes) of the message will be after calling ToRequestEntry on it and
// marshalling it to json
func (m *Message) RequestEntrySize() (int, error) {
buf, err := json.Marshal(m.ToRequestEntry())
if err != nil {
return 0, nil
}
return len(buf), nil
}
// ToRequestEntry creates a kinesis.PutRecordsRequestEntry to be used in the kinesis.PutRecords API call.
func (m *Message) ToRequestEntry() *kinesis.PutRecordsRequestEntry {
return &kinesis.PutRecordsRequestEntry{
Data: m.Data,
ExplicitHashKey: m.ExplicitHashKey,
PartitionKey: m.PartitionKey,
}
}
// ToFirehoseRecord creates a firehose.Record to be used in the firehose.PutRecordBatch API call.
func (m *Message) ToFirehoseRecord() *firehose.Record {
return &firehose.Record{
Data: m.Data,
}
}