Skip to content

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

adelinowona
Copy link
Contributor

No description provided.

Copy link
Contributor

@rstam rstam left a 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.

_direction = direction;
}

public override BsonDocument Render(RenderArgs<TDocument> args)
Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We currently have no use for that property, so I decided to omit it for now to avoid introducing unused code.

{
SortDirection.Ascending => 1,
SortDirection.Descending => -1,
_ => throw new InvalidOperationException("Unknown value for " + typeof(SortDirection) + ".")
Copy link
Contributor

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);
Copy link
Contributor

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So what happens in other cases (which are not UpdateDefinitionBuilder), will it be rendered as
sort: { direction: ... } and do we want that?

We could add
internal virtual BsonValue RenderAsBsonValue(RenderArgs<TDocument> args) => Render(args);
to SortDefinition, and refactor Render to return BsonValue in 4.0.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I like this! I thought of something similar but thought adding something to SortDefinition would be a breaking change but I suppose adding an internal virtual method is not? Learning some library maintainer tricks :)

var newArgs = args.WithNewDocumentType((IBsonSerializer<TItem>)itemSerializer);

var renderedSort = _sort is NoFieldDirectionalSortDefinition<TItem>
? _sort.Render(newArgs)["direction"]
Copy link
Contributor

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.


public override BsonDocument Render(RenderArgs<TDocument> args)
{
BsonValue value = _direction switch
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do the SortDirection to BsonValue conversion in 2 different places already.
Consider extorting this to internal SortDirectionExtensions or similar?

_ => throw new InvalidOperationException("Unknown value for " + typeof(SortDirection) + ".")
};

return new BsonDocument("direction", value);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So what happens in other cases (which are not UpdateDefinitionBuilder), will it be rendered as
sort: { direction: ... } and do we want that?

We could add
internal virtual BsonValue RenderAsBsonValue(RenderArgs<TDocument> args) => Render(args);
to SortDefinition, and refactor Render to return BsonValue in 4.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants