Skip to content

Commit ec16b87

Browse files
christophstroblmp911de
authored andcommitted
Add support for $degreesToRadians aggregation operator.
Closes: #3714 Original pull request: #3755.
1 parent 2a3a4cf commit ec16b87

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

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

+58
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,16 @@ public ToString convertToString() {
231231
return ToString.toString(valueObject());
232232
}
233233

234+
/**
235+
* {@link AggregationExpression} for {@code $degreesToRadians} that converts an input value measured in degrees to radians.\
236+
*
237+
* @return new instance of {@link DegreesToRadians}.
238+
* @since 3.3
239+
*/
240+
public DegreesToRadians convertDegreesToRadians() {
241+
return DegreesToRadians.degreesToRadians(valueObject());
242+
}
243+
234244
private Convert createConvert() {
235245
return usesFieldRef() ? Convert.convertValueOf(fieldReference) : Convert.convertValueOf(expression);
236246
}
@@ -692,4 +702,52 @@ protected String getMongoMethod() {
692702
return "$toString";
693703
}
694704
}
705+
706+
/**
707+
* {@link AggregationExpression} for {@code $degreesToRadians} that converts an input value measured in degrees to radians.
708+
*
709+
* @author Christoph Strobl
710+
* @since 3.3
711+
*/
712+
public static class DegreesToRadians extends AbstractAggregationExpression {
713+
714+
private DegreesToRadians(Object value) {
715+
super(value);
716+
}
717+
718+
/**
719+
* Create a new instance of {@link DegreesToRadians} that converts the value of the given field, measured in degrees, to radians.
720+
*
721+
* @param fieldName must not be {@literal null}.
722+
* @return new instance of {@link DegreesToRadians}.
723+
*/
724+
public static DegreesToRadians degreesToRadiansOf(String fieldName) {
725+
return degreesToRadians(Fields.field(fieldName));
726+
}
727+
728+
/**
729+
* Create a new instance of {@link DegreesToRadians} that converts the result of the given {@link AggregationExpression expression}, measured in degrees, to radians.
730+
*
731+
* @param expression must not be {@literal null}.
732+
* @return new instance of {@link DegreesToRadians}.
733+
*/
734+
public static DegreesToRadians degreesToRadiansOf(AggregationExpression expression) {
735+
return degreesToRadians(expression);
736+
}
737+
738+
/**
739+
* Create a new instance of {@link DegreesToRadians} that converts the given value, measured in degrees, to radians.
740+
*
741+
* @param value must not be {@literal null}.
742+
* @return new instance of {@link DegreesToRadians}.
743+
*/
744+
public static DegreesToRadians degreesToRadians(Object value) {
745+
return new DegreesToRadians(value);
746+
}
747+
748+
@Override
749+
protected String getMongoMethod() {
750+
return "$degreesToRadians";
751+
}
752+
}
695753
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ public class MethodReferenceNode extends ExpressionNode {
199199
map.put("toLong", singleArgRef().forOperator("$toLong"));
200200
map.put("toObjectId", singleArgRef().forOperator("$toObjectId"));
201201
map.put("toString", singleArgRef().forOperator("$toString"));
202+
map.put("degreesToRadians", singleArgRef().forOperator("$degreesToRadians"));
202203

203204
FUNCTIONS = Collections.unmodifiableMap(map);
204205
}

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

+7
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,11 @@ public void toStringUsingExpression() {
222222
assertThat(ConvertOperators.valueOf(EXPRESSION).convertToString().toDocument(Aggregation.DEFAULT_CONTEXT))
223223
.isEqualTo(Document.parse("{ $toString: " + EXPRESSION_STRING + " } "));
224224
}
225+
226+
@Test // GH-3714
227+
void degreesToRadiansUsingFieldReference() {
228+
229+
assertThat(ConvertOperators.valueOf("angle_a").convertDegreesToRadians().toDocument(Aggregation.DEFAULT_CONTEXT))
230+
.isEqualTo(Document.parse("{ $degreesToRadians : \"$angle_a\"}"));
231+
}
225232
}

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

+5
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,11 @@ void shouldRenderRoundWithPlace() {
941941
assertThat(transform("round(field, 2)")).isEqualTo(Document.parse("{ \"$round\" : [\"$field\", 2]}"));
942942
}
943943

944+
@Test // GH-3714
945+
void shouldRenderDegreesToRadians() {
946+
assertThat(transform("degreesToRadians(angle_a)")).isEqualTo(Document.parse("{ \"$degreesToRadians\" : \"$angle_a\"}"));
947+
}
948+
944949
@Test // GH-3712
945950
void shouldRenderCovariancePop() {
946951
assertThat(transform("covariancePop(field1, field2)"))

0 commit comments

Comments
 (0)