-
Notifications
You must be signed in to change notification settings - Fork 1.3k
CSHARP-5672: Support sorting by value in PushEach operation #1748
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. Minor changes requested.
@@ -288,4 +311,26 @@ public override BsonDocument Render(RenderArgs<TDocument> args) | |||
return new BsonDocument(renderedField.FieldName, value); | |||
} | |||
} | |||
|
|||
internal sealed class NoFieldDirectionalSortDefinition<TDocument> : SortDefinition<TDocument> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest naming this class ValueDirectionalSortDefinition
.
I'd rather emphasize that we are sorting by "value". The fact that there is no field name is a side effect, not the main feature.
_direction = direction; | ||
} | ||
|
||
public override BsonDocument Render(RenderArgs<TDocument> args) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want a Direction
property like DirectionalSortDefinition
has?
{ | ||
SortDirection.Ascending => 1, | ||
SortDirection.Descending => -1, | ||
_ => throw new InvalidOperationException("Unknown value for " + typeof(SortDirection) + ".") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would have done this:
_ => throw new InvalidOperationException($"Invalid sort direction: {_direction}.")
but I suppose then we might want to change DirectionalSortDefinition
also...
_ => throw new InvalidOperationException("Unknown value for " + typeof(SortDirection) + ".") | ||
}; | ||
|
||
return new BsonDocument("direction", value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When we use "magic" values like this we usually enclose them in angle brackets to make it visually apparent that something is special about this value:
return new BsonDocument("<direction>", value);
We only have a field name here because Render
is already declared to return BsonDocument
and changing it to return BsonValue
would be a breaking change.
var newArgs = args.WithNewDocumentType((IBsonSerializer<TItem>)itemSerializer); | ||
|
||
var renderedSort = _sort is NoFieldDirectionalSortDefinition<TItem> | ||
? _sort.Render(newArgs)["direction"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
? _sort.Render(newArgs)["<direction>"]
Note the added angle brackets.
@@ -31,6 +32,14 @@ public void Ascending() | |||
Assert(subject.Ascending("a"), "{a: 1}"); | |||
} | |||
|
|||
[Fact] | |||
public void Ascending_no_field() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would name this test Ascending_value
@@ -84,6 +103,14 @@ public void Descending() | |||
Assert(subject.Descending("a"), "{a: -1}"); | |||
} | |||
|
|||
[Fact] | |||
public void Descending_no_field() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would name this test Descending_value
.
No description provided.