Skip to content

Commit 647f145

Browse files
author
lifeng.lif
committed
feat: update lint
1 parent 22bb159 commit 647f145

File tree

5 files changed

+34
-64
lines changed

5 files changed

+34
-64
lines changed

bsonx/bsonx.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ package bsonx
1717
import (
1818
"bytes"
1919

20-
"github.com/chenmingyong0423/go-mongox/v2/internal/pkg/utils"
21-
2220
"go.mongodb.org/mongo-driver/v2/bson"
2321
)
2422

@@ -98,11 +96,15 @@ func dToM(d bson.D) bson.M {
9896
func StringSortToBsonD(sorts ...string) bson.D {
9997
var res bson.D
10098
for _, sort := range sorts {
101-
key, n := utils.SplitSortField(sort)
102-
if key == "" {
99+
if len(sort) == 0 || sort == "-" {
103100
continue
104101
}
105-
res = append(res, bson.E{Key: key, Value: n})
102+
103+
if sort[0] == '-' {
104+
res = append(res, bson.E{Key: sort[1:], Value: -1})
105+
} else {
106+
res = append(res, bson.E{Key: sort, Value: 1})
107+
}
106108
}
107109

108110
return res

bsonx/bsonx_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,25 @@ func TestStringSortToBsonD(t *testing.T) {
6969
value any
7070
want bson.D
7171
}{
72+
{
73+
name: "empty string",
74+
value: []string{""},
75+
want: nil,
76+
},
77+
{
78+
name: "only minus sign",
79+
value: []string{"-"},
80+
want: nil,
81+
},
7282
{
7383
name: "one sort",
7484
value: []string{"-created_at"},
75-
want: bson.D{{"created_at", -1}},
85+
want: bson.D{{Key: "created_at", Value: -1}},
7686
},
7787
{
7888
name: "two sort",
7989
value: []string{"age", "-created_at"},
80-
want: bson.D{{"age", 1}, {"created_at", -1}},
90+
want: bson.D{{Key: "age", Value: 1}, {Key: "created_at", Value: -1}},
8191
},
8292
}
8393

finder/finder.go

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package finder
1616

1717
import (
1818
"context"
19+
1920
"github.com/chenmingyong0423/go-mongox/v2/callback"
2021
"github.com/chenmingyong0423/go-mongox/v2/operation"
2122

@@ -126,13 +127,7 @@ func (f *Finder[T]) postActionHandler(ctx context.Context, globalOpContext *oper
126127
}
127128

128129
func (f *Finder[T]) FindOne(ctx context.Context, opts ...options.Lister[options.FindOneOptions]) (*T, error) {
129-
if f.skip > 0 {
130-
opts = append(opts, options.FindOne().SetSkip(f.skip))
131-
}
132-
if f.sort != nil {
133-
opts = append(opts, options.FindOne().SetSort(f.sort))
134-
}
135-
130+
opts = append(opts, options.FindOne().SetSkip(f.skip).SetSort(f.sort))
136131
t := new(T)
137132

138133
globalOpContext := operation.NewOpContext(f.collection, operation.WithDoc(t), operation.WithFilter(f.filter), operation.WithMongoOptions(opts), operation.WithModelHook(f.modelHook))
@@ -155,15 +150,7 @@ func (f *Finder[T]) FindOne(ctx context.Context, opts ...options.Lister[options.
155150
}
156151

157152
func (f *Finder[T]) Find(ctx context.Context, opts ...options.Lister[options.FindOptions]) ([]*T, error) {
158-
if f.skip > 0 {
159-
opts = append(opts, options.Find().SetSkip(f.skip))
160-
}
161-
if f.limit > 0 {
162-
opts = append(opts, options.Find().SetLimit(f.limit))
163-
}
164-
if f.sort != nil {
165-
opts = append(opts, options.Find().SetSort(f.sort))
166-
}
153+
opts = append(opts, options.Find().SetSkip(f.skip).SetLimit(f.limit).SetSort(f.sort))
167154

168155
t := make([]*T, 0)
169156

finder/finder_e2e_test.go

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func TestFinder_e2e_FindOne(t *testing.T) {
127127
},
128128

129129
{
130-
name: "find by name and sort",
130+
name: "find by name with sort",
131131
before: func(ctx context.Context, t *testing.T) {
132132
insertOneResult, err := collection.InsertMany(ctx, []*TestUser{
133133
&TestUser{
@@ -155,7 +155,7 @@ func TestFinder_e2e_FindOne(t *testing.T) {
155155
},
156156
},
157157
{
158-
name: "find by name and sort",
158+
name: "find by name with sort and skip",
159159
before: func(ctx context.Context, t *testing.T) {
160160
insertOneResult, err := collection.InsertMany(ctx, []*TestUser{
161161
&TestUser{
@@ -409,13 +409,11 @@ func TestFinder_e2e_FindOne(t *testing.T) {
409409
}
410410

411411
finder = finder.RegisterBeforeHooks(tc.beforeHook...).
412-
RegisterAfterHooks(tc.afterHook...).Filter(tc.filter)
413-
if tc.skip > 0 {
414-
finder = finder.Skip(tc.skip)
415-
}
416-
if tc.sort != nil {
417-
finder = finder.Sort(tc.sort)
418-
}
412+
RegisterAfterHooks(tc.afterHook...).
413+
Filter(tc.filter).
414+
Skip(tc.skip).
415+
Limit(tc.limit).
416+
Sort(tc.sort)
419417

420418
user, err := finder.
421419
FindOne(tc.ctx, tc.opts...)
@@ -902,16 +900,11 @@ func TestFinder_e2e_Find(t *testing.T) {
902900
}
903901

904902
finder = finder.RegisterBeforeHooks(tc.beforeHook...).
905-
RegisterAfterHooks(tc.afterHook...).Filter(tc.filter)
906-
if tc.skip > 0 {
907-
finder = finder.Skip(tc.skip)
908-
}
909-
if tc.sort != nil {
910-
finder = finder.Sort(tc.sort)
911-
}
912-
if tc.limit > 0 {
913-
finder = finder.Limit(tc.limit)
914-
}
903+
RegisterAfterHooks(tc.afterHook...).
904+
Filter(tc.filter).
905+
Skip(tc.skip).
906+
Limit(tc.limit).
907+
Sort(tc.sort)
915908

916909
users, err := finder.Find(tc.ctx, tc.opts...)
917910
tc.after(tc.ctx, t)

internal/pkg/utils/utils.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package utils
1717
import (
1818
"fmt"
1919
"reflect"
20-
"strings"
2120

2221
"go.mongodb.org/mongo-driver/v2/mongo"
2322

@@ -104,24 +103,3 @@ func IsNumeric(value any) bool {
104103
return false
105104
}
106105
}
107-
108-
// SplitSortField handle sort symbol: "+"/"-" in front of field
109-
// if "+", return sort as 1
110-
// if "-", return sort as -1
111-
func SplitSortField(field string) (key string, sort int32) {
112-
sort = 1
113-
key = field
114-
115-
if len(field) != 0 {
116-
switch field[0] {
117-
case '+':
118-
key = strings.TrimPrefix(field, "+")
119-
sort = 1
120-
case '-':
121-
key = strings.TrimPrefix(field, "-")
122-
sort = -1
123-
}
124-
}
125-
126-
return key, sort
127-
}

0 commit comments

Comments
 (0)