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.

@@ -288,4 +311,26 @@ public override BsonDocument Render(RenderArgs<TDocument> args)
return new BsonDocument(renderedField.FieldName, value);
}
}

internal sealed class NoFieldDirectionalSortDefinition<TDocument> : SortDefinition<TDocument>
Copy link
Contributor

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)
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?

{
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.

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.

@@ -31,6 +32,14 @@ public void Ascending()
Assert(subject.Ascending("a"), "{a: 1}");
}

[Fact]
public void Ascending_no_field()
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 name this test Ascending_value

@@ -84,6 +103,14 @@ public void Descending()
Assert(subject.Descending("a"), "{a: -1}");
}

[Fact]
public void Descending_no_field()
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 name this test Descending_value.

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.

2 participants