47 lines
1.1 KiB
Go
47 lines
1.1 KiB
Go
package postgres
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"strings"
|
|
"time"
|
|
|
|
"high-loud-apps-labs/internal/domain"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// Взял за основу что в inmemery делал
|
|
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()
|
|
}
|
|
if _, err := uuid.Parse(parts[2]); err != nil {
|
|
return time.Time{}, "", badCursor()
|
|
}
|
|
return t, parts[2], nil
|
|
}
|
|
|
|
func badCursor() error {
|
|
return &domain.ValidationError{
|
|
Field: "cursor",
|
|
Message: "некорректный формат",
|
|
}
|
|
}
|