mirror of
https://github.com/chaitin/MonkeyCode.git
synced 2026-11-22 19:39:31 +08:00
234 lines
5.5 KiB
Cheetah
234 lines
5.5 KiB
Cheetah
{{ define "cursor" }}
|
|
|
|
{{/* 头部引用 */}}
|
|
{{ $pkg := base $.Config.Package }}
|
|
{{ template "header" $ }}
|
|
|
|
// Extra imports required by this file.
|
|
import (
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"fmt"
|
|
"entgo.io/ent/dialect/sql"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type Cursor struct {
|
|
Cursor string `json:"cursor,omitempty"` // 游标
|
|
HasNextPage bool `json:"has_next_page"` // 是否有下一页
|
|
}
|
|
|
|
type createdAtCursor struct {
|
|
ID any `json:"id"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
}
|
|
|
|
func (i *createdAtCursor) marshal() (string, error) {
|
|
if i == nil {
|
|
return "", nil
|
|
}
|
|
|
|
milli := i.CreatedAt.UnixMilli()
|
|
b36 := strconv.FormatInt(milli, 36)
|
|
|
|
switch ii := i.ID.(type) {
|
|
case string:
|
|
return fmt.Sprintf("s.%s.%s", b36, ii), nil
|
|
|
|
case uuid.UUID:
|
|
bb, err := ii.MarshalBinary()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
is := base64.RawURLEncoding.EncodeToString(bb)
|
|
return fmt.Sprintf("u.%s.%s", b36, is), nil
|
|
default:
|
|
return "", fmt.Errorf("unsupported ID type")
|
|
}
|
|
}
|
|
|
|
func unmarshalCreatedAt(s string) (*createdAtCursor, error) {
|
|
if s == "" {
|
|
return nil, nil
|
|
}
|
|
parts := strings.Split(s, ".")
|
|
if len(parts) != 3 {
|
|
return nil, fmt.Errorf("invalid cursor format")
|
|
}
|
|
milli, err := strconv.ParseInt(parts[1], 36, 64)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
switch parts[0] {
|
|
case "s":
|
|
return &createdAtCursor{
|
|
CreatedAt: time.UnixMilli(milli),
|
|
ID: parts[2],
|
|
}, nil
|
|
|
|
case "u":
|
|
id, err := base64.RawURLEncoding.DecodeString(parts[2])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
uuid, err := uuid.FromBytes(id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &createdAtCursor{
|
|
CreatedAt: time.UnixMilli(milli),
|
|
ID: uuid,
|
|
}, nil
|
|
default:
|
|
return nil, fmt.Errorf("invalid cursor prefix")
|
|
}
|
|
}
|
|
|
|
type updateAtCursor struct {
|
|
ID any `json:"id"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
func (i *updateAtCursor) marshal() (string, error) {
|
|
if i == nil {
|
|
return "", nil
|
|
}
|
|
|
|
milli := i.UpdatedAt.UnixMilli()
|
|
b36 := strconv.FormatInt(milli, 36)
|
|
|
|
switch ii := i.ID.(type) {
|
|
case string:
|
|
return fmt.Sprintf("s.%s.%s", b36, ii), nil
|
|
|
|
case uuid.UUID:
|
|
bb, err := ii.MarshalBinary()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
is := base64.RawURLEncoding.EncodeToString(bb)
|
|
return fmt.Sprintf("u.%s.%s", b36, is), nil
|
|
default:
|
|
return "", fmt.Errorf("unsupported ID type")
|
|
}
|
|
}
|
|
|
|
func unmarshalUpdatedAt(s string) (*updateAtCursor, error) {
|
|
if s == "" {
|
|
return nil, nil
|
|
}
|
|
parts := strings.Split(s, ".")
|
|
if len(parts) != 3 {
|
|
return nil, fmt.Errorf("invalid cursor format")
|
|
}
|
|
milli, err := strconv.ParseInt(parts[1], 36, 64)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
switch parts[0] {
|
|
case "s":
|
|
return &updateAtCursor{
|
|
UpdatedAt: time.UnixMilli(milli),
|
|
ID: parts[2],
|
|
}, nil
|
|
|
|
case "u":
|
|
id, err := base64.RawURLEncoding.DecodeString(parts[2])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
uuid, err := uuid.FromBytes(id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &updateAtCursor{
|
|
UpdatedAt: time.UnixMilli(milli),
|
|
ID: uuid,
|
|
}, nil
|
|
default:
|
|
return nil, fmt.Errorf("invalid cursor prefix")
|
|
}
|
|
}
|
|
|
|
|
|
{{ range $n := $.Nodes }}
|
|
{{/* 检查 Annotation(使用内置模板函数) */}}
|
|
{{- with $cur := $n.Annotations.CursorPagination -}}
|
|
func (q *{{ $n.QueryName }}) After(ctx context.Context, cursor string, limit int) ([]*{{ $n.Name }}, *Cursor, error) {
|
|
{{- if eq $cur.kind "updated_at" }}
|
|
i, err := unmarshalUpdatedAt(cursor)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
q.Order({{ $n.Package }}.ByUpdatedAt(sql.OrderDesc()), {{ $n.Package }}.ByID(sql.OrderDesc()))
|
|
{{- else }}
|
|
i, err := unmarshalCreatedAt(cursor)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
q.Order({{ $n.Package }}.ByCreatedAt(sql.OrderDesc()), {{ $n.Package }}.ByID(sql.OrderDesc()))
|
|
{{- end }}
|
|
q.Limit(limit + 1)
|
|
|
|
if i != nil {
|
|
q.Where(func(s *sql.Selector) {
|
|
s.Where(sql.Or(
|
|
{{- if eq $cur.kind "updated_at" }}
|
|
sql.LT(s.C("updated_at"), i.UpdatedAt),
|
|
sql.And(
|
|
sql.EQ(s.C("updated_at"), i.UpdatedAt),
|
|
sql.LT(s.C("id"), i.ID),
|
|
),
|
|
{{- else }}
|
|
sql.LT(s.C("created_at"), i.CreatedAt),
|
|
sql.And(
|
|
sql.EQ(s.C("created_at"), i.CreatedAt),
|
|
sql.LT(s.C("id"), i.ID),
|
|
),
|
|
{{- end }}
|
|
))
|
|
})
|
|
}
|
|
nodes, err := q.All(ctx)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
res := &Cursor{}
|
|
if len(nodes) > limit {
|
|
res.HasNextPage = true
|
|
nodes = nodes[:limit]
|
|
}
|
|
|
|
if len(nodes) > 0 {
|
|
last := nodes[len(nodes)-1]
|
|
{{- if eq $cur.kind "updated_at" }}
|
|
i := &updateAtCursor{
|
|
UpdatedAt: last.UpdatedAt,
|
|
ID: last.ID,
|
|
}
|
|
cursor, err := i.marshal()
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
res.Cursor = cursor
|
|
{{- else }}
|
|
i := &createdAtCursor{
|
|
CreatedAt: last.CreatedAt,
|
|
ID: last.ID,
|
|
}
|
|
cursor, err := i.marshal()
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
res.Cursor = cursor
|
|
{{- end }}
|
|
}
|
|
return nodes, res, nil
|
|
}
|
|
{{ end }}
|
|
{{ end }}
|
|
|
|
{{ end }} |