Skip to content

Commit ff9d5a9

Browse files
Update method names and wording.
- MongoExpresssion#expressionFromString(String) -> MongoExpresssion#create - AggregationExpression#create(MongoExpression) ->A ggregationExpression#from - MongoExpression#as -> removed from public API
1 parent 9369bd6 commit ff9d5a9

File tree

6 files changed

+49
-52
lines changed

6 files changed

+49
-52
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/BindableMongoExpression.java

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@
2525
import org.springframework.util.StringUtils;
2626

2727
/**
28-
* A {@link MongoExpression} using the {@link ParameterBindingDocumentCodec} for parsing the {@literal json} expression.
29-
* The expression will be wrapped within <code>{ }</code> if necessary. Placeholders like {@code ?0} are resolved when
30-
* first obtaining the target {@link Document} via {@link #toDocument()}.
28+
* A {@link MongoExpression} using the {@link ParameterBindingDocumentCodec} for parsing a raw ({@literal json})
29+
* expression. The expression will be wrapped within <code>{ ... }</code> if necessary. The actual parsing and parameter
30+
* binding of placeholders like {@code ?0} is delayed upon first call on the the target {@link Document} via
31+
* {@link #toDocument()}.
3132
* <p />
3233
*
3334
* <pre class="code">
@@ -38,16 +39,15 @@
3839
* { '$toUpper' : '?0' }, "$name" -> { '$toUpper' : '$name' }
3940
* </pre>
4041
*
41-
* Some types (like {@link java.util.UUID}) cannot be used directly but require a special {@link org.bson.codecs.Codec}.
42-
* Make sure to provide a {@link CodecRegistry} containing the required {@link org.bson.codecs.Codec codecs} via
43-
* {@link #withCodecRegistry(CodecRegistry)}.
42+
* Some types might require a special {@link org.bson.codecs.Codec}. If so, make sure to provide a {@link CodecRegistry}
43+
* containing the required {@link org.bson.codecs.Codec codec} via {@link #withCodecRegistry(CodecRegistry)}.
4444
*
4545
* @author Christoph Strobl
4646
* @since 3.2
4747
*/
4848
public class BindableMongoExpression implements MongoExpression {
4949

50-
private final String json;
50+
private final String expressionString;
5151

5252
@Nullable //
5353
private final CodecRegistryProvider codecRegistryProvider;
@@ -60,24 +60,24 @@ public class BindableMongoExpression implements MongoExpression {
6060
/**
6161
* Create a new instance of {@link BindableMongoExpression}.
6262
*
63-
* @param json must not be {@literal null}.
63+
* @param expression must not be {@literal null}.
6464
* @param args can be {@literal null}.
6565
*/
66-
public BindableMongoExpression(String json, @Nullable Object[] args) {
67-
this(json, null, args);
66+
public BindableMongoExpression(String expression, @Nullable Object[] args) {
67+
this(expression, null, args);
6868
}
6969

7070
/**
7171
* Create a new instance of {@link BindableMongoExpression}.
7272
*
73-
* @param json must not be {@literal null}.
73+
* @param expression must not be {@literal null}.
7474
* @param codecRegistryProvider can be {@literal null}.
7575
* @param args can be {@literal null}.
7676
*/
77-
public BindableMongoExpression(String json, @Nullable CodecRegistryProvider codecRegistryProvider,
77+
public BindableMongoExpression(String expression, @Nullable CodecRegistryProvider codecRegistryProvider,
7878
@Nullable Object[] args) {
7979

80-
this.json = wrapJsonIfNecessary(json);
80+
this.expressionString = expression;
8181
this.codecRegistryProvider = codecRegistryProvider;
8282
this.args = args;
8383
this.target = Lazy.of(this::parse);
@@ -90,7 +90,7 @@ public BindableMongoExpression(String json, @Nullable CodecRegistryProvider code
9090
* @return new instance of {@link BindableMongoExpression}.
9191
*/
9292
public BindableMongoExpression withCodecRegistry(CodecRegistry codecRegistry) {
93-
return new BindableMongoExpression(json, () -> codecRegistry, args);
93+
return new BindableMongoExpression(expressionString, () -> codecRegistry, args);
9494
}
9595

9696
/**
@@ -100,19 +100,27 @@ public BindableMongoExpression withCodecRegistry(CodecRegistry codecRegistry) {
100100
* @return new instance of {@link BindableMongoExpression}.
101101
*/
102102
public BindableMongoExpression bind(Object... args) {
103-
return new BindableMongoExpression(json, codecRegistryProvider, args);
103+
return new BindableMongoExpression(expressionString, codecRegistryProvider, args);
104104
}
105105

106106
/*
107107
* (non-Javadoc)
108-
*
109108
* @see org.springframework.data.mongodb.MongoExpression#toDocument()
110109
*/
111110
@Override
112111
public Document toDocument() {
113112
return target.get();
114113
}
115114

115+
/*
116+
* (non-Javadoc)
117+
* @see java.lang.Object#toString()
118+
*/
119+
@Override
120+
public String toString() {
121+
return "BindableMongoExpression{" + "expressionString='" + expressionString + '\'' + ", args=" + args + '}';
122+
}
123+
116124
private String wrapJsonIfNecessary(String json) {
117125

118126
if (StringUtils.hasText(json) && (json.startsWith("{") && json.endsWith("}"))) {
@@ -124,18 +132,20 @@ private String wrapJsonIfNecessary(String json) {
124132

125133
private Document parse() {
126134

135+
String expression = wrapJsonIfNecessary(expressionString);
136+
127137
if (ObjectUtils.isEmpty(args)) {
128138

129139
if (codecRegistryProvider == null) {
130-
return Document.parse(json);
140+
return Document.parse(expression);
131141
}
132142

133-
return Document.parse(json, codecRegistryProvider.getCodecFor(Document.class)
143+
return Document.parse(expression, codecRegistryProvider.getCodecFor(Document.class)
134144
.orElseGet(() -> new DocumentCodec(codecRegistryProvider.getCodecRegistry())));
135145
}
136146

137147
ParameterBindingDocumentCodec codec = codecRegistryProvider == null ? new ParameterBindingDocumentCodec()
138148
: new ParameterBindingDocumentCodec(codecRegistryProvider.getCodecRegistry());
139-
return codec.decode(json, args);
149+
return codec.decode(expression, args);
140150
}
141151
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/MongoExpression.java

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package org.springframework.data.mongodb;
1717

18-
import java.util.function.Function;
19-
2018
/**
2119
* Wrapper object for MongoDB expressions like {@code $toUpper : $name} that manifest as {@link org.bson.Document} when
2220
* passed on to the driver.
@@ -36,7 +34,7 @@
3634
* @see org.springframework.data.mongodb.core.aggregation.DateOperators
3735
* @see org.springframework.data.mongodb.core.aggregation.ObjectOperators
3836
* @see org.springframework.data.mongodb.core.aggregation.SetOperators
39-
* @see org.springframework.data.mongodb.core.aggregation.StringOperators @
37+
* @see org.springframework.data.mongodb.core.aggregation.StringOperators
4038
*/
4139
@FunctionalInterface
4240
public interface MongoExpression {
@@ -49,38 +47,27 @@ public interface MongoExpression {
4947
org.bson.Document toDocument();
5048

5149
/**
52-
* Convert this instance to another expression applying the given conversion {@link Function}.
53-
*
54-
* @param function must not be {@literal null}.
55-
* @param <T>
56-
* @return never {@literal null}.
57-
*/
58-
default <T extends MongoExpression> T as(Function<MongoExpression, T> function) {
59-
return function.apply(this);
60-
}
61-
62-
/**
63-
* Create a new {@link MongoExpression} from plain String (eg. {@code $toUpper : $name}). <br />
64-
* The given source value will be wrapped with <code>{ }</code> to match an actual MongoDB {@link org.bson.Document}
50+
* Create a new {@link MongoExpression} from plain {@link String} (eg. {@code $toUpper : $name}). <br />
51+
* The given expression will be wrapped with <code>{ ... }</code> to match an actual MongoDB {@link org.bson.Document}
6552
* if necessary.
6653
*
67-
* @param json must not be {@literal null}.
54+
* @param expression must not be {@literal null}.
6855
* @return new instance of {@link MongoExpression}.
6956
*/
70-
static MongoExpression expressionFromString(String json) {
71-
return new BindableMongoExpression(json, null);
57+
static MongoExpression create(String expression) {
58+
return new BindableMongoExpression(expression, null);
7259
}
7360

7461
/**
75-
* Create a new {@link MongoExpression} from plain String containing placeholders (eg. {@code $toUpper : ?0}) that
76-
* will be resolved on {@link #toDocument()}. <br />
77-
* The given source value will be wrapped with <code>{ }</code> to match an actual MongoDB {@link org.bson.Document}
62+
* Create a new {@link MongoExpression} from plain {@link String} containing placeholders (eg. {@code $toUpper : ?0})
63+
* that will be resolved on first call of {@link #toDocument()}. <br />
64+
* The given expression will be wrapped with <code>{ ... }</code> to match an actual MongoDB {@link org.bson.Document}
7865
* if necessary.
7966
*
80-
* @param json must not be {@literal null}.
67+
* @param expression must not be {@literal null}.
8168
* @return new instance of {@link MongoExpression}.
8269
*/
83-
static MongoExpression expressionFromString(String json, Object... args) {
84-
return new BindableMongoExpression(json, args);
70+
static MongoExpression create(String expression, Object... args) {
71+
return new BindableMongoExpression(expression, args);
8572
}
8673
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/QueryOperations.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,7 @@ Document getMappedFields(@Nullable MongoPersistentEntity<?> entity, Class<?> tar
300300
AggregationOperationContext ctx = entity == null ? Aggregation.DEFAULT_CONTEXT
301301
: new RelaxedTypeBasedAggregationOperationContext(entity.getType(), mappingContext, queryMapper);
302302

303-
fields.put(entry.getKey(),
304-
((MongoExpression) entry.getValue()).as(AggregationExpression::create).toDocument(ctx));
303+
fields.put(entry.getKey(), AggregationExpression.from((MongoExpression) entry.getValue()).toDocument(ctx));
305304
} else {
306305
fields.put(entry.getKey(), entry.getValue());
307306
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AggregationExpression.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,15 @@ default Document toDocument() {
4141
}
4242

4343
/**
44-
* Create an {@link AggregationExpression} out of a given {@link MongoExpression}. <br />
45-
* If the given expression is already an {@link AggregationExpression} return the very same instance.
44+
* Create an {@link AggregationExpression} out of a given {@link MongoExpression} to ensure the resulting
45+
* {@link MongoExpression#toDocument() Document} is mapped against the {@link AggregationOperationContext}. <br />
46+
* If the given expression is already an {@link AggregationExpression} the very same instance is returned.
4647
*
4748
* @param expression must not be {@literal null}.
4849
* @return never {@literal null}.
4950
* @since 3.2
5051
*/
51-
static AggregationExpression create(MongoExpression expression) {
52+
static AggregationExpression from(MongoExpression expression) {
5253

5354
if (expression instanceof AggregationExpression) {
5455
return AggregationExpression.class.cast(expression);

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateFieldProjectionTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void afterEach() {
6868
void usesMongoExpressionAsIs() {
6969

7070
Person result = findLuke(fields -> {
71-
fields.include("firstname").project(MongoExpression.expressionFromString("'$toUpper' : '$last_name'"))
71+
fields.include("firstname").project(MongoExpression.create("'$toUpper' : '$last_name'"))
7272
.as("last_name");
7373
});
7474

@@ -79,7 +79,7 @@ void usesMongoExpressionAsIs() {
7979
void usesMongoExpressionWithPlaceholdersAsIs() {
8080

8181
Person result = findLuke(fields -> {
82-
fields.include("firstname").project(MongoExpression.expressionFromString("'$toUpper' : '$?0'", "last_name"))
82+
fields.include("firstname").project(MongoExpression.create("'$toUpper' : '$?0'", "last_name"))
8383
.as("last_name");
8484
});
8585

src/main/asciidoc/reference/mongodb.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1291,7 +1291,7 @@ Starting with MongoDB 4.4 it is possible to use the aggregation expressions synt
12911291
[source,java]
12921292
----
12931293
query.fields()
1294-
.project(MongoExpression.expressionFromString("'$toUpper' : '$last_name'")) <1>
1294+
.project(MongoExpression.create("'$toUpper' : '$last_name'")) <1>
12951295
.as("last_name"); <2>
12961296
12971297
query.fields()

0 commit comments

Comments
 (0)