-
Notifications
You must be signed in to change notification settings - Fork 90
/
cursor.go
53 lines (43 loc) · 1.05 KB
/
cursor.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
package cursor
import (
"strconv"
"github.com/pkg/errors"
)
type Cursor interface {
Comparable(Cursor) bool
Equal(Cursor) bool
Before(Cursor) bool
After(Cursor) bool
}
type SequenceCursor struct {
cursor uint64
}
func MustParse(s string) Cursor {
if c, err := strconv.ParseUint(s, 10, 64); err == nil {
return SequenceCursor{cursor: c}
}
panic(errors.Errorf("cannot use %q to construct cursor", s))
}
func NewCursor(s string) (Cursor, error) {
if c, err := strconv.ParseUint(s, 10, 64); err == nil {
return SequenceCursor{cursor: c}, nil
}
return nil, errors.Errorf("cannot use %q to construct cursor", s)
}
func (c SequenceCursor) Comparable(o Cursor) bool {
switch o.(type) {
case SequenceCursor, *SequenceCursor:
return true
default:
return false
}
}
func (c SequenceCursor) Equal(o Cursor) bool {
return c.cursor == o.(SequenceCursor).cursor
}
func (c SequenceCursor) Before(o Cursor) bool {
return c.cursor < o.(SequenceCursor).cursor
}
func (c SequenceCursor) After(o Cursor) bool {
return c.cursor > o.(SequenceCursor).cursor
}