/
message.go
52 lines (44 loc) · 1.14 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
package message
import (
"errors"
"fmt"
"strings"
"time"
"github.com/klev-dev/kleverr"
)
const (
// OffsetOldest represents the smallest offset still available
// Use it to consume all messages, starting at the first available
OffsetOldest int64 = -2
// OffsetNewest represents the offset that will be used for the next produce
// Use it to consume only new messages
OffsetNewest int64 = -1
// OffsetInvalid is the offset returned when error is detected
OffsetInvalid int64 = -3
)
var ErrInvalidOffset = errors.New("invalid offset")
var ErrNotFound = errors.New("not found")
func ValidateOffset(offset int64) error {
if offset < OffsetOldest {
return kleverr.Newf("%w: %d is not a valid offset", ErrInvalidOffset, offset)
}
return nil
}
type Message struct {
Offset int64
Time time.Time
Key []byte
Value []byte
}
var Invalid = Message{Offset: OffsetInvalid}
func Gen(count int) []Message {
var msgs = make([]Message, count)
for i := range msgs {
msgs[i] = Message{
Time: time.Date(2023, 1, 1, 0, 0, i, 0, time.UTC),
Key: []byte(fmt.Sprintf("%10d", i)),
Value: []byte(strings.Repeat(" ", 128)),
}
}
return msgs
}