chore(storage): refactor read conditions
@@ -30,7 +30,6 @@ import (
"cloud.google.com/go/internal/trace"
"google.golang.org/api/googleapi"
storagepb "google.golang.org/genproto/googleapis/storage/v2"
- "google.golang.org/protobuf/proto"
)
var crc32cTable = crc32.MakeTable(crc32.Castagnoli)
@@ -475,8 +474,10 @@ func (o *ObjectHandle) newRangeReaderWithGRPC(ctx context.Context, offset, lengt
}
req.ReadOffset = start
- // TODO: refactor this to use applyCondsProto.
- setRequestConditions(req, o.conds)
+ if err := applyCondsProto("reopenWithGRPC", o.gen, o.conds, req); err != nil {
+ cancel()
+ return nil, nil, err
+ }
var stream storagepb.Storage_ReadObjectClient
var msg *storagepb.ReadObjectResponse
@@ -681,29 +682,6 @@ func (r *Reader) reopenStream(seen int64) (*storagepb.ReadObjectResponse, error)
return res.response, nil
}
-// setRequestConditions is used to apply the given Conditions to a gRPC request
-// message.
-//
-// This is an experimental API and not intended for public use.
-func setRequestConditions(req *storagepb.ReadObjectRequest, conds *Conditions) {
- if conds == nil {
- return
- }
- if conds.MetagenerationMatch != 0 {
- req.IfMetagenerationMatch = proto.Int64(conds.MetagenerationMatch)
- } else if conds.MetagenerationNotMatch != 0 {
- req.IfMetagenerationNotMatch = proto.Int64(conds.MetagenerationNotMatch)
- }
- switch {
- case conds.GenerationNotMatch != 0:
- req.IfGenerationNotMatch = proto.Int64(conds.GenerationNotMatch)
- case conds.GenerationMatch != 0:
- req.IfGenerationMatch = proto.Int64(conds.GenerationMatch)
- case conds.DoesNotExist:
- req.IfGenerationMatch = proto.Int64(0)
- }
-}
-
// Size returns the size of the object in bytes.
// The returned value is always the same and is not affected by
// calls to Read or Close.
