Skip to content

Commit 42733b4

Browse files
authored
Merge pull request #193 from SkynetLabs/ivo/bson_m
[MINOR] Convert bson.D to bson.M where possible.
2 parents 2158073 + 1bf5db1 commit 42733b4

File tree

7 files changed

+40
-49
lines changed

7 files changed

+40
-49
lines changed

database/apikeys.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,7 @@ func (db *DB) APIKeyDelete(ctx context.Context, user User, akID primitive.Object
187187

188188
// APIKeyByKey returns a specific API key.
189189
func (db *DB) APIKeyByKey(ctx context.Context, key string) (APIKeyRecord, error) {
190-
filter := bson.M{"key": key}
191-
sr := db.staticAPIKeys.FindOne(ctx, filter)
190+
sr := db.staticAPIKeys.FindOne(ctx, bson.M{"key": key})
192191
if sr.Err() != nil {
193192
return APIKeyRecord{}, sr.Err()
194193
}
@@ -202,8 +201,7 @@ func (db *DB) APIKeyByKey(ctx context.Context, key string) (APIKeyRecord, error)
202201

203202
// APIKeyGet returns a specific API key.
204203
func (db *DB) APIKeyGet(ctx context.Context, akID primitive.ObjectID) (APIKeyRecord, error) {
205-
filter := bson.M{"_id": akID}
206-
sr := db.staticAPIKeys.FindOne(ctx, filter)
204+
sr := db.staticAPIKeys.FindOne(ctx, bson.M{"_id": akID})
207205
if sr.Err() != nil {
208206
return APIKeyRecord{}, sr.Err()
209207
}

database/download.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ type DownloadResponse struct {
4040
// DownloadByID fetches a single download from the DB.
4141
func (db *DB) DownloadByID(ctx context.Context, id primitive.ObjectID) (*Download, error) {
4242
var d Download
43-
filter := bson.D{{"_id", id}}
44-
sr := db.staticDownloads.FindOne(ctx, filter)
43+
sr := db.staticDownloads.FindOne(ctx, bson.M{"_id": id})
4544
err := sr.Decode(&d)
4645
if err != nil {
4746
return nil, err
@@ -129,13 +128,13 @@ func (db *DB) downloadsBy(ctx context.Context, matchStage bson.D, offset, pageSi
129128
// DownloadRecent returns the most recent download of the given skylink.
130129
func (db *DB) DownloadRecent(ctx context.Context, uID primitive.ObjectID, skylinkID primitive.ObjectID) (*Download, error) {
131130
updatedAtThreshold := time.Now().UTC().Add(-1 * DownloadUpdateWindow)
132-
filter := bson.D{
133-
{"user_id", uID},
134-
{"skylink_id", skylinkID},
135-
{"updated_at", bson.D{{"$gt", updatedAtThreshold}}},
131+
filter := bson.M{
132+
"user_id": uID,
133+
"skylink_id": skylinkID,
134+
"updated_at": bson.M{"$gt": updatedAtThreshold},
136135
}
137136
opts := options.FindOneOptions{
138-
Sort: bson.D{{"updated_at", -1}},
137+
Sort: bson.M{"updated_at": -1},
139138
}
140139
sr := db.staticDownloads.FindOne(ctx, filter, &opts)
141140
var d Download

database/email.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,7 @@ func (db *DB) PurgeEmailCollection(ctx context.Context) (int64, error) {
180180
if build.Release != "testing" {
181181
return 0, nil
182182
}
183-
filter := bson.M{}
184-
dr, err := db.staticEmails.DeleteMany(ctx, filter)
183+
dr, err := db.staticEmails.DeleteMany(ctx, bson.M{})
185184
if err != nil {
186185
return 0, err
187186
}

database/schema.go

+18-18
Original file line numberDiff line numberDiff line change
@@ -12,91 +12,91 @@ var (
1212
Schema = map[string][]mongo.IndexModel{
1313
collUsers: {
1414
{
15-
Keys: bson.D{{"sub", 1}},
15+
Keys: bson.M{"sub": 1},
1616
Options: options.Index().SetName("sub_unique").SetUnique(true),
1717
},
1818
},
1919
collSkylinks: {
2020
{
21-
Keys: bson.D{{"skylink", 1}},
21+
Keys: bson.M{"skylink": 1},
2222
Options: options.Index().SetName("skylink_unique").SetUnique(true),
2323
},
2424
},
2525
collUploads: {
2626
{
27-
Keys: bson.D{{"user_id", 1}},
27+
Keys: bson.M{"user_id": 1},
2828
Options: options.Index().SetName("user_id"),
2929
},
3030
{
31-
Keys: bson.D{{"skylink_id", 1}},
31+
Keys: bson.M{"skylink_id": 1},
3232
Options: options.Index().SetName("skylink_id"),
3333
},
3434
},
3535
collDownloads: {
3636
{
37-
Keys: bson.D{{"user_id", 1}},
37+
Keys: bson.M{"user_id": 1},
3838
Options: options.Index().SetName("user_id"),
3939
},
4040
{
41-
Keys: bson.D{{"skylink_id", 1}},
41+
Keys: bson.M{"skylink_id": 1},
4242
Options: options.Index().SetName("skylink_id"),
4343
},
4444
},
4545
collEmails: {
4646
{
47-
Keys: bson.D{{"failed_attempts", 1}},
47+
Keys: bson.M{"failed_attempts": 1},
4848
Options: options.Index().SetName("failed_attempts"),
4949
},
5050
{
51-
Keys: bson.D{{"locked_by", 1}},
51+
Keys: bson.M{"locked_by": 1},
5252
Options: options.Index().SetName("locked_by"),
5353
},
5454
{
55-
Keys: bson.D{{"sent_at", 1}},
55+
Keys: bson.M{"sent_at": 1},
5656
Options: options.Index().SetName("sent_at"),
5757
},
5858
{
59-
Keys: bson.D{{"sent_by", 1}},
59+
Keys: bson.M{"sent_by": 1},
6060
Options: options.Index().SetName("sent_by"),
6161
},
6262
},
6363
collChallenges: {
6464
{
65-
Keys: bson.D{{"challenge", 1}},
65+
Keys: bson.M{"challenge": 1},
6666
Options: options.Index().SetName("challenge"),
6767
},
6868
{
69-
Keys: bson.D{{"type", 1}},
69+
Keys: bson.M{"type": 1},
7070
Options: options.Index().SetName("type"),
7171
},
7272
{
73-
Keys: bson.D{{"expires_at", 1}},
73+
Keys: bson.M{"expires_at": 1},
7474
Options: options.Index().SetName("expires_at"),
7575
},
7676
},
7777
collUnconfirmedUserUpdates: {
7878
{
79-
Keys: bson.D{{"challenge_id", 1}},
79+
Keys: bson.M{"challenge_id": 1},
8080
Options: options.Index().SetName("challenge_id"),
8181
},
8282
{
83-
Keys: bson.D{{"expires_at", 1}},
83+
Keys: bson.M{"expires_at": 1},
8484
Options: options.Index().SetName("expires_at"),
8585
},
8686
},
8787
collConfiguration: {
8888
{
89-
Keys: bson.D{{"key", 1}},
89+
Keys: bson.M{"key": 1},
9090
Options: options.Index().SetName("key_unique").SetUnique(true),
9191
},
9292
},
9393
collAPIKeys: {
9494
{
95-
Keys: bson.D{{"key", 1}},
95+
Keys: bson.M{"key": 1},
9696
Options: options.Index().SetName("key_unique").SetUnique(true),
9797
},
9898
{
99-
Keys: bson.D{{"user_id", 1}},
99+
Keys: bson.M{"user_id": 1},
100100
Options: options.Index().SetName("user_id"),
101101
},
102102
},

database/skylink.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (db *DB) Skylink(ctx context.Context, skylink string) (*Skylink, error) {
4949
Skylink: skylinkHash,
5050
}
5151
// Try to find the skylink in the database.
52-
filter := bson.D{{"skylink", skylinkHash}}
52+
filter := bson.M{"skylink": skylinkHash}
5353
upsert := bson.M{"$setOnInsert": bson.M{"skylink": skylinkHash}}
5454
opts := options.FindOneAndUpdate().SetUpsert(true).SetReturnDocument(options.After)
5555
sr := db.staticSkylinks.FindOneAndUpdate(ctx, filter, upsert, opts)
@@ -62,8 +62,7 @@ func (db *DB) Skylink(ctx context.Context, skylink string) (*Skylink, error) {
6262

6363
// SkylinkByID finds a skylink by its ID.
6464
func (db *DB) SkylinkByID(ctx context.Context, id primitive.ObjectID) (*Skylink, error) {
65-
filter := bson.D{{"_id", id}}
66-
sr := db.staticSkylinks.FindOne(ctx, filter)
65+
sr := db.staticSkylinks.FindOne(ctx, bson.M{"_id": id})
6766
var sl Skylink
6867
err := sr.Decode(&sl)
6968
if err != nil {

database/upload.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ type UploadResponse struct {
3434
// UploadByID fetches a single upload from the DB.
3535
func (db *DB) UploadByID(ctx context.Context, id primitive.ObjectID) (*Upload, error) {
3636
var d Upload
37-
filter := bson.D{{"_id", id}}
38-
sr := db.staticUploads.FindOne(ctx, filter)
37+
sr := db.staticUploads.FindOne(ctx, bson.M{"_id": id})
3938
err := sr.Decode(&d)
4039
if err != nil {
4140
return nil, err
@@ -105,10 +104,10 @@ func (db *DB) UnpinUploads(ctx context.Context, skylink Skylink, user User) (int
105104
if user.ID.IsZero() {
106105
return 0, errors.New("invalid user")
107106
}
108-
filter := bson.D{
109-
{"skylink_id", skylink.ID},
110-
{"user_id", user.ID},
111-
{"unpinned", false},
107+
filter := bson.M{
108+
"skylink_id": skylink.ID,
109+
"user_id": user.ID,
110+
"unpinned": false,
112111
}
113112
update := bson.M{"$set": bson.M{"unpinned": true}}
114113
ur, err := db.staticUploads.UpdateMany(ctx, filter, update)

database/user.go

+6-9
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,7 @@ func (db *DB) UserByEmail(ctx context.Context, email string) (*User, error) {
167167

168168
// UserByID finds a user by their ID.
169169
func (db *DB) UserByID(ctx context.Context, id primitive.ObjectID) (*User, error) {
170-
filter := bson.D{{"_id", id}}
171-
c, err := db.staticUsers.Find(ctx, filter)
170+
c, err := db.staticUsers.Find(ctx, bson.M{"_id": id})
172171
if err != nil {
173172
return nil, errors.AddContext(err, "failed to Find")
174173
}
@@ -215,8 +214,7 @@ func (db *DB) UserByRecoveryToken(ctx context.Context, token string) (*User, err
215214

216215
// UserByStripeID finds a user by their Stripe customer id.
217216
func (db *DB) UserByStripeID(ctx context.Context, id string) (*User, error) {
218-
filter := bson.D{{"stripe_id", id}}
219-
c, err := db.staticUsers.Find(ctx, filter)
217+
c, err := db.staticUsers.Find(ctx, bson.M{"stripe_id": id})
220218
if err != nil {
221219
return nil, errors.AddContext(err, "failed to Find")
222220
}
@@ -465,7 +463,7 @@ func (db *DB) UserDelete(ctx context.Context, u *User) error {
465463
return errors.AddContext(ErrUserNotFound, "user struct not fully initialised")
466464
}
467465
// Delete all data associated with this user.
468-
filter := bson.D{{"user_id", u.ID}}
466+
filter := bson.M{"user_id": u.ID}
469467
_, err := db.staticDownloads.DeleteMany(ctx, filter)
470468
if err != nil {
471469
return errors.AddContext(err, "failed to delete user downloads")
@@ -486,12 +484,12 @@ func (db *DB) UserDelete(ctx context.Context, u *User) error {
486484
if err != nil {
487485
return errors.AddContext(err, "failed to delete user API keys")
488486
}
489-
_, err = db.staticUnconfirmedUserUpdates.DeleteMany(ctx, bson.D{{"sub", u.Sub}})
487+
_, err = db.staticUnconfirmedUserUpdates.DeleteMany(ctx, bson.M{"sub": u.Sub})
490488
if err != nil {
491489
return errors.AddContext(err, "failed to delete user unconfirmed updates")
492490
}
493491
// Delete the actual user.
494-
filter = bson.D{{"_id", u.ID}}
492+
filter = bson.M{"_id": u.ID}
495493
dr, err := db.staticUsers.DeleteOne(ctx, filter)
496494
if err != nil {
497495
return errors.AddContext(err, "failed to Delete")
@@ -597,8 +595,7 @@ func (db *DB) Ping(ctx context.Context) error {
597595
// managedUsersByField finds all users that have a given field value.
598596
// The calling method is responsible for the validation of the value.
599597
func (db *DB) managedUsersByField(ctx context.Context, fieldName, fieldValue string) ([]*User, error) {
600-
filter := bson.M{fieldName: fieldValue}
601-
c, err := db.staticUsers.Find(ctx, filter)
598+
c, err := db.staticUsers.Find(ctx, bson.M{fieldName: fieldValue})
602599
if err != nil {
603600
return nil, errors.AddContext(err, "failed to find user")
604601
}

0 commit comments

Comments
 (0)