Skip to content

Commit 63673f3

Browse files
authored
refactor(datastore): Add validateReadOptions method (#8985)
* refactor(datastore): Add validateReadOptions method * refactor(datastore): Unindenting happy path
1 parent 9eba896 commit 63673f3

File tree

2 files changed

+88
-11
lines changed

2 files changed

+88
-11
lines changed

datastore/query.go

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ func (q *Query) toRunQueryRequest(req *pb.RunQueryRequest) error {
503503
return err
504504
}
505505

506-
req.ReadOptions, err = parseReadOptions(q)
506+
req.ReadOptions, err = parseReadOptions(q.eventual, q.trans)
507507
if err != nil {
508508
return err
509509
}
@@ -730,6 +730,8 @@ func (c *Client) Run(ctx context.Context, q *Query) *Iterator {
730730
ProjectId: c.dataset,
731731
DatabaseId: c.databaseID,
732732
},
733+
trans: q.trans,
734+
eventual: q.eventual,
733735
}
734736

735737
if q.namespace != "" {
@@ -786,7 +788,7 @@ func (c *Client) RunAggregationQuery(ctx context.Context, aq *AggregationQuery)
786788
}
787789

788790
// Parse the read options.
789-
req.ReadOptions, err = parseReadOptions(aq.query)
791+
req.ReadOptions, err = parseReadOptions(aq.query.eventual, aq.query.trans)
790792
if err != nil {
791793
return nil, err
792794
}
@@ -808,21 +810,33 @@ func (c *Client) RunAggregationQuery(ctx context.Context, aq *AggregationQuery)
808810
return ar, nil
809811
}
810812

813+
func validateReadOptions(eventual bool, t *Transaction) error {
814+
if t == nil {
815+
return nil
816+
}
817+
if t.id == nil {
818+
return errExpiredTransaction
819+
}
820+
if eventual {
821+
return errors.New("datastore: cannot use EventualConsistency query in a transaction")
822+
}
823+
return nil
824+
}
825+
811826
// parseReadOptions translates Query read options into protobuf format.
812-
func parseReadOptions(q *Query) (*pb.ReadOptions, error) {
813-
if t := q.trans; t != nil {
814-
if t.id == nil {
815-
return nil, errExpiredTransaction
816-
}
817-
if q.eventual {
818-
return nil, errors.New("datastore: cannot use EventualConsistency query in a transaction")
819-
}
827+
func parseReadOptions(eventual bool, t *Transaction) (*pb.ReadOptions, error) {
828+
err := validateReadOptions(eventual, t)
829+
if err != nil {
830+
return nil, err
831+
}
832+
833+
if t != nil {
820834
return &pb.ReadOptions{
821835
ConsistencyType: &pb.ReadOptions_Transaction{Transaction: t.id},
822836
}, nil
823837
}
824838

825-
if q.eventual {
839+
if eventual {
826840
return &pb.ReadOptions{ConsistencyType: &pb.ReadOptions_ReadConsistency_{ReadConsistency: pb.ReadOptions_EVENTUAL}}, nil
827841
}
828842

@@ -858,6 +872,14 @@ type Iterator struct {
858872
pageCursor []byte
859873
// entityCursor is the compiled cursor of the next result.
860874
entityCursor []byte
875+
876+
// trans records the transaction in which the query was run
877+
// Currently, this value is set but unused
878+
trans *Transaction
879+
880+
// eventual records whether the query was eventual
881+
// Currently, this value is set but unused
882+
eventual bool
861883
}
862884

863885
// Next returns the key of the next result. When there are no more results,

datastore/query_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,3 +858,58 @@ func TestAggregationQueryIsNil(t *testing.T) {
858858
t.Fatal(err)
859859
}
860860
}
861+
862+
func TestValidateReadOptions(t *testing.T) {
863+
for _, test := range []struct {
864+
desc string
865+
eventual bool
866+
trans *Transaction
867+
wantErr error
868+
}{
869+
{
870+
desc: "EventualConsistency query in a transaction",
871+
eventual: true,
872+
trans: &Transaction{
873+
id: []byte("test id"),
874+
},
875+
wantErr: errors.New("datastore: cannot use EventualConsistency query in a transaction"),
876+
},
877+
{
878+
desc: "Expired transaction in non-eventual query",
879+
trans: &Transaction{
880+
id: nil,
881+
},
882+
wantErr: errExpiredTransaction,
883+
},
884+
{
885+
desc: "Expired transaction in eventual query",
886+
trans: &Transaction{
887+
id: nil,
888+
},
889+
eventual: true,
890+
wantErr: errExpiredTransaction,
891+
},
892+
{
893+
desc: "No transaction in non-eventual query",
894+
},
895+
{
896+
desc: "No transaction in eventual query",
897+
eventual: true,
898+
},
899+
} {
900+
gotErr := validateReadOptions(test.eventual, test.trans)
901+
gotErrMsg := ""
902+
if gotErr != nil {
903+
gotErrMsg = gotErr.Error()
904+
}
905+
906+
wantErrMsg := ""
907+
if test.wantErr != nil {
908+
wantErrMsg = test.wantErr.Error()
909+
}
910+
911+
if gotErrMsg != wantErrMsg {
912+
t.Errorf("%q: got: %v, want: %v", test.desc, gotErr, test.wantErr)
913+
}
914+
}
915+
}

0 commit comments

Comments
 (0)