Skip to content

Commit aae584f

Browse files
author
Dmitry Tkachenko
committed
Missing cast to array type #24
1 parent 50118d9 commit aae584f

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

include/redis-cpp/value.h

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111
#define REDISCPP_VALUE_H_
1212

1313
// STD
14+
#include <algorithm>
1415
#include <iosfwd>
16+
#include <iterator>
1517
#include <memory>
1618
#include <string_view>
19+
#include <vector>
1720

1821
// REDIS-CPP
1922
#include <redis-cpp/detail/config.h>
@@ -142,6 +145,18 @@ class value final
142145
get_value<std::string_view, resp::deserialization::bulk_string>();
143146
}
144147

148+
[[nodiscard]]
149+
auto as_string_array() const
150+
{
151+
return get_array<std::string>();
152+
}
153+
154+
[[nodiscard]]
155+
auto as_integer_array() const
156+
{
157+
return get_array<std::int64_t>();
158+
}
159+
145160
template <typename T>
146161
operator T () const
147162
{
@@ -201,13 +216,36 @@ class value final
201216
[&result] (T const &val)
202217
{
203218
if (is_null(&val))
204-
throw std::logic_error("You can't cast Null to any type.");
219+
throw std::logic_error("You can't cast Null to a type.");
205220
result = val.get();
206221
}
207222
}, get());
208223

209224
return result;
210225
}
226+
227+
template <typename T>
228+
std::vector<T> get_array() const
229+
{
230+
std::vector<T> result;
231+
232+
std::visit(resp::detail::overloaded{
233+
[] (auto const &)
234+
{ throw std::bad_cast{}; },
235+
[&result] (resp::deserialization::array const &val)
236+
{
237+
if (is_null(&val))
238+
throw std::logic_error("You can't cast Null to a type.");
239+
auto const &array = val.get();
240+
std::transform(std::begin(array), std::end(array),
241+
std::back_inserter(result),
242+
[] (auto const &i) { return value{i}.as<T>(); }
243+
);
244+
}
245+
}, get());
246+
247+
return result;
248+
}
211249
};
212250

213251
} // namespace rediscpp

0 commit comments

Comments
 (0)