Skip to content

Commit b6ee363

Browse files
committed
DATAMONGO-1538 - Polishing.
Use InheritingExposedFieldsAggregationOperationContext instead of anonymous context class. Drop aggregation input collections before tests. Minor reformatting.
1 parent 8f5a49a commit b6ee363

File tree

3 files changed

+10
-52
lines changed

3 files changed

+10
-52
lines changed

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

Lines changed: 6 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.springframework.data.mongodb.core.aggregation.AggregationExpressions.Filter.AsBuilder;
2929
import org.springframework.data.mongodb.core.aggregation.AggregationExpressions.Let.ExpressionVariable;
3030
import org.springframework.data.mongodb.core.aggregation.ExposedFields.ExposedField;
31-
import org.springframework.data.mongodb.core.aggregation.ExposedFields.FieldReference;
3231
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
3332
import org.springframework.util.Assert;
3433
import org.springframework.util.ClassUtils;
@@ -3905,21 +3904,8 @@ public static AsBuilder filter(List<?> values) {
39053904
*/
39063905
@Override
39073906
public DBObject toDbObject(final AggregationOperationContext context) {
3908-
3909-
return toFilter(new ExposedFieldsAggregationOperationContext(ExposedFields.from(as), context) {
3910-
3911-
@Override
3912-
public FieldReference getReference(Field field) {
3913-
3914-
FieldReference ref = null;
3915-
try {
3916-
ref = context.getReference(field);
3917-
} catch (Exception e) {
3918-
// just ignore that one.
3919-
}
3920-
return ref != null ? ref : super.getReference(field);
3921-
}
3922-
});
3907+
return toFilter(
3908+
new InheritingExposedFieldsAggregationOperationContext(ExposedFields.from(as), context));
39233909
}
39243910

39253911
private DBObject toFilter(AggregationOperationContext context) {
@@ -6019,22 +6005,7 @@ public Map andApply(final AggregationExpression expression) {
60196005

60206006
@Override
60216007
public DBObject toDbObject(final AggregationOperationContext context) {
6022-
6023-
return toMap(new ExposedFieldsAggregationOperationContext(
6024-
ExposedFields.synthetic(Fields.fields(itemVariableName)), context) {
6025-
6026-
@Override
6027-
public FieldReference getReference(Field field) {
6028-
6029-
FieldReference ref = null;
6030-
try {
6031-
ref = context.getReference(field);
6032-
} catch (Exception e) {
6033-
// just ignore that one.
6034-
}
6035-
return ref != null ? ref : super.getReference(field);
6036-
}
6037-
});
6008+
return toMap(new InheritingExposedFieldsAggregationOperationContext(ExposedFields.synthetic(Fields.fields(itemVariableName)), context));
60386009
}
60396010

60406011
private DBObject toMap(AggregationOperationContext context) {
@@ -6792,22 +6763,7 @@ public interface LetBuilder {
67926763

67936764
@Override
67946765
public DBObject toDbObject(final AggregationOperationContext context) {
6795-
6796-
return toLet(new ExposedFieldsAggregationOperationContext(
6797-
ExposedFields.synthetic(Fields.fields(getVariableNames())), context) {
6798-
6799-
@Override
6800-
public FieldReference getReference(Field field) {
6801-
6802-
FieldReference ref = null;
6803-
try {
6804-
ref = context.getReference(field);
6805-
} catch (Exception e) {
6806-
// just ignore that one.
6807-
}
6808-
return ref != null ? ref : super.getReference(field);
6809-
}
6810-
});
6766+
return toLet(new InheritingExposedFieldsAggregationOperationContext(ExposedFields.synthetic(Fields.fields(getVariableNames())), context));
68116767
}
68126768

68136769
private String[] getVariableNames() {
@@ -6816,14 +6772,15 @@ private String[] getVariableNames() {
68166772
for (int i = 0; i < this.vars.size(); i++) {
68176773
varNames[i] = this.vars.get(i).variableName;
68186774
}
6775+
68196776
return varNames;
68206777
}
68216778

68226779
private DBObject toLet(AggregationOperationContext context) {
68236780

68246781
DBObject letExpression = new BasicDBObject();
6825-
68266782
DBObject mappedVars = new BasicDBObject();
6783+
68276784
for (ExpressionVariable var : this.vars) {
68286785
mappedVars.putAll(getMappedVariable(var, context));
68296786
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,16 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
package org.springframework.data.mongodb.core.aggregation;
1817

1918
import org.springframework.data.mongodb.core.aggregation.ExposedFields.FieldReference;
20-
import org.springframework.util.Assert;
2119

2220
/**
2321
* {@link ExposedFieldsAggregationOperationContext} that inherits fields from its parent
2422
* {@link AggregationOperationContext}.
2523
*
2624
* @author Mark Paluch
25+
* @since 1.9
2726
*/
2827
class InheritingExposedFieldsAggregationOperationContext extends ExposedFieldsAggregationOperationContext {
2928

@@ -40,7 +39,7 @@ public InheritingExposedFieldsAggregationOperationContext(ExposedFields exposedF
4039
AggregationOperationContext previousContext) {
4140

4241
super(exposedFields, previousContext);
43-
Assert.notNull(previousContext, "PreviousContext must not be null!");
42+
4443
this.previousContext = previousContext;
4544
}
4645

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ private void cleanDb() {
143143
mongoTemplate.dropCollection(MeterData.class);
144144
mongoTemplate.dropCollection(LineItem.class);
145145
mongoTemplate.dropCollection(InventoryItem.class);
146+
mongoTemplate.dropCollection(Sales.class);
147+
mongoTemplate.dropCollection(Sales2.class);
146148
}
147149

148150
/**

0 commit comments

Comments
 (0)