/
token.go
75 lines (64 loc) · 1.59 KB
/
token.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
68
69
70
71
72
73
74
75
package snapshot
import (
"encoding/base64"
"encoding/binary"
"github.com/jackc/pgtype"
"github.com/Permify/permify/internal/storage/postgres/types"
"github.com/Permify/permify/pkg/token"
)
type (
// Token - Structure for Token
Token struct {
Value types.XID8
}
// EncodedToken - Structure for EncodedToken
EncodedToken struct {
Value string
}
)
// NewToken - Creates a new snapshot token
func NewToken(value types.XID8) token.SnapToken {
return Token{
Value: value,
}
}
// Encode - Encodes the token to a string
func (t Token) Encode() token.EncodedSnapToken {
b := make([]byte, 8)
binary.LittleEndian.PutUint64(b, t.Value.Uint)
return EncodedToken{
Value: base64.StdEncoding.EncodeToString(b),
}
}
// Eg snapshot is equal to given snapshot
func (t Token) Eg(token token.SnapToken) bool {
ct, ok := token.(Token)
return ok && t.Value.Uint == ct.Value.Uint
}
// Gt snapshot is greater than given snapshot
func (t Token) Gt(token token.SnapToken) bool {
ct, ok := token.(Token)
return ok && t.Value.Uint > ct.Value.Uint
}
// Lt snapshot is less than given snapshot
func (t Token) Lt(token token.SnapToken) bool {
ct, ok := token.(Token)
return ok && t.Value.Uint < ct.Value.Uint
}
// Decode decodes the token from a string
func (t EncodedToken) Decode() (token.SnapToken, error) {
b, err := base64.StdEncoding.DecodeString(t.Value)
if err != nil {
return nil, err
}
return Token{
Value: types.XID8{
Uint: binary.LittleEndian.Uint64(b),
Status: pgtype.Present,
},
}, nil
}
// Decode decodes the token from a string
func (t EncodedToken) String() string {
return t.Value
}