@@ -15,47 +15,33 @@
|
|||||||
*/
|
*/
|
||||||
package org.springframework.data.mongodb.core.aggregation;
|
package org.springframework.data.mongodb.core.aggregation;
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
import org.bson.conversions.Bson;
|
import org.bson.conversions.Bson;
|
||||||
|
|
||||||
import org.springframework.data.mongodb.util.BsonUtils;
|
import org.springframework.data.mongodb.util.BsonUtils;
|
||||||
import org.springframework.util.ObjectUtils;
|
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
|
* @author Christoph Strobl
|
||||||
* @since 4.0
|
* @since 4.0
|
||||||
*/
|
*/
|
||||||
class BasicAggregationOperation implements AggregationOperation {
|
record BasicAggregationOperation(Object value) implements AggregationOperation {
|
||||||
|
|
||||||
private final Object value;
|
|
||||||
|
|
||||||
BasicAggregationOperation(Object value) {
|
|
||||||
this.value = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Document toDocument(AggregationOperationContext context) {
|
public Document toDocument(AggregationOperationContext context) {
|
||||||
|
|
||||||
if (value instanceof Document document) {
|
|
||||||
return document;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (value instanceof Bson bson) {
|
if (value instanceof Bson bson) {
|
||||||
return BsonUtils.asDocument(bson, context.getCodecRegistry());
|
return BsonUtils.asDocument(bson, context.getCodecRegistry());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (value instanceof Map map) {
|
|
||||||
return new Document(map);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (value instanceof String json && BsonUtils.isJsonDocument(json)) {
|
if (value instanceof String json && BsonUtils.isJsonDocument(json)) {
|
||||||
return BsonUtils.parse(json, context);
|
return BsonUtils.parse(json, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new IllegalStateException(
|
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)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -89,7 +89,7 @@ public class BsonUtils {
|
|||||||
* Return the {@link Bson} object as {@link Map}. Depending on the input type, the return value can be either a casted
|
* Return the {@link Bson} object as {@link Map}. Depending on the input type, the return value can be either a casted
|
||||||
* version of {@code bson} or a converted (detached from the original value) using the given {@link CodecRegistry} to
|
* version of {@code bson} or a converted (detached from the original value) using the given {@link CodecRegistry} to
|
||||||
* obtain {@link org.bson.codecs.Codec codecs} that might be required for conversion.
|
* obtain {@link org.bson.codecs.Codec codecs} that might be required for conversion.
|
||||||
*
|
*
|
||||||
* @param bson can be {@literal null}.
|
* @param bson can be {@literal null}.
|
||||||
* @param codecRegistry must not be {@literal null}.
|
* @param codecRegistry must not be {@literal null}.
|
||||||
* @return never {@literal null}. Returns an empty {@link Map} if input {@link Bson} is {@literal null}.
|
* @return never {@literal null}. Returns an empty {@link Map} if input {@link Bson} is {@literal null}.
|
||||||
@@ -138,10 +138,6 @@ public class BsonUtils {
|
|||||||
*/
|
*/
|
||||||
public static Document asDocument(Bson bson, CodecRegistry codecRegistry) {
|
public static Document asDocument(Bson bson, CodecRegistry codecRegistry) {
|
||||||
|
|
||||||
if (bson instanceof Document document) {
|
|
||||||
return document;
|
|
||||||
}
|
|
||||||
|
|
||||||
Map<String, Object> map = asMap(bson, codecRegistry);
|
Map<String, Object> map = asMap(bson, codecRegistry);
|
||||||
|
|
||||||
if (map instanceof Document) {
|
if (map instanceof Document) {
|
||||||
|
|||||||
@@ -34,6 +34,8 @@ import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
|
|||||||
import com.mongodb.MongoClientSettings;
|
import com.mongodb.MongoClientSettings;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Unit tests for {@link BasicAggregationOperation}.
|
||||||
|
*
|
||||||
* @author Christoph Strobl
|
* @author Christoph Strobl
|
||||||
*/
|
*/
|
||||||
@ExtendWith(MockitoExtension.class)
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
|||||||
Reference in New Issue
Block a user