userver: userver/ugrpc/server/result.hpp Source File
Loading...
Searching...
No Matches
result.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/ugrpc/server/result.hpp
4/// @brief @copybrief ugrpc::server::Result
5
6#include <optional>
7#include <variant>
8
9#include <grpcpp/support/status.h>
10
11#include <userver/utils/assert.hpp>
12#include <userver/utils/impl/internal_tag.hpp>
13
14#include <userver/ugrpc/server/impl/message_storage.hpp>
15
16USERVER_NAMESPACE_BEGIN
17
18namespace ugrpc::server {
19
20/// @brief Result type for service handlers (non server-streaming).
21///
22/// Provides a way to return one of:
23/// 1. `Response` + `grpc::Status::OK`;
24/// 2. error `grpc::Status`.
25///
26/// For server-streaming RPCs, see @ref ugrpc::server::StreamingResult.
27template <typename Response>
28class Result {
29public:
30 /// Construct from `Response`, implies `OK` status.
31 /*implicit*/ Result(Response&& response)
32 : result_{std::in_place_type<impl::MessageStorage<Response>>, std::move(response)}
33 {}
34
35 /// @brief Construct a successful result that borrows an arena-backed protobuf response.
36 /// @param response A protobuf message allocated on an arena.
37 /// @pre The arena must remain alive and must not be reset until result consumption,
38 /// serialization, and middleware hooks have finished.
39 /// @returns A result referring to @a response without taking ownership.
40 static Result BorrowArenaResponse(Response& response) {
41 UINVARIANT(response.GetArena(), "The response must be allocated on an arena");
42 return Result(std::ref(response));
43 }
44
45 /// Construct from `grpc::Status`, only error status is allowed.
46 /*implicit*/ Result(grpc::Status&& status)
47 : result_{std::move(status)}
48 {
49 UINVARIANT(!GetErrorStatus().ok(), "Only error status is allowed, for OK status a response should be provided");
50 }
51
52 /// Construct from `grpc::Status`, only error status is allowed.
53 /*implicit*/ Result(const grpc::Status& status)
54 : result_{status}
55 {
56 UINVARIANT(!GetErrorStatus().ok(), "Only error status is allowed, for OK status a response should be provided");
57 }
58
59 /// @returns `true` if the `Result` contains `Response`, as opposed to an error status.
60 bool IsSuccess() const { return std::holds_alternative<impl::MessageStorage<Response>>(result_); }
61
62 /// @returns the contained error status.
63 /// @pre `IsSuccess() == false`.
64 grpc::Status&& ExtractErrorStatus() {
65 UINVARIANT(!IsSuccess(), "ExtractErrorStatus is only allowed in case of error status");
66 return std::move(std::get<grpc::Status>(result_));
67 }
68
69 /// @returns the contained error status.
70 /// @pre `IsSuccess() == false`.
71 const grpc::Status& GetErrorStatus() const {
72 UINVARIANT(!IsSuccess(), "GetErrorStatus is only allowed in case of error status");
73 return std::get<grpc::Status>(result_);
74 }
75
76 /// @returns the contained response.
77 /// @pre `IsSuccess() == true`.
78 /// @warning Extracting an arena-backed response is usually a mistake: moving it into a standalone message
79 /// copies its data off the arena. The returned reference does not extend the arena's lifetime.
80 /// Prefer @ref ugrpc::server::Result::GetResponse for read-only access.
81 Response&& ExtractResponse() {
82 UINVARIANT(IsSuccess(), "ExtractResponse is only allowed in case of OK status");
83 return std::move(std::get<impl::MessageStorage<Response>>(result_).Get());
84 }
85
86 /// @returns the contained response.
87 /// @pre `IsSuccess() == true`.
88 const Response& GetResponse() const {
89 UINVARIANT(IsSuccess(), "GetResponse is only allowed in case of OK status");
90 return std::get<impl::MessageStorage<Response>>(result_).Get();
91 }
92
93 /// @cond
94 // For internal use only.
95 impl::MessageStorage<Response>&& ExtractStorage(utils::impl::InternalTag) {
96 return std::move(std::get<impl::MessageStorage<Response>>(result_));
97 }
98 /// @endcond
99
100private:
101 explicit Result(std::reference_wrapper<Response> response)
102 : result_{std::in_place_type<impl::MessageStorage<Response>>, response}
103 {}
104
105 std::variant<impl::MessageStorage<Response>, grpc::Status> result_;
106};
107
108/// @brief Special result type for server-streaming service handlers.
109///
110/// Provides a way to return one of:
111/// 1. `grpc::Status::OK`;
112/// 2. error `grpc::Status`;
113/// 3. last response + `grpc::Status::OK`, sent in a single batch.
114///
115/// For non-server-streaming RPCs, see @ref ugrpc::server::Result.
116template <typename Response>
117class StreamingResult final {
118public:
119 /// Construct from `grpc::Status` (which can be `OK` or an error status).
120 /*implicit*/ StreamingResult(grpc::Status&& status)
121 : status_{std::move(status)}
122 {}
123
124 /// Construct from `grpc::Status` (which can be `OK` or an error status).
125 /*implicit*/ StreamingResult(const grpc::Status& status)
126 : status_{status}
127 {}
128
129 /// Construct from last response. Allows to send last response and `OK` status coalesced in a single batch.
130 /*implicit*/ StreamingResult(Response&& last_response)
131 : last_response_(std::move(last_response))
132 {}
133
134 /// @brief Construct a successful result that borrows an arena-backed last response.
135 /// @param response A protobuf message allocated on an arena.
136 /// @pre The arena must remain alive and must not be reset until result consumption,
137 /// serialization, and middleware hooks have finished.
138 /// @returns A result referring to @a response without taking ownership.
139 static StreamingResult BorrowArenaResponse(Response& response) {
140 UINVARIANT(response.GetArena(), "The response must be allocated on an arena");
141 return StreamingResult(std::ref(response));
142 }
143
144 /// @returns `true` if the `StreamingResult` contains `OK` status, possibly with a last response.
145 bool IsSuccess() const { return status_.ok(); }
146
147 /// @returns the contained status, which can be `OK` or an error status.
148 grpc::Status&& ExtractStatus() { return std::move(status_); }
149
150 /// @returns the contained status, which can be `OK` or an error status.
151 const grpc::Status& GetStatus() const { return status_; }
152
153 /// @returns `true` if the `StreamingResult` contains last response, which implies `OK` status.
154 bool HasLastResponse() const { return last_response_.has_value(); }
155
156 /// @returns the contained last response.
157 /// @pre `HasLastResponse() == true`.
158 /// @warning Extracting an arena-backed response is usually a mistake: moving it into a standalone message
159 /// copies its data off the arena. The returned reference does not extend the arena's lifetime.
160 /// Prefer @ref ugrpc::server::StreamingResult::GetLastResponse for read-only access.
161 Response&& ExtractLastResponse() {
162 UINVARIANT(HasLastResponse(), "There is no last response in the StreamingResult");
163 return std::move(last_response_->Get());
164 }
165
166 /// @returns the contained last response.
167 /// @pre `HasLastResponse() == true`.
168 const Response& GetLastResponse() const {
169 UINVARIANT(HasLastResponse(), "There is no last response in the StreamingResult");
170 return last_response_->Get();
171 }
172
173 /// @cond
174 // For internal use only.
175 impl::MessageStorage<Response>&& ExtractStorage(utils::impl::InternalTag) { return std::move(*last_response_); }
176 /// @endcond
177
178private:
179 explicit StreamingResult(std::reference_wrapper<Response> response)
180 : last_response_(std::in_place, response)
181 {}
182
183 std::optional<impl::MessageStorage<Response>> last_response_;
184 grpc::Status status_{grpc::Status::OK};
185};
186
187} // namespace ugrpc::server
188
189USERVER_NAMESPACE_END