5#include <unordered_map>
6#include <unordered_set>
9#include <userver/storages/redis/impl/types.hpp>
10#include <userver/utils/void_t.hpp>
12#include <userver/storages/redis/reply_types.hpp>
14USERVER_NAMESPACE_BEGIN
16namespace storages::
redis {
19ReplyData&& ExtractData(ReplyPtr& reply);
21template <
typename Result,
typename ReplyType,
typename = utils::void_t<>>
22struct HasParseFunctionFromRedisReply {
23 static constexpr bool value =
false;
26template <
typename Result,
typename ReplyType>
27struct HasParseFunctionFromRedisReply<
30 utils::void_t<
decltype(Result::Parse(std::declval<ReplyData>(), std::declval<
const std::string&>()))>> {
31 static constexpr bool value = std::is_same<
32 decltype(Result::Parse(std::declval<ReplyData>(), std::declval<
const std::string&>())),
36bool IsNil(
const ReplyData& reply_data);
38void ExpectIsOk(
const ReplyPtr& reply,
const std::string& request_description);
40void ExpectArray(
const ReplyData& reply_data,
const std::string& request_description);
42const std::string& RequestDescription(
const ReplyPtr& reply,
const std::string& request_description);
48template <
typename Result,
typename ReplyType = Result>
51std::vector<std::string>
52ParseReplyDataArray(ReplyData&& array_data,
const std::string& request_description, To<std::vector<std::string>>);
54std::vector<std::optional<std::string>>
55ParseReplyDataArray(ReplyData&& array_data,
const std::string& request_description, To<std::vector<std::optional<std::string>>>);
57std::vector<std::pair<std::string, std::string>>
58ParseReplyDataArray(ReplyData&& array_data,
const std::string& request_description, To<std::vector<std::pair<std::string, std::string>>>);
60std::vector<MemberScore>
61ParseReplyDataArray(ReplyData&& array_data,
const std::string& request_description, To<std::vector<MemberScore>>);
64ParseReplyDataArray(ReplyData&& array_data,
const std::string& request_description, To<std::vector<GeoPoint>>);
66std::string Parse(ReplyData&& reply_data,
const std::string& request_description, To<std::string>);
68double Parse(ReplyData&& reply_data,
const std::string& request_description, To<
double>);
70size_t Parse(ReplyData&& reply_data,
const std::string& request_description, To<size_t>);
72bool Parse(ReplyData&& reply_data,
const std::string& request_description, To<size_t,
bool>);
74int64_t Parse(ReplyData&& reply_data,
const std::string& request_description, To<int64_t>);
76std::chrono::system_clock::time_point
77Parse(ReplyData&& reply_data,
const std::string& request_description, To<std::chrono::system_clock::time_point>);
79HsetReply Parse(ReplyData&& reply_data,
const std::string& request_description, To<HsetReply>);
81PersistReply Parse(ReplyData&& reply_data,
const std::string& request_description, To<PersistReply>);
83KeyType Parse(ReplyData&& reply_data,
const std::string& request_description, To<KeyType>);
85void Parse(ReplyData&& reply_data,
const std::string& request_description,
To<StatusOk,
void>);
87bool Parse(ReplyData&& reply_data,
const std::string& request_description,
To<std::optional<StatusOk>,
bool>);
89void Parse(ReplyData&& reply_data,
const std::string& request_description,
To<StatusPong,
void>);
91SetReply Parse(ReplyData&& reply_data,
const std::string& request_description, To<SetReply>);
93std::unordered_set<std::string>
94Parse(ReplyData&& reply_data,
const std::string& request_description, To<std::unordered_set<std::string>>);
96std::unordered_map<std::string, std::string>
97Parse(ReplyData&& reply_data,
const std::string& request_description, To<std::unordered_map<std::string, std::string>>);
99ReplyData Parse(ReplyData&& reply_data,
const std::string& request_description, To<ReplyData>);
101template <
typename Result,
typename ReplyType = Result>
102std::enable_if_t<impl::HasParseFunctionFromRedisReply<Result, ReplyType>::value, ReplyType>
103Parse(ReplyData&& reply_data,
const std::string& request_description, To<Result, ReplyType>) {
104 return Result::Parse(std::move(reply_data), request_description);
108std::vector<T> Parse(ReplyData&& reply_data,
const std::string& request_description, To<std::vector<T>>) {
109 impl::ExpectArray(reply_data, request_description);
110 return ParseReplyDataArray(std::move(reply_data), request_description, To<std::vector<T>>{});
114std::optional<T> Parse(ReplyData&& reply_data,
const std::string& request_description, To<std::optional<T>>) {
115 if (impl::IsNil(reply_data))
return std::nullopt;
116 return Parse(std::move(reply_data), request_description, To<T>{});
119template <
typename Result,
typename ReplyType = Result>
120ReplyType ParseReply(ReplyPtr reply,
const std::string& request_description = {}) {
121 const auto& description = impl::RequestDescription(reply, request_description);
122 impl::ExpectIsOk(reply, description);
123 return Parse(impl::ExtractData(reply), description, To<Result, ReplyType>{});