Description
Is your feature request related to a problem?
Currently, when working with com.google.cloud.datastore.AggregationResult
, it is necessary to explicitly choose between getLong(String)
and getDouble(String)
depending on the underlying value type. This requires additional logic in client code to handle cases where the aggregation result type is not known in advance.
The solution
I would like
Introduce a generic getNumber(String alias)
method in AggregationResult
that returns a Number
instance, automatically resolving the correct numeric type (Long or Double) based on the stored value. This will simplify client code and improve API ergonomics.
Draft code
com.google.cloud.datastore.AggregationResult
public Number getNumber(String alias) {
Value<?> value = properties.get(alias);
switch (value.getType()) {
case LONG -> (Long) value.get();
case DOUBLE -> (Double) value.get();
default:
throw new RuntimeException(
String.format("Unsupported type %s received for alias '%s'.", value.getType(), alias));
}
};
Example:
Number result = aggregationResult.getNumber("alias");
...
long longValue = result.longValue();
...
double doubleValue = result.doubleValue();
Alternatives I have considered:
- Implement utility methods in client projects to hide this logic, but this duplicates effort across codebases.
Additional context:
This enhancement would improve usability when dealing with aggregation queries that may return either LONG or DOUBLE types depending on data or aggregation functions used. It aligns with the principle of reducing unnecessary complexity for end-users.