-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
46 lines (38 loc) · 1.01 KB
/
parse.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
package main
import (
"encoding/binary"
"fmt"
"time"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
)
func parseBatch(batch Batch) ([]byte, error) {
var batchData []byte
for _, value := range batch {
protoLog := &ProtoLog{
LogId: value.Log.LogId,
Timestamp: timeToTimestamp(value.Log.Timestamp),
Message: value.Log.Message,
}
// serialize the protobuf message
data, err := proto.Marshal(protoLog)
if err != nil {
return nil, fmt.Errorf("error serializing protobuf message: %w", err)
}
data = prependLittleEndianSize(data)
batchData = append(batchData, data...)
}
return batchData, nil
}
func prependLittleEndianSize(data []byte) []byte {
size := len(data)
sizeBytes := make([]byte, 4)
binary.LittleEndian.PutUint32(sizeBytes, uint32(size))
return append(sizeBytes, data...)
}
func timeToTimestamp(t time.Time) *timestamppb.Timestamp {
return ×tamppb.Timestamp{
Seconds: int64(t.Unix()),
Nanos: int32(t.Nanosecond()),
}
}