DATAMONGO-2047 - Polishing.
Retain previous options when calling withTimezone(…)/onNull…(…). Add tests. Javadoc. Original pull request: #593.
This commit is contained in:
@@ -1409,7 +1409,7 @@ public class DateOperators {
|
||||
public DateToString withTimezone(Timezone timezone) {
|
||||
|
||||
Assert.notNull(timezone, "Timezone must not be null.");
|
||||
return new DateToString(argumentMap(get("date"), get("format"), timezone));
|
||||
return new DateToString(append("timezone", timezone));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1455,7 +1455,7 @@ public class DateOperators {
|
||||
|
||||
private static java.util.Map<String, Object> argumentMap(Object date, @Nullable String format, Timezone timezone) {
|
||||
|
||||
java.util.Map<String, Object> args = new LinkedHashMap<String, Object>(2);
|
||||
java.util.Map<String, Object> args = new LinkedHashMap<>(2);
|
||||
|
||||
if (StringUtils.hasText(format)) {
|
||||
args.put("format", format);
|
||||
@@ -1469,6 +1469,25 @@ public class DateOperators {
|
||||
return args;
|
||||
}
|
||||
|
||||
protected java.util.Map<String, Object> append(String key, Object value) {
|
||||
|
||||
java.util.Map<String, Object> clone = new LinkedHashMap<>(argumentMap());
|
||||
|
||||
if (value instanceof Timezone) {
|
||||
|
||||
if (ObjectUtils.nullSafeEquals(value, Timezone.none())) {
|
||||
clone.remove("timezone");
|
||||
} else {
|
||||
clone.put("timezone", ((Timezone) value).value);
|
||||
}
|
||||
|
||||
} else {
|
||||
clone.put(key, value);
|
||||
}
|
||||
|
||||
return clone;
|
||||
}
|
||||
|
||||
public interface FormatBuilder {
|
||||
|
||||
/**
|
||||
|
||||
@@ -1207,7 +1207,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
|
||||
/**
|
||||
* Generates a {@code $dateToString} expression that takes the date representation of the previously mentioned field
|
||||
* using the server default format. <br />
|
||||
* strong>NOTE:</strong> Requires MongoDB 4.0 or later.
|
||||
* <strong>NOTE:</strong> Requires MongoDB 4.0 or later.
|
||||
*
|
||||
* @return
|
||||
* @since 2.0.10
|
||||
|
||||
@@ -1318,7 +1318,7 @@ public class ProjectionOperationUnitTests {
|
||||
Document.parse("{ $project: { time: { $dateToString: { format: \"%H:%M:%S:%L\", date: \"$date\" } } } }"));
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-1834
|
||||
@Test // DATAMONGO-1834, DATAMONGO-2047
|
||||
public void shouldRenderDateToStringAggregationExpressionWithTimezone() {
|
||||
|
||||
Document agg = project()
|
||||
@@ -1327,6 +1327,13 @@ public class ProjectionOperationUnitTests {
|
||||
|
||||
assertThat(agg).isEqualTo(Document.parse(
|
||||
"{ $project: { time: { $dateToString: { format: \"%H:%M:%S:%L\", date: \"$date\", \"timezone\" : \"America/Chicago\" } } } } } }"));
|
||||
|
||||
Document removedTimezone = project().and(DateOperators.dateOf("date")
|
||||
.withTimezone(Timezone.valueOf("America/Chicago")).toString("%H:%M:%S:%L").withTimezone(Timezone.none()))
|
||||
.as("time").toDocument(Aggregation.DEFAULT_CONTEXT);
|
||||
|
||||
assertThat(removedTimezone).isEqualTo(
|
||||
Document.parse("{ $project: { time: { $dateToString: { format: \"%H:%M:%S:%L\", date: \"$date\" } } } } } }"));
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-2047
|
||||
@@ -1340,6 +1347,29 @@ public class ProjectionOperationUnitTests {
|
||||
.parse("{ $project: { time: { $dateToString: { date: \"$date\", \"onNull\" : \"$fallback-field\" } } } }"));
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-2047
|
||||
public void shouldRenderDateToStringWithOnNullExpression() {
|
||||
|
||||
Document agg = project()
|
||||
.and(DateOperators.dateOf("date").toStringWithDefaultFormat()
|
||||
.onNullReturnValueOf(LiteralOperators.valueOf("my-literal").asLiteral()))
|
||||
.as("time").toDocument(Aggregation.DEFAULT_CONTEXT);
|
||||
|
||||
assertThat(agg).isEqualTo(Document.parse(
|
||||
"{ $project: { time: { $dateToString: { date: \"$date\", \"onNull\" : { \"$literal\": \"my-literal\"} } } } }"));
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-2047
|
||||
public void shouldRenderDateToStringWithOnNullAndTimezone() {
|
||||
|
||||
Document agg = project().and(DateOperators.dateOf("date").toStringWithDefaultFormat()
|
||||
.onNullReturnValueOf("fallback-field").withTimezone(Timezone.ofField("foo"))).as("time")
|
||||
.toDocument(Aggregation.DEFAULT_CONTEXT);
|
||||
|
||||
assertThat(agg).isEqualTo(Document.parse(
|
||||
"{ $project: { time: { $dateToString: { date: \"$date\", \"onNull\" : \"$fallback-field\", \"timezone\": \"$foo\" } } } }"));
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-1536
|
||||
public void shouldRenderSumAggregationExpression() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user