Commit 9fe3598e authored by Dominik Charousset's avatar Dominik Charousset

Add URI to config_value

Add `uri` to the sum type `config_value` and extend the INI parser to
recognize `<uri>` notation.
parent a2873264
...@@ -35,6 +35,7 @@ ...@@ -35,6 +35,7 @@
#include "caf/sum_type_access.hpp" #include "caf/sum_type_access.hpp"
#include "caf/sum_type_token.hpp" #include "caf/sum_type_token.hpp"
#include "caf/timestamp.hpp" #include "caf/timestamp.hpp"
#include "caf/uri.hpp"
#include "caf/variant.hpp" #include "caf/variant.hpp"
namespace caf { namespace caf {
...@@ -62,7 +63,7 @@ public: ...@@ -62,7 +63,7 @@ public:
using dictionary = caf::dictionary<config_value>; using dictionary = caf::dictionary<config_value>;
using types = detail::type_list<integer, boolean, real, atom, timespan, using types = detail::type_list<integer, boolean, real, atom, timespan, uri,
string, list, dictionary>; string, list, dictionary>;
using variant_type = detail::tl_apply_t<types, variant>; using variant_type = detail::tl_apply_t<types, variant>;
...@@ -156,8 +157,8 @@ private: ...@@ -156,8 +157,8 @@ private:
} }
template <class T> template <class T>
detail::enable_if_t< detail::enable_if_t<detail::is_one_of<T, real, atom, timespan, uri, string,
detail::is_one_of<T, real, atom, timespan, string, list, dictionary>::value> list, dictionary>::value>
set(T x) { set(T x) {
data_ = std::move(x); data_ = std::move(x);
} }
......
...@@ -28,8 +28,10 @@ ...@@ -28,8 +28,10 @@
#include "caf/detail/parser/read_bool.hpp" #include "caf/detail/parser/read_bool.hpp"
#include "caf/detail/parser/read_number_or_timespan.hpp" #include "caf/detail/parser/read_number_or_timespan.hpp"
#include "caf/detail/parser/read_string.hpp" #include "caf/detail/parser/read_string.hpp"
#include "caf/detail/parser/read_uri.hpp"
#include "caf/detail/scope_guard.hpp" #include "caf/detail/scope_guard.hpp"
#include "caf/pec.hpp" #include "caf/pec.hpp"
#include "caf/uri_builder.hpp"
CAF_PUSH_UNUSED_LABEL_WARNING CAF_PUSH_UNUSED_LABEL_WARNING
...@@ -140,6 +142,32 @@ void read_ini_map(state<Iterator, Sentinel>& ps, Consumer&& consumer) { ...@@ -140,6 +142,32 @@ void read_ini_map(state<Iterator, Sentinel>& ps, Consumer&& consumer) {
fin(); fin();
} }
template <class Iterator, class Sentinel, class Consumer>
void read_ini_uri(state<Iterator, Sentinel>& ps, Consumer&& consumer) {
uri_builder builder;
auto g = make_scope_guard([&] {
if (ps.code <= pec::trailing_character)
consumer.value(builder.make());
});
start();
state(init) {
transition(init, " \t\n")
transition(before_uri, '<')
}
state(before_uri) {
transition(before_uri, " \t\n")
fsm_epsilon(read_uri(ps, builder), after_uri)
}
state(after_uri) {
transition(after_uri, " \t\n")
transition(done, '>')
}
term_state(done) {
// nop
}
fin();
}
template <class Iterator, class Sentinel, class Consumer> template <class Iterator, class Sentinel, class Consumer>
void read_ini_value(state<Iterator, Sentinel>& ps, Consumer&& consumer) { void read_ini_value(state<Iterator, Sentinel>& ps, Consumer&& consumer) {
start(); start();
...@@ -149,6 +177,7 @@ void read_ini_value(state<Iterator, Sentinel>& ps, Consumer&& consumer) { ...@@ -149,6 +177,7 @@ void read_ini_value(state<Iterator, Sentinel>& ps, Consumer&& consumer) {
fsm_epsilon(read_number(ps, consumer), done, '.') fsm_epsilon(read_number(ps, consumer), done, '.')
fsm_epsilon(read_bool(ps, consumer), done, "ft") fsm_epsilon(read_bool(ps, consumer), done, "ft")
fsm_epsilon(read_number_or_timespan(ps, consumer), done, "0123456789+-") fsm_epsilon(read_number_or_timespan(ps, consumer), done, "0123456789+-")
fsm_epsilon(read_ini_uri(ps, consumer), done, '<')
fsm_transition(read_ini_list(ps, consumer.begin_list()), done, '[') fsm_transition(read_ini_list(ps, consumer.begin_list()), done, '[')
fsm_transition(read_ini_map(ps, consumer.begin_map()), done, '{') fsm_transition(read_ini_map(ps, consumer.begin_map()), done, '{')
} }
......
...@@ -35,6 +35,7 @@ const char* type_names[] { ...@@ -35,6 +35,7 @@ const char* type_names[] {
"real", "real",
"atom", "atom",
"timespan", "timespan",
"uri",
"string", "string",
"list", "list",
"dictionary", "dictionary",
......
...@@ -63,7 +63,12 @@ struct test_consumer { ...@@ -63,7 +63,12 @@ struct test_consumer {
template <class T> template <class T>
void value(T x) { void value(T x) {
add_entry("value: ", deep_to_string(x)); config_value cv{std::move(x)};
std::string entry = "value (";
entry += cv.type_name();
entry += "): ";
entry += to_string(cv);
log.emplace_back(std::move(entry));
} }
void add_entry(const char* prefix, std::string str) { void add_entry(const char* prefix, std::string str) {
...@@ -87,8 +92,10 @@ struct fixture { ...@@ -87,8 +92,10 @@ struct fixture {
res.i = str.begin(); res.i = str.begin();
res.e = str.end(); res.e = str.end();
detail::parser::read_ini(res, f); detail::parser::read_ini(res, f);
if ((res.code == pec::success) != expect_success) if ((res.code == pec::success) != expect_success) {
CAF_MESSAGE("unexpected parser result state: " << res.code); CAF_MESSAGE("unexpected parser result state: " << res.code);
CAF_MESSAGE("input remainder: " << std::string(res.i, res.e));
}
return std::move(f.log); return std::move(f.log);
} }
}; };
...@@ -125,17 +132,61 @@ entry1=123, ...@@ -125,17 +132,61 @@ entry1=123,
entry3= "abc", entry3= "abc",
entry4 = 'def', ; some comment and a trailing comma entry4 = 'def', ; some comment and a trailing comma
} }
[middleman]
preconnect=[<
tcp://localhost:8080
>,<udp://remotehost?trust=false>]
)"; )";
const auto ini0_log = make_log( const auto ini0_log = make_log(
"key: logger", "{", "key: padding", "value: 10", "key: file-name", "key: logger",
"value: \"foobar.ini\"", "}", "key: scheduler", "{", "key: timing", "{",
"value: 2000ns", "key: impl", "value: 'foo'", "key: x_", "key: padding",
"value: " + deep_to_string(.123), "key: some-bool", "value: true", "value (integer): 10",
"key: some-other-bool", "value: false", "key: some-list", "[", "value: 123", "key: file-name",
"value: 23", "value: \"abc\"", "value: 'def'", "]", "key: some-map", "{", "value (string): \"foobar.ini\"",
"key: entry1", "value: 123", "key: entry2", "value: 23", "key: entry3", "}",
"value: \"abc\"", "key: entry4", "value: 'def'", "}", "}"); "key: scheduler",
"{",
"key: timing",
"value (timespan): 2000ns",
"key: impl",
"value (atom): 'foo'",
"key: x_",
"value (real): " + deep_to_string(.123),
"key: some-bool",
"value (boolean): true",
"key: some-other-bool",
"value (boolean): false",
"key: some-list",
"[",
"value (integer): 123",
"value (integer): 23",
"value (string): \"abc\"",
"value (atom): 'def'",
"]",
"key: some-map",
"{",
"key: entry1",
"value (integer): 123",
"key: entry2",
"value (integer): 23",
"key: entry3",
"value (string): \"abc\"",
"key: entry4",
"value (atom): 'def'",
"}",
"}",
"key: middleman",
"{",
"key: preconnect",
"[",
"value (uri): tcp://localhost:8080",
"value (uri): udp://remotehost?trust=false",
"]",
"}"
);
} // namespace <anonymous> } // namespace <anonymous>
......
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