Commit b079d078 authored by Dominik Charousset's avatar Dominik Charousset

Simplify key lookup API

parent fdf82e06
......@@ -31,6 +31,7 @@
#include "caf/detail/type_traits.hpp"
#include "caf/fwd.hpp"
#include "caf/optional.hpp"
#include "caf/string_algorithms.hpp"
#include "caf/sum_type.hpp"
#include "caf/sum_type_access.hpp"
#include "caf/timestamp.hpp"
......@@ -359,11 +360,14 @@ struct config_value_access<std::map<std::string, V>> {
}
};
/// Retrieves.
/// Tries to retrieve the value associated to `name` from `xs`.
/// @relates config_value
template <class T>
optional<T> get_if(const config_value::dictionary* xs,
std::initializer_list<std::string> path) {
const std::string& name) {
// Split the name into a path.
std::vector<std::string> path;
split(path, name, ".");
// Sanity check.
if (path.size() == 0)
return none;
......@@ -388,43 +392,26 @@ optional<T> get_if(const config_value::dictionary* xs,
return none;
}
/// Retrieves the value associated to `name` from `xs`.
/// @relates config_value
template <class T>
optional<T> get_if(const config_value::dictionary* xs, std::string path) {
return get_if<T>(xs, {path});
}
/// @relates config_value
template <class T>
T get(const config_value::dictionary& xs,
std::initializer_list<std::string> path) {
auto result = get_if<T>(&xs, std::move(path));
if (!result)
CAF_RAISE_ERROR("invalid type found");
return std::move(*result);
}
/// @relates config_value
template <class T>
T get(const config_value::dictionary& xs, std::string path) {
return get<T>(xs, {path});
}
/// @relates config_value
template <class T>
T get_or(const config_value::dictionary& xs,
std::initializer_list<std::string> path, const T& default_value) {
auto result = get<T>(xs, std::move(path));
T get(const config_value::dictionary& xs, const std::string& name) {
auto result = get_if<T>(&xs, name);
if (result)
return *result;
return default_value;
return std::move(*result);
CAF_RAISE_ERROR("invalid type or name found");
}
/// Retrieves the value associated to `name` from `xs` or returns
/// `default_value`.
/// @relates config_value
template <class T>
T get_or(const config_value::dictionary& xs, std::string path,
T get_or(const config_value::dictionary& xs, const std::string& name,
const T& default_value) {
return get_or(xs, {path}, default_value);
auto result = get_if<T>(&xs, name);
if (result)
return std::move(*result);
return default_value;
}
/// @relates config_value
......
......@@ -124,19 +124,25 @@ CAF_TEST(append) {
CAF_TEST(homogeneous dictionary) {
using integer_map = std::map<std::string, int64_t>;
auto xs = dict()
.add("value-1", config_value{1})
.add("value-1", config_value{100000})
.add("value-2", config_value{2})
.add("value-3", config_value{3})
.add("value-4", config_value{4})
.make();
integer_map ys{
{"value-1", 1},
{"value-1", 100000},
{"value-2", 2},
{"value-3", 3},
{"value-4", 4},
};
CAF_CHECK_EQUAL(get<int64_t>(xs, "value-1"), 1);
CAF_CHECK_EQUAL(get<integer_map>(config_value{xs}), ys);
config_value xs_cv{xs};
CAF_CHECK_EQUAL(get_if<int64_t>(&xs, "value-1"), int64_t{100000});
CAF_CHECK_EQUAL(get_if<int32_t>(&xs, "value-1"), int32_t{100000});
CAF_CHECK_EQUAL(get_if<int16_t>(&xs, "value-1"), none);
CAF_CHECK_EQUAL(get<int64_t>(xs, "value-1"), 100000);
CAF_CHECK_EQUAL(get<int32_t>(xs, "value-1"), 100000);
CAF_CHECK_EQUAL(get_if<integer_map>(&xs_cv), ys);
CAF_CHECK_EQUAL(get<integer_map>(xs_cv), ys);
}
CAF_TEST(heterogeneous dictionary) {
......@@ -152,9 +158,9 @@ CAF_TEST(heterogeneous dictionary) {
.make_cv())
.make();
CAF_CHECK_EQUAL(get<atom_value>(xs, {"scheduler", "policy"}), atom("none"));
CAF_CHECK_EQUAL(get<int64_t>(xs, {"scheduler", "max-threads"}), 2);
CAF_CHECK_EQUAL(get_if<double>(&xs, {"scheduler", "max-threads"}), none);
CAF_CHECK_EQUAL(get<atom_value>(xs, "scheduler.policy"), atom("none"));
CAF_CHECK_EQUAL(get<int64_t>(xs, "scheduler.max-threads"), 2);
CAF_CHECK_EQUAL(get_if<double>(&xs, "scheduler.max-threads"), none);
string_list nodes{"sun", "venus", "mercury", "earth", "mars"};
CAF_CHECK_EQUAL(get<string_list>(xs, {"nodes", "preload"}), nodes);
CAF_CHECK_EQUAL(get<string_list>(xs, "nodes.preload"), nodes);
}
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