Browse Source

DATAMONGO-788 - Polishing.

Slightly changed the way the the simple reference rendering for projections is implemented. Introduced an isAliased() method on Field to be able to determine whether the field reference has been renamed explicitly.

Original pull request: #90.
pull/125/merge
Oliver Gierke 12 years ago
parent
commit
5d7e12a4fa
  1. 9
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ExposedFields.java
  2. 7
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/Field.java
  3. 9
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/Fields.java
  4. 9
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperation.java
  5. 4
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/AggregationUnitTests.java
  6. 2
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperationUnitTests.java

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

@ -259,6 +259,15 @@ public class ExposedFields implements Iterable<ExposedField> { @@ -259,6 +259,15 @@ public class ExposedFields implements Iterable<ExposedField> {
return field.getTarget();
}
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.aggregation.Field#isAliased()
*/
@Override
public boolean isAliased() {
return field.isAliased();
}
/**
* Returns whether the field can be referred to using the given name.
*

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

@ -36,4 +36,11 @@ public interface Field { @@ -36,4 +36,11 @@ public interface Field {
* @return must not be {@literal null}.
*/
String getTarget();
/**
* Returns whether the Field is aliased, which means it has a name set different from the target.
*
* @return
*/
boolean isAliased();
}

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

@ -224,6 +224,15 @@ public class Fields implements Iterable<Field> { @@ -224,6 +224,15 @@ public class Fields implements Iterable<Field> {
return StringUtils.hasText(this.target) ? this.target : this.name;
}
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.aggregation.Field#isAliased()
*/
@Override
public boolean isAliased() {
return !getName().equals(getTarget());
}
/*
* (non-Javadoc)
* @see java.lang.Object#toString()

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

@ -507,15 +507,8 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation { @@ -507,15 +507,8 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
// check whether referenced field exists in the context
FieldReference reference = context.getReference(field.getTarget());
return reference.isSynthetic() && !field.isAliased() ? 1 : reference.toString();
if (field.getName().equals(field.getTarget()) && reference.isSynthetic()) {
// render field as included
return 1;
}
// render field reference
return reference.toString();
} else if (Boolean.FALSE.equals(value)) {
// render field as excluded

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

@ -117,7 +117,7 @@ public class AggregationUnitTests { @@ -117,7 +117,7 @@ public class AggregationUnitTests {
@SuppressWarnings("unchecked")
DBObject secondProjection = ((List<DBObject>) agg.get("pipeline")).get(2);
DBObject fields = DBObjectUtils.getAsDBObject(secondProjection, "$project");
assertThat((Integer) fields.get("aCnt"), is(1));
assertThat((String) fields.get("a"), is("$_id.a"));
assertThat(fields.get("aCnt"), is((Object) 1));
assertThat(fields.get("a"), is((Object) "$_id.a"));
}
}

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

@ -67,7 +67,7 @@ public class ProjectionOperationUnitTests { @@ -67,7 +67,7 @@ public class ProjectionOperationUnitTests {
DBObject dbObject = operation.toDBObject(Aggregation.DEFAULT_CONTEXT);
DBObject projectClause = DBObjectUtils.getAsDBObject(dbObject, PROJECT);
assertThat((Integer) projectClause.get("foo"), is(1));
assertThat(projectClause.get("foo"), is((Object) 1));
assertThat(projectClause.get("bar"), is((Object) "$foobar"));
}

Loading…
Cancel
Save