Files
mtusi2026lab2/backend/internal/storage/memory/cursor.go
T

48 lines
1.1 KiB
Go

package memory
import (
"encoding/base64"
"strings"
"time"
"high-loud-apps-labs/internal/domain"
)
func encodeCursor(scope string, createdAt time.Time, id string) string {
raw := scope + "|" + createdAt.UTC().Format(time.RFC3339Nano) + "|" + id
return base64.RawURLEncoding.EncodeToString([]byte(raw))
}
func decodeCursor(cursor, expectedScope string) (time.Time, string, error) {
data, err := base64.RawURLEncoding.DecodeString(cursor)
if err != nil {
return time.Time{}, "", badCursor()
}
parts := strings.SplitN(string(data), "|", 3)
if len(parts) != 3 {
return time.Time{}, "", badCursor()
}
if parts[0] != expectedScope {
return time.Time{}, "", badCursor()
}
t, err := time.Parse(time.RFC3339Nano, parts[1])
if err != nil {
return time.Time{}, "", badCursor()
}
return t, parts[2], nil
}
func badCursor() error {
return &domain.ValidationError{
Field: "cursor",
Message: "некорректный формат",
}
}
func lessThan(aTime time.Time, aID string, bTime time.Time, bID string) bool {
if aTime.Equal(bTime) {
return aID < bID
}
return aTime.Before(bTime)
}