Commit ea11a6e9 authored by Dominik Charousset's avatar Dominik Charousset

Integrate review feedback

parent 82c456fe
......@@ -35,8 +35,8 @@
namespace caf {
/// Deserializes objects from sequence of bytes. Does not perform run-time type
/// checks.
/// Deserializes C++ objects from sequence of bytes. Does not perform run-time
/// type checks.
class CAF_CORE_EXPORT binary_deserializer
: public load_inspector_base<binary_deserializer> {
public:
......
......@@ -121,8 +121,8 @@ public:
/// Tries to parse a value from `str`.
static expected<config_value> parse(string_view str);
/// Tries to parse a config value (list) from `str` and then converting it to
/// an allowed input message type for `Handle`.
/// Tries to parse a config value (list) from `str` and to convert it to an
/// allowed input message type for `Handle`.
template <class Handle>
static optional<message> parse_msg(string_view str, const Handle&) {
auto allowed = Handle::allowed_inputs();
......
......@@ -308,12 +308,7 @@ expected<timespan> config_value::to_timespan() const {
auto f = detail::make_overload(
no_conversions<timespan, none_t, bool, integer, real, uri,
config_value::list, config_value::dictionary>(),
[](timespan x) {
// This cast may lose precision on the value. We could try and check that,
// but refusing to convert on loss of precision could also be unexpected
// behavior. So we rather always convert, even if it costs precision.
return result_type{x};
},
[](timespan x) { return result_type{x}; },
[](const std::string& x) {
auto tmp = timespan{};
if (detail::parse(x, tmp) == none)
......@@ -328,9 +323,18 @@ expected<timespan> config_value::to_timespan() const {
expected<config_value::list> config_value::to_list() const {
using result_type = expected<list>;
auto dict_to_list = [](const dictionary& dict, list& result) {
for (const auto& [key, val] : dict) {
list kvp;
kvp.reserve(2);
kvp.emplace_back(key);
kvp.emplace_back(val);
result.emplace_back(std::move(kvp));
}
};
auto f = detail::make_overload(
no_conversions<list, none_t, bool, integer, real, timespan, uri>(),
[](const std::string& x) {
[dict_to_list](const std::string& x) {
// Check whether we can parse the string as a list. If that fails, try
// whether we can parse it as a dictionary instead (and then convert that
// to a list).
......@@ -340,13 +344,7 @@ expected<config_value::list> config_value::to_list() const {
config_value::dictionary dict;
if (detail::parse(x, dict, detail::require_opening_char) == none) {
tmp.clear();
for (const auto& [key, val] : dict) {
list kvp;
kvp.reserve(2);
kvp.emplace_back(key);
kvp.emplace_back(val);
tmp.emplace_back(std::move(kvp));
}
dict_to_list(dict, tmp);
return result_type{std::move(tmp)};
}
std::string msg = "cannot convert ";
......@@ -355,15 +353,9 @@ expected<config_value::list> config_value::to_list() const {
return result_type{make_error(sec::conversion_failed, std::move(msg))};
},
[](const list& x) { return result_type{x}; },
[](const dictionary& x) {
[dict_to_list](const dictionary& x) {
list tmp;
for (const auto& [key, val] : x) {
list kvp;
kvp.reserve(2);
kvp.emplace_back(key);
kvp.emplace_back(val);
tmp.emplace_back(std::move(kvp));
}
dict_to_list(x, tmp);
return result_type{std::move(tmp)};
});
return visit(f, data_);
......
......@@ -593,6 +593,7 @@ bool config_value_reader::value(span<byte> bytes) {
bool config_value_reader::fetch_object_type(const settings* obj,
type_id_t& type) {
if (auto str = get_if<std::string>(obj, "@type"); str == nullptr) {
// fetch_next_object_type only calls this function
type = type_id_v<config_value::dictionary>;
return true;
} else if (auto id = query_type_id(*str); id != invalid_type_id) {
......
......@@ -296,7 +296,7 @@ SCENARIO("get_as can convert config values to floating point numbers") {
}
}
GIVEN("config_values of null, URI, boolean, list or dictionary") {
WHEN("using get_as with floating point types") {
WHEN("using get_as with integer types") {
THEN("conversion fails") {
CHECK_EQ(get_as<int64_t>(cv_null), sec::conversion_failed);
CHECK_EQ(get_as<int64_t>(cv_true), sec::conversion_failed);
......@@ -323,7 +323,7 @@ SCENARIO("get_as can convert config values to timespans") {
THEN("conversion fails") {
CHECK_EQ(get_as<int64_t>(x), sec::conversion_failed);
CHECK_EQ(get_as<double>(x), sec::conversion_failed);
// CHECK_EQ(get_as<uri>(x), sec::conversion_failed);
CHECK_EQ(get_as<uri>(x), sec::conversion_failed);
CHECK_EQ(get_as<config_value::list>(x), sec::conversion_failed);
CHECK_EQ(get_as<config_value::dictionary>(x), sec::conversion_failed);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment