Skip to content

Commit 121021d

Browse files
committed
address pr comments
1 parent ea3784a commit 121021d

File tree

5 files changed

+51
-38
lines changed

5 files changed

+51
-38
lines changed

src/MongoDB.Driver/SortDefinition.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ public abstract class SortDefinition<TDocument>
4747
/// <returns>A <see cref="BsonDocument"/>.</returns>
4848
public abstract BsonDocument Render(RenderArgs<TDocument> args);
4949

50+
// TODO: remove this and refactor Render to return a BsonValue in 4.0
51+
internal virtual BsonValue RenderAsBsonValue(RenderArgs<TDocument> args) => Render(args);
52+
5053
/// <summary>
5154
/// Performs an implicit conversion from <see cref="BsonDocument"/> to <see cref="SortDefinition{TDocument}"/>.
5255
/// </summary>

src/MongoDB.Driver/SortDefinitionBuilder.cs

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,10 @@ public sealed class SortDefinitionBuilder<TDocument>
133133
/// <summary>
134134
/// Creates a value ascending sort.
135135
/// </summary>
136-
/// <returns>An ascending sort.</returns>
136+
/// <returns>A value ascending sort.</returns>
137137
public SortDefinition<TDocument> Ascending()
138138
{
139-
return new NoFieldDirectionalSortDefinition<TDocument>(SortDirection.Ascending);
139+
return new ValueDirectionalSortDefinition<TDocument>(SortDirection.Ascending);
140140
}
141141

142142
/// <summary>
@@ -182,10 +182,10 @@ public SortDefinition<TDocument> Combine(IEnumerable<SortDefinition<TDocument>>
182182
/// <summary>
183183
/// Creates a value descending sort.
184184
/// </summary>
185-
/// <returns>A descending sort.</returns>
185+
/// <returns>A value descending sort.</returns>
186186
public SortDefinition<TDocument> Descending()
187187
{
188-
return new NoFieldDirectionalSortDefinition<TDocument>(SortDirection.Descending);
188+
return new ValueDirectionalSortDefinition<TDocument>(SortDirection.Descending);
189189
}
190190

191191
/// <summary>
@@ -251,7 +251,7 @@ public CombinedSortDefinition(IEnumerable<SortDefinition<TDocument>> sorts)
251251
{
252252
_sorts = Ensure.IsNotNull(sorts, nameof(sorts)).ToList();
253253

254-
if (_sorts.Any(sort => sort is NoFieldDirectionalSortDefinition<TDocument>))
254+
if (_sorts.Any(sort => sort is ValueDirectionalSortDefinition<TDocument>))
255255
{
256256
throw new InvalidOperationException("Value-based sort cannot be combined with other sorts. When sorting by the entire element value, no other sorting criteria can be applied.");
257257
}
@@ -295,42 +295,25 @@ public override BsonDocument Render(RenderArgs<TDocument> args)
295295
{
296296
var renderedField = _field.Render(args);
297297

298-
BsonValue value;
299-
switch (_direction)
300-
{
301-
case SortDirection.Ascending:
302-
value = 1;
303-
break;
304-
case SortDirection.Descending:
305-
value = -1;
306-
break;
307-
default:
308-
throw new InvalidOperationException("Unknown value for " + typeof(SortDirection) + ".");
309-
}
310-
311-
return new BsonDocument(renderedField.FieldName, value);
298+
return new BsonDocument(renderedField.FieldName, _direction.ToBsonValue());
312299
}
313300
}
314301

315-
internal sealed class NoFieldDirectionalSortDefinition<TDocument> : SortDefinition<TDocument>
302+
internal sealed class ValueDirectionalSortDefinition<TDocument> : SortDefinition<TDocument>
316303
{
317304
private readonly SortDirection _direction;
318305

319-
public NoFieldDirectionalSortDefinition(SortDirection direction)
306+
public ValueDirectionalSortDefinition(SortDirection direction)
320307
{
321308
_direction = direction;
322309
}
323310

311+
internal override BsonValue RenderAsBsonValue(RenderArgs<TDocument> args) => _direction.ToBsonValue();
312+
324313
public override BsonDocument Render(RenderArgs<TDocument> args)
325314
{
326-
BsonValue value = _direction switch
327-
{
328-
SortDirection.Ascending => 1,
329-
SortDirection.Descending => -1,
330-
_ => throw new InvalidOperationException("Unknown value for " + typeof(SortDirection) + ".")
331-
};
332-
333-
return new BsonDocument("direction", value);
315+
throw new InvalidOperationException(
316+
"Value-based sort cannot be rendered as a document. You might be trying to use a value-based sort where a field-based sort is expected.");
334317
}
335318
}
336319
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* Copyright 2010-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
using MongoDB.Bson;
18+
19+
namespace MongoDB.Driver
20+
{
21+
internal static class SortDirectionExtensions
22+
{
23+
internal static BsonValue ToBsonValue(this SortDirection direction)
24+
{
25+
return direction switch
26+
{
27+
SortDirection.Ascending => 1,
28+
SortDirection.Descending => -1,
29+
_ => throw new InvalidOperationException($"Invalid sort direction: {direction}.")
30+
};
31+
}
32+
}
33+
}

src/MongoDB.Driver/UpdateDefinitionBuilder.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,13 +1685,7 @@ public override BsonValue Render(RenderArgs<TDocument> args)
16851685

16861686
if (_sort != null)
16871687
{
1688-
var newArgs = args.WithNewDocumentType((IBsonSerializer<TItem>)itemSerializer);
1689-
1690-
var renderedSort = _sort is NoFieldDirectionalSortDefinition<TItem>
1691-
? _sort.Render(newArgs)["direction"]
1692-
: _sort.Render(newArgs);
1693-
1694-
document["$push"][renderedField.FieldName]["$sort"] = renderedSort;
1688+
document["$push"][renderedField.FieldName]["$sort"] = _sort.RenderAsBsonValue(args.WithNewDocumentType((IBsonSerializer<TItem>)itemSerializer));
16951689
}
16961690

16971691
return document;

tests/MongoDB.Driver.Tests/SortDefinitionBuilderTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void Ascending()
3333
}
3434

3535
[Fact]
36-
public void Ascending_no_field()
36+
public void Ascending_value()
3737
{
3838
var subject = CreateSubject<BsonDocument>();
3939

@@ -104,7 +104,7 @@ public void Descending()
104104
}
105105

106106
[Fact]
107-
public void Descending_no_field()
107+
public void Descending_value()
108108
{
109109
var subject = CreateSubject<BsonDocument>();
110110

0 commit comments

Comments
 (0)