-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Simplify usage of user provided aggregation operations. #4059
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2022 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.mongodb.core.aggregation; | ||
|
||
import java.util.Map; | ||
|
||
import org.bson.Document; | ||
import org.bson.conversions.Bson; | ||
import org.springframework.data.mongodb.util.BsonUtils; | ||
import org.springframework.util.ObjectUtils; | ||
|
||
/** | ||
* {@link AggregationOperation} implementation that c | ||
* | ||
* @author Christoph Strobl | ||
* @since 4.0 | ||
*/ | ||
class BasicAggregationOperation implements AggregationOperation { | ||
|
||
private final Object value; | ||
|
||
BasicAggregationOperation(Object value) { | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public Document toDocument(AggregationOperationContext context) { | ||
|
||
if (value instanceof Document document) { | ||
return document; | ||
} | ||
|
||
if (value instanceof Bson bson) { | ||
return BsonUtils.asDocument(bson, context.getCodecRegistry()); | ||
} | ||
|
||
if (value instanceof Map map) { | ||
return new Document(map); | ||
} | ||
Comment on lines
+42
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
if (value instanceof Map map) {
return new Document(map);
} is a type-safe code. Am I missing something? Given the above, and that if (value instanceof Bson bson) {
return BsonUtils.asDocument(bson, context.getCodecRegistry());
} ? |
||
|
||
if (value instanceof String json && BsonUtils.isJsonDocument(json)) { | ||
return BsonUtils.parse(json, context); | ||
} | ||
|
||
throw new IllegalStateException( | ||
String.format("%s cannot be converted to org.bson.Document.", ObjectUtils.nullSafeClassName(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.
In practice, when (if ever) will this default implementation be used? The underlying driver never uses this method, because it doesn't take into account important user configuration (most notably, the UUID representation).
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.
Since it’s public API that users may have implemented, defaulting here eases the pain when upgrading to the new version. Internal implementations pick up the driver registry.
Are custom codecs heavily used?
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.
The main thing is that even the default registry is changed in the MongoClient constructor in order to apply the uuidRepresentation to the registry. The default registry will throw when trying to encode a UUID value, because the default UuidRepresentation is UNSPECIFIED
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.
Just a note:
MongoClientSettings.getDefaultCodecRegistry()
is also used explicitly in the default implementation of theMongoConverter.getCodecRegistry
method.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.
true, same reason for those upgrading. As long as the user does not provide a custom implementation of the converter the codec registry (with all the user settings regarding UUID codec,...) will be used.