Files
mtusi2026lab2/internal/service/cursor.go
T
Weinmaier_Andrew d8842a3de4
Skeleton contracts / contracts (pull_request) Canceled after 0s
lab2: postgres, sessions, redis, persistence, stale read
2026-10-09 21:30:36 +03:00

31 lines
885 B
Go

package service
import (
"encoding/base64"
"strings"
"time"
)
// encodeCursor кодирует (scope, created_at, id) в base64.
func encodeCursor(scope string, createdAt time.Time, id string) string {
raw := scope + "|" + createdAt.UTC().Format(time.RFC3339Nano) + "|" + id
return base64.RawURLEncoding.EncodeToString([]byte(raw))
}
// decodeCursor декодирует и проверяет scope.
func decodeCursor(cursor, expectedScope string) (time.Time, string, error) {
raw, err := base64.RawURLEncoding.DecodeString(cursor)
if err != nil {
return time.Time{}, "", ErrInvalidInput
}
parts := strings.SplitN(string(raw), "|", 3)
if len(parts) != 3 || parts[0] != expectedScope {
return time.Time{}, "", ErrInvalidInput
}
t, err := time.Parse(time.RFC3339Nano, parts[1])
if err != nil {
return time.Time{}, "", ErrInvalidInput
}
return t, parts[2], nil
}