Add Generic getNumber Method to AggregationResult · Issue #1851 · googleapis/java-datastore · GitHub | Latest TMZ Celebrity News & Gossip | Watch TMZ Live
Skip to content

Add Generic getNumber Method to AggregationResult #1851

Open
@st4sik

Description

@st4sik

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    api: datastoreIssues related to the googleapis/java-datastore API.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      TMZ Celebrity News – Breaking Stories, Videos & Gossip

      Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

      Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

      🎥 Watch TMZ Live

      TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.