Skip to content

Commit

Permalink
fromLast => fromEnd
Browse files Browse the repository at this point in the history
  • Loading branch information
molon committed Oct 7, 2024
1 parent 8d545db commit 8135871
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 17 deletions.
4 changes: 2 additions & 2 deletions cursor/keyset.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

type KeysetFinder[T any] interface {
Find(ctx context.Context, after, before *map[string]any, orderBys []relay.OrderBy, limit int, fromLast bool) ([]T, error)
Find(ctx context.Context, after, before *map[string]any, orderBys []relay.OrderBy, limit int, fromEnd bool) ([]T, error)
Count(ctx context.Context) (int, error)
}

Expand Down Expand Up @@ -45,7 +45,7 @@ func NewKeysetAdapter[T any](finder KeysetFinder[T]) relay.ApplyCursorsFunc[T] {
if req.Limit <= 0 || (totalCount != nil && *totalCount <= 0) {
edges = make([]relay.LazyEdge[T], 0)
} else {
nodes, err := finder.Find(ctx, after, before, req.OrderBys, req.Limit, req.FromLast)
nodes, err := finder.Find(ctx, after, before, req.OrderBys, req.Limit, req.FromEnd)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions cursor/offset.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ func NewOffsetAdapter[T any](finder OffsetFinder[T]) relay.ApplyCursorsFunc[T] {
totalCount = &count
}

if req.FromLast && before == nil {
if req.FromEnd && before == nil {
if totalCount == nil {
return nil, errors.New("totalCount is required for fromLast and nil before")
return nil, errors.New("totalCount is required for fromEnd and nil before")
}
before = totalCount
}
Expand All @@ -61,7 +61,7 @@ func NewOffsetAdapter[T any](finder OffsetFinder[T]) relay.ApplyCursorsFunc[T] {
if limit > rangeLen {
limit = rangeLen
}
if req.FromLast && limit < rangeLen {
if req.FromEnd && limit < rangeLen {
skip = *before - limit
}
}
Expand Down
18 changes: 9 additions & 9 deletions gormrelay/keyset.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func createWhereExpr(s *schema.Schema, orderBys []relay.OrderBy, keyset map[stri
// clause.Limit{Limit: &limit},
//
// )
func scopeKeyset(after, before *map[string]any, orderBys []relay.OrderBy, limit int, fromLast bool) func(db *gorm.DB) *gorm.DB {
func scopeKeyset(after, before *map[string]any, orderBys []relay.OrderBy, limit int, fromEnd bool) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
if db.Statement.Model == nil {
db.AddError(errors.New("model is nil"))
Expand Down Expand Up @@ -131,7 +131,7 @@ func scopeKeyset(after, before *map[string]any, orderBys []relay.OrderBy, limit
}

desc := orderBy.Desc
if fromLast {
if fromEnd {
desc = !desc
}
orderByColumns = append(orderByColumns, clause.OrderByColumn{
Expand All @@ -150,7 +150,7 @@ func scopeKeyset(after, before *map[string]any, orderBys []relay.OrderBy, limit
}
}

func findByKeyset[T any](db *gorm.DB, after, before *map[string]any, orderBys []relay.OrderBy, limit int, fromLast bool) ([]T, error) {
func findByKeyset[T any](db *gorm.DB, after, before *map[string]any, orderBys []relay.OrderBy, limit int, fromEnd bool) ([]T, error) {
var nodes []T
if limit == 0 {
return nodes, nil
Expand All @@ -166,7 +166,7 @@ func findByKeyset[T any](db *gorm.DB, after, before *map[string]any, orderBys []
sliceType := reflect.SliceOf(modelType)
nodesVal := reflect.New(sliceType).Elem()

err := db.Scopes(scopeKeyset(after, before, orderBys, limit, fromLast)).Find(nodesVal.Addr().Interface()).Error
err := db.Scopes(scopeKeyset(after, before, orderBys, limit, fromEnd)).Find(nodesVal.Addr().Interface()).Error
if err != nil {
return nil, errors.Wrap(err, "find")
}
Expand All @@ -176,7 +176,7 @@ func findByKeyset[T any](db *gorm.DB, after, before *map[string]any, orderBys []
nodes[i] = nodesVal.Index(i).Interface().(T)
}

if fromLast {
if fromEnd {
lo.Reverse(nodes)
}
return nodes, nil
Expand All @@ -187,11 +187,11 @@ func findByKeyset[T any](db *gorm.DB, after, before *map[string]any, orderBys []
db = db.Model(t)
}

err = db.Scopes(scopeKeyset(after, before, orderBys, limit, fromLast)).Find(&nodes).Error
err = db.Scopes(scopeKeyset(after, before, orderBys, limit, fromEnd)).Find(&nodes).Error
if err != nil {
return nil, errors.Wrap(err, "find")
}
if fromLast {
if fromEnd {
lo.Reverse(nodes)
}
return nodes, nil
Expand All @@ -205,7 +205,7 @@ func NewKeysetFinder[T any](db *gorm.DB) *KeysetFinder[T] {
return &KeysetFinder[T]{db: db}
}

func (a *KeysetFinder[T]) Find(ctx context.Context, after, before *map[string]any, orderBys []relay.OrderBy, limit int, fromLast bool) ([]T, error) {
func (a *KeysetFinder[T]) Find(ctx context.Context, after, before *map[string]any, orderBys []relay.OrderBy, limit int, fromEnd bool) ([]T, error) {
if limit == 0 {
return []T{}, nil
}
Expand All @@ -215,7 +215,7 @@ func (a *KeysetFinder[T]) Find(ctx context.Context, after, before *map[string]an
db = db.WithContext(ctx)
}

nodes, err := findByKeyset[T](db, after, before, orderBys, limit, fromLast)
nodes, err := findByKeyset[T](db, after, before, orderBys, limit, fromEnd)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion gormrelay/offset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,6 @@ func TestOffsetWithLastAndNilBeforeIfSkipTotalCount(t *testing.T) {
Last: lo.ToPtr(10),
},
)
require.ErrorContains(t, err, "totalCount is required for fromLast and nil before")
require.ErrorContains(t, err, "totalCount is required for fromEnd and nil before")
require.Nil(t, resp)
}
4 changes: 2 additions & 2 deletions paginaition.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type ApplyCursorsRequest struct {
After *string
OrderBys []OrderBy
Limit int
FromLast bool
FromEnd bool
}

type LazyEdge[T any] struct {
Expand Down Expand Up @@ -106,7 +106,7 @@ func Paginate[T any](ctx context.Context, req *PaginateRequest[T], applyCursorsF
After: req.After,
OrderBys: orderBys,
Limit: limit,
FromLast: req.Last != nil,
FromEnd: req.Last != nil,
})
if err != nil {
return nil, err
Expand Down

0 comments on commit 8135871

Please sign in to comment.