@@ -15,47 +15,33 @@
|
||||
*/
|
||||
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
|
||||
* {@link AggregationOperation} implementation that can return a {@link Document} from a {@link Bson} or {@link String}
|
||||
* document.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @since 4.0
|
||||
*/
|
||||
class BasicAggregationOperation implements AggregationOperation {
|
||||
|
||||
private final Object value;
|
||||
|
||||
BasicAggregationOperation(Object value) {
|
||||
this.value = value;
|
||||
}
|
||||
record BasicAggregationOperation(Object value) implements AggregationOperation {
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
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)));
|
||||
String.format("%s cannot be converted to org.bson.Document", ObjectUtils.nullSafeClassName(value)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -138,10 +138,6 @@ public class BsonUtils {
|
||||
*/
|
||||
public static Document asDocument(Bson bson, CodecRegistry codecRegistry) {
|
||||
|
||||
if (bson instanceof Document document) {
|
||||
return document;
|
||||
}
|
||||
|
||||
Map<String, Object> map = asMap(bson, codecRegistry);
|
||||
|
||||
if (map instanceof Document) {
|
||||
|
||||
@@ -34,6 +34,8 @@ import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
|
||||
import com.mongodb.MongoClientSettings;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link BasicAggregationOperation}.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
|
||||
Reference in New Issue
Block a user