Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(server): sort duplicate key error #55

Merged
merged 5 commits into from
Nov 20, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions mongox/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/reearth/reearthx/rerror"
"github.com/reearth/reearthx/usecasex"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
Expand Down Expand Up @@ -40,8 +41,15 @@ func (c *Collection) Paginate(ctx context.Context, rawFilter any, s *usecasex.So
sortOrder *= -1
}

var sort primitive.D
if sortKey == idKey {
sort = bson.D{{Key: sortKey, Value: sortOrder}}
} else {
sort = bson.D{{Key: sortKey, Value: sortOrder}, {Key: idKey, Value: sortOrder}}
}

findOpts := options.Find().
SetSort(bson.D{{Key: sortKey, Value: sortOrder}, {Key: idKey, Value: sortOrder}}).
SetSort(sort).
SetLimit(limit(*p))

if p.Offset != nil {
Expand Down Expand Up @@ -199,11 +207,11 @@ func (c *Collection) aggregateFilter(ctx context.Context, p usecasex.Pagination,
}

func aggregateOptionsFromPagination(_ usecasex.Pagination, _ *usecasex.Sort) *options.AggregateOptions {
collation := options.Collation{
Locale: "en",
Strength: 2,
}
return options.Aggregate().SetAllowDiskUse(true).SetCollation(&collation)
collation := options.Collation{
Locale: "en",
Strength: 2,
}
return options.Aggregate().SetAllowDiskUse(true).SetCollation(&collation)
}

func (c *Collection) pageFilter(ctx context.Context, p usecasex.Pagination, s *usecasex.Sort) (bson.M, error) {
Expand Down Expand Up @@ -280,11 +288,11 @@ func (c *Collection) getCursorDocument(ctx context.Context, cursor usecasex.Curs
}

func sortFilter(p usecasex.Pagination, s *usecasex.Sort) bson.D {
var sortOptions bson.D
if s != nil && s.Key != "" && s.Key != idKey {
sortOptions = append(sortOptions, bson.E{Key: s.Key, Value: sortDirection(p, s)})
}
return append(sortOptions, bson.E{Key: idKey, Value: sortDirection(p, s)})
var sortOptions bson.D
if s != nil && s.Key != "" && s.Key != idKey {
sortOptions = append(sortOptions, bson.E{Key: s.Key, Value: sortDirection(p, s)})
}
return append(sortOptions, bson.E{Key: idKey, Value: sortDirection(p, s)})
Comment on lines +291 to +295
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix potential nil pointer dereference

The condition s != nil && s.Key != "" is correct, but the subsequent code assumes s is not nil when calling sortDirection(p, s). While sortDirection handles nil s, it would be more explicit to handle this in the sort options construction.

 func sortFilter(p usecasex.Pagination, s *usecasex.Sort) bson.D {
 	var sortOptions bson.D
 	if s != nil && s.Key != "" && s.Key != idKey {
-		sortOptions = append(sortOptions, bson.E{Key: s.Key, Value: sortDirection(p, s)})
+		direction := sortDirection(p, s)
+		sortOptions = append(sortOptions, bson.E{Key: s.Key, Value: direction})
+		return append(sortOptions, bson.E{Key: idKey, Value: direction})
 	}
-	return append(sortOptions, bson.E{Key: idKey, Value: sortDirection(p, s)})
+	return bson.D{{Key: idKey, Value: sortDirection(p, nil)}}
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
var sortOptions bson.D
if s != nil && s.Key != "" && s.Key != idKey {
sortOptions = append(sortOptions, bson.E{Key: s.Key, Value: sortDirection(p, s)})
}
return append(sortOptions, bson.E{Key: idKey, Value: sortDirection(p, s)})
func sortFilter(p usecasex.Pagination, s *usecasex.Sort) bson.D {
var sortOptions bson.D
if s != nil && s.Key != "" && s.Key != idKey {
direction := sortDirection(p, s)
sortOptions = append(sortOptions, bson.E{Key: s.Key, Value: direction})
return append(sortOptions, bson.E{Key: idKey, Value: direction})
}
return bson.D{{Key: idKey, Value: sortDirection(p, nil)}}
}

}

func limit(p usecasex.Pagination) int64 {
Expand Down
Loading