Files
mtusi2026lab7/internal/platform/pagination/page.go
T
kudesn1k 8054913ef9
Application / e2e (pull_request) Blocked by required conditions
Application / unit (pull_request) Blocked by required conditions
Skeleton contracts / contracts (pull_request) Blocked by required conditions
Перенеси реализацию мессенджера из ЛР 6
Источник — проверенное рабочее дерево ЛР 6 поверх 18878d6, включая изменения после повторного ревью. SHA-256 списка исходных файлов: e45a3d85b41feff1da78dce3f20fd1f4af1c5ee629e6ed703218bc8e21ad58fe.
2026-11-30 10:54:41 +03:00

42 lines
1.2 KiB
Go

package pagination
import "github.com/google/uuid"
// Page is the service-level page: the items and the id to continue from,
// nil when the listing is exhausted. It carries the raw keyset id so that any
// transport can encode the continuation its own way.
type Page[T any] struct {
Items []T
Next *uuid.UUID
}
// PageResponse is the wire form of a page with the continuation already
// encoded as an opaque cursor.
type PageResponse[T any] struct {
Items []T `json:"items"`
NextCursor *string `json:"next_cursor"`
}
// Cut turns a slice over-fetched by one item into a page of at most limit
// items; the extra item only proves that a next page exists.
func Cut[T any](items []T, limit int, id func(T) uuid.UUID) Page[T] {
if len(items) <= limit {
return Page[T]{Items: items}
}
items = items[:limit]
next := id(items[limit-1])
return Page[T]{Items: items, Next: &next}
}
func ToResponse[D, R any](p Page[D], scope uuid.UUID, convert func(D) R) PageResponse[R] {
out := PageResponse[R]{Items: make([]R, 0, len(p.Items))}
for _, item := range p.Items {
out.Items = append(out.Items, convert(item))
}
if p.Next != nil {
cursor := EncodeCursor(scope, *p.Next)
out.NextCursor = &cursor
}
return out
}