Action Engine
Loading...
Searching...
No Matches
reply_converters.h
1// Copyright 2025 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef ACTIONENGINE_REDIS_REPLY_CONVERTERS_H_
16#define ACTIONENGINE_REDIS_REPLY_CONVERTERS_H_
17
18#include <absl/status/status.h>
19
20#include "actionengine/data/conversion.h"
21#include "actionengine/redis/reply.h"
22
23namespace act::redis {
24
25constexpr std::string_view MapReplyEnumToTypeName(ReplyType type) {
26 switch (type) {
27 case ReplyType::Status:
28 return "Status";
29 case ReplyType::Error:
30 return "Error";
31 case ReplyType::Integer:
32 return "Integer";
33 case ReplyType::Nil:
34 return "Nil";
35 case ReplyType::String:
36 return "String";
37 case ReplyType::Bool:
38 return "Bool";
39 case ReplyType::Double:
40 return "Double";
41 case ReplyType::Array:
42 return "Array";
43 case ReplyType::Map:
44 return "Map";
45 case ReplyType::Set:
46 return "Set";
47 case ReplyType::Push:
48 return "Push";
49 case ReplyType::Attr:
50 return "Attr";
51 case ReplyType::BigNum:
52 return "BigNum";
53 case ReplyType::Verbatim:
54 return "Verbatim";
55 default:
56 return "unknown";
57 }
58}
59
60// Converters to the primitive types of RESP2 and RESP3.
61absl::Status EgltAssignInto(const Reply& from, absl::Status* to);
62absl::Status EgltAssignInto(const Reply& from, int64_t* to);
63absl::Status EgltAssignInto(const Reply& from, double* to);
64absl::Status EgltAssignInto(Reply from, std::string* to);
65absl::Status EgltAssignInto(const Reply& from, bool* to);
66
67// Converters to the structured types of RESP2 and RESP3.
68absl::Status EgltAssignInto(Reply from, ArrayReplyData* to);
69absl::Status EgltAssignInto(Reply from, MapReplyData* to);
70absl::Status EgltAssignInto(Reply from, SetReplyData* to);
71absl::Status EgltAssignInto(Reply from, PushReplyData* to);
72absl::Status EgltAssignInto(Reply from, VerbatimReplyData* to);
73
74// Converters between structured types of RESP2 and RESP3.
75absl::Status EgltAssignInto(ArrayReplyData from, MapReplyData* to);
76absl::Status EgltAssignInto(MapReplyData from, ArrayReplyData* to);
77absl::Status EgltAssignInto(PushReplyData from, ArrayReplyData* to);
78
79// Converters of structured types into containers of Replies.
80absl::Status EgltAssignInto(ArrayReplyData from, std::vector<Reply>* to);
81absl::Status EgltAssignInto(ArrayReplyData from,
82 absl::flat_hash_map<std::string, Reply>* to);
83absl::Status EgltAssignInto(MapReplyData from, std::vector<Reply>* to);
84absl::Status EgltAssignInto(MapReplyData from,
85 absl::flat_hash_map<std::string, Reply>* to);
86absl::Status EgltAssignInto(SetReplyData from, std::vector<Reply>* to);
87absl::Status EgltAssignInto(PushReplyData from, std::vector<Reply>* to);
88absl::Status EgltAssignInto(VerbatimReplyData from, std::string* to);
89
90absl::Status EgltAssignInto(Reply from, std::vector<Reply>* to);
91absl::Status EgltAssignInto(Reply from,
92 absl::flat_hash_map<std::string, Reply>* to);
93
94// Converters of structured types into containers of native types.
95template <typename T>
96absl::Status EgltAssignInto(Reply from, std::vector<T>* to) {
97 ASSIGN_OR_RETURN(std::vector<Reply> reply_vector,
98 ConvertTo<std::vector<Reply>>(std::move(from)));
99 std::vector<T> converted_vector;
100 converted_vector.reserve(reply_vector.size());
101 for (const Reply& reply : reply_vector) {
102 T value;
103 ASSIGN_OR_RETURN(value, ConvertTo<T>(reply));
104 converted_vector.push_back(std::move(value));
105 }
106 *to = std::move(converted_vector);
107 return absl::OkStatus();
108}
109
110template <typename T>
111absl::Status EgltAssignInto(Reply from,
112 absl::flat_hash_map<std::string, T>* to) {
113 auto reply_map =
114 ConvertTo<absl::flat_hash_map<std::string, Reply>>(std::move(from));
115 RETURN_IF_ERROR(reply_map.status());
116 absl::flat_hash_map<std::string, T> converted_map;
117 converted_map.reserve(reply_map->size());
118 for (const auto& [key, reply] : *reply_map) {
119 T value;
120 ASSIGN_OR_RETURN(value, ConvertTo<T>(reply));
121 converted_map.emplace(std::move(key), std::move(value));
122 }
123 *to = std::move(converted_map);
124 return absl::OkStatus();
125}
126
127} // namespace act::redis
128
129#endif // ACTIONENGINE_REDIS_REPLY_CONVERTERS_H_