Skip to content

Commit c386028

Browse files
authored
refactor(common): re-use value extractors in synchronous polling loop (#6823)
1 parent c6d6f96 commit c386028

1 file changed

Lines changed: 8 additions & 45 deletions

File tree

google/cloud/internal/polling_loop.h

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_INTERNAL_POLLING_LOOP_H
1717

1818
#include "google/cloud/grpc_error_delegate.h"
19+
#include "google/cloud/internal/extract_long_running_result.h"
1920
#include "google/cloud/internal/invoke_result.h"
2021
#include "google/cloud/polling_policy.h"
2122
#include "google/cloud/status_or.h"
@@ -36,27 +37,10 @@ namespace internal {
3637
template <typename ResultType>
3738
struct PollingLoopResponseExtractor {
3839
using ReturnType = StatusOr<ResultType>;
39-
40-
static ReturnType Extract(google::longrunning::Operation const& operation,
40+
static ReturnType Extract(google::longrunning::Operation operation,
4141
char const* location) {
42-
if (!operation.has_response()) {
43-
return Status(StatusCode::kInternal,
44-
std::string(location) +
45-
"() operation completed "
46-
"without error or response, name=" +
47-
operation.name());
48-
}
49-
google::protobuf::Any const& any = operation.response();
50-
if (!any.Is<ResultType>()) {
51-
return Status(StatusCode::kInternal,
52-
std::string(location) +
53-
"() operation completed "
54-
"with an invalid response type, name=" +
55-
operation.name());
56-
}
57-
ResultType result;
58-
any.UnpackTo(&result);
59-
return result;
42+
return ExtractLongRunningResultResponse<ResultType>(std::move(operation),
43+
location);
6044
}
6145
};
6246

@@ -66,27 +50,10 @@ struct PollingLoopResponseExtractor {
6650
template <typename ResultType>
6751
struct PollingLoopMetadataExtractor {
6852
using ReturnType = StatusOr<ResultType>;
69-
70-
static ReturnType Extract(google::longrunning::Operation const& operation,
53+
static ReturnType Extract(google::longrunning::Operation operation,
7154
char const* location) {
72-
if (!operation.has_metadata()) {
73-
return Status(StatusCode::kInternal,
74-
std::string(location) +
75-
"() operation completed "
76-
"without error or metadata, name=" +
77-
operation.name());
78-
}
79-
google::protobuf::Any const& any = operation.metadata();
80-
if (!any.Is<ResultType>()) {
81-
return Status(StatusCode::kInternal,
82-
std::string(location) +
83-
"() operation completed "
84-
"with an invalid metadata type, name=" +
85-
operation.name());
86-
}
87-
ResultType result;
88-
any.UnpackTo(&result);
89-
return result;
55+
return ExtractLongRunningResultMetadata<ResultType>(std::move(operation),
56+
location);
9057
}
9158
};
9259

@@ -154,11 +121,7 @@ typename ValueExtractor::ReturnType PollingLoopImpl(
154121
}
155122
}
156123

157-
if (operation.has_error()) {
158-
// The long running operation failed, return the error to the caller.
159-
return google::cloud::MakeStatusFromRpcError(operation.error());
160-
}
161-
return ValueExtractor::Extract(operation, location);
124+
return ValueExtractor::Extract(std::move(operation), location);
162125
}
163126

164127
/// @copydoc RetryLoopImpl

0 commit comments

Comments
 (0)