Skip to content

Commit

Permalink
fix(server): sort duplicate key error (#55)
Browse files Browse the repository at this point in the history
* fix sort duplicate key error

* add test case

* fix test error

* fix lint

* add Mutex lock

---------

Co-authored-by: tomokazu tantaka <[email protected]>
  • Loading branch information
hexaforce and tomokazu tantaka authored Nov 20, 2024
1 parent cd5d2cb commit e4cc7e5
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 113 deletions.
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
help:
@echo "Usage:"
@echo " make <target>"
@echo ""
@echo "Targets:"
@echo " lint Run golangci-lint with auto-fix"
@echo " test Run unit tests with race detector in short mode"

lint:
golangci-lint run --fix

test:
go test -race -short -v ./...

.PHONY: lint test
5 changes: 5 additions & 0 deletions account/accountdomain/workspace/member.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package workspace

import (
"sort"
"sync"

"github.com/reearth/reearthx/account/accountdomain/user"
"github.com/reearth/reearthx/i18n"
Expand Down Expand Up @@ -29,6 +30,7 @@ type Members struct {
users map[UserID]Member
integrations map[IntegrationID]Member
fixed bool
mu sync.Mutex
}

func NewMembers() *Members {
Expand Down Expand Up @@ -221,6 +223,9 @@ func (m *Members) AddIntegration(iid IntegrationID, role Role, i UserID) error {
}

func (m *Members) Leave(u UserID) error {
m.mu.Lock()
defer m.mu.Unlock()

if m.fixed {
return ErrCannotModifyPersonalWorkspace
}
Expand Down
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)})
}

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

0 comments on commit e4cc7e5

Please sign in to comment.