Skip to content

Commit 1f050ea

Browse files
MarioGravelMario Gravelmeredithslota
authored
fix(datastore): PKG:datastore TYPE:datastoreClient FUNC:RunAggregationQuery (#7803)
Bug: #7800 Calling `func (c *datastore.Client) RunAggregationQuery` panics at line `789` when calling `c.client.RunAggregationQuery`. `c.client` is defined `type pb.DatastoreClient interface` and it should have `RunAggregationQuery(ctx context.Context, in *RunAggregationQueryRequest, opts ...grpc.CallOption) (*RunAggregationQueryResponse, error)` but, `c.client` is implemented as a `type datastoreClient struct` and is missing `func (dc *datastoreClient) RunAggregationQuery(ctx context.Context, in *pb.RunAggregationQueryRequest, opts ...grpc.CallOption) (res *pb.RunAggregationQueryResponse, err error)`. This bug was covered by the fact that `datastoreClient` complies to `type pb.DatastoreClient interface` because the interface is part of it struct declaration: ```go // From /datastore/client.go, line 36 ... type datastoreClient struct { // Embed so we still implement the DatastoreClient interface, // if the interface adds more methods. pb.DatastoreClient ... ``` Also(Bonus): `func (c *datastore.Client) RunAggregationQuery(ctx context.Context, aq *AggregationQuery) (AggregationResult, error)` returning an `type AggregationResult map[string]interface{}` but the content of type `interface{}` are `*datastore.Value` from `google.golang.org/genproto/googleapis/datastore/v1/entity.pb.go`. Not very useful. Solution: Add `func (dc *datastoreClient) RunAggregationQuery(ctx context.Context, in *pb.RunAggregationQueryRequest, opts ...grpc.CallOption) (res *pb.RunAggregationQueryResponse, err error)` Add a `switch` statement to use concrete values from the `GetXXXValue() YYY` of `google.golang.org/genproto/googleapis/datastore/v1 :: datastore.Value` Co-authored-by: Mario Gravel <info@MarioGravel.dev> Co-authored-by: meredithslota <meredithslota@google.com>
1 parent 93d6a1a commit 1f050ea

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

datastore/client.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,17 @@ func (dc *datastoreClient) RunQuery(ctx context.Context, in *pb.RunQueryRequest,
7373
return res, err
7474
}
7575

76+
func (dc *datastoreClient) RunAggregationQuery(ctx context.Context, in *pb.RunAggregationQueryRequest, opts ...grpc.CallOption) (res *pb.RunAggregationQueryResponse, err error) {
77+
ctx = trace.StartSpan(ctx, "cloud.google.com/go/datastore.datastoreClient.RunAggregationQuery")
78+
defer func() { trace.EndSpan(ctx, err) }()
79+
80+
err = dc.invoke(ctx, func(ctx context.Context) error {
81+
res, err = dc.c.RunAggregationQuery(ctx, in, opts...)
82+
return err
83+
})
84+
return res, nil
85+
}
86+
7687
func (dc *datastoreClient) BeginTransaction(ctx context.Context, in *pb.BeginTransactionRequest, opts ...grpc.CallOption) (res *pb.BeginTransactionResponse, err error) {
7788
ctx = trace.StartSpan(ctx, "cloud.google.com/go/datastore.datastoreClient.BeginTransaction")
7889
defer func() { trace.EndSpan(ctx, err) }()

datastore/query.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,32 @@ func (c *Client) RunAggregationQuery(ctx context.Context, aq *AggregationQuery)
796796
// TODO(developer): change batch parsing logic if other aggregations are supported.
797797
for _, a := range res.Batch.AggregationResults {
798798
for k, v := range a.AggregateProperties {
799-
ar[k] = v
799+
switch v.ValueType.(type) {
800+
case *pb.Value_NullValue:
801+
ar[k] = v.GetNullValue()
802+
case *pb.Value_BooleanValue:
803+
ar[k] = v.GetBooleanValue()
804+
case *pb.Value_IntegerValue:
805+
ar[k] = v.GetIntegerValue()
806+
case *pb.Value_DoubleValue:
807+
ar[k] = v.GetDoubleValue()
808+
case *pb.Value_TimestampValue:
809+
ar[k] = v.GetTimestampValue()
810+
case *pb.Value_KeyValue:
811+
ar[k] = v.GetKeyValue()
812+
case *pb.Value_StringValue:
813+
ar[k] = v.GetStringValue()
814+
case *pb.Value_BlobValue:
815+
ar[k] = v.GetBlobValue()
816+
case *pb.Value_GeoPointValue:
817+
ar[k] = v.GetGeoPointValue()
818+
case *pb.Value_EntityValue:
819+
ar[k] = v.GetEntityValue()
820+
case *pb.Value_ArrayValue:
821+
ar[k] = v.GetArrayValue()
822+
default:
823+
ar[k] = v
824+
}
800825
}
801826
}
802827

0 commit comments

Comments
 (0)