Skip to content

Commit dd5b4ad

Browse files
committed
simplify
1 parent 4a29f5a commit dd5b4ad

File tree

1 file changed

+12
-24
lines changed

1 file changed

+12
-24
lines changed

internal/util/sharding.go

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/pkg/errors"
88
"go.mongodb.org/mongo-driver/bson"
99
"go.mongodb.org/mongo-driver/mongo"
10-
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
1110
)
1211

1312
const (
@@ -27,7 +26,14 @@ func GetShardKey(
2726
Database(configDBName).
2827
Collection(collsCollName)
2928

30-
rawResult, err := configCollectionsColl.FindOne(ctx, bson.D{{"_id", namespace}}).Raw()
29+
decoded := struct {
30+
Key option.Option[bson.Raw]
31+
}{}
32+
33+
err := configCollectionsColl.
34+
FindOne(ctx, bson.D{{"_id", namespace}}).
35+
Decode(&decoded)
36+
3137
if errors.Is(err, mongo.ErrNoDocuments) {
3238
return option.None[bson.Raw](), nil
3339
} else if err != nil {
@@ -38,29 +44,11 @@ func GetShardKey(
3844
)
3945
}
4046

41-
keyAsVal, err := rawResult.LookupErr("key")
42-
if errors.Is(err, bsoncore.ErrElementNotFound) {
43-
return option.None[bson.Raw](), nil
44-
} else if err != nil {
45-
return option.None[bson.Raw](), errors.Wrapf(
46-
err,
47-
"failed to find %#q in %#q's %#q entry",
48-
"key",
49-
namespace,
50-
FullName(configCollectionsColl),
51-
)
52-
}
47+
key, hasKey := decoded.Key.Get()
5348

54-
keyAsRaw, isDoc := keyAsVal.DocumentOK()
55-
if !isDoc {
56-
return option.None[bson.Raw](), errors.Errorf(
57-
"%#q in %#q's %#q entry is of type %#q, not an object",
58-
"key",
59-
namespace,
60-
FullName(configCollectionsColl),
61-
keyAsVal.Type,
62-
)
49+
if !hasKey {
50+
return option.None[bson.Raw](), nil
6351
}
6452

65-
return option.Some(keyAsRaw), nil
53+
return option.Some(key), nil
6654
}

0 commit comments

Comments
 (0)