Line data Source code
1 :
2 : #include "array_match.hpp"
3 : #include "ibuilder.hpp"
4 :
5 : namespace wfc{ namespace jsonrpc{
6 :
7 11 : array_match::array_match( const std::shared_ptr<ibuilder>& builder )
8 11 : : _builder( builder )
9 : {
10 11 : }
11 :
12 11 : bool array_match::configure(const char* beg, const char* end, json::json_error& err)
13 : {
14 : typedef std::pair<const char*, const char*> field_pair_t;
15 : typedef std::vector< field_pair_t > pair_list_t;
16 : typedef json::vector_of< json::iterator_pair<field_pair_t> > pair_list_json;
17 11 : pair_list_t pair_list;
18 11 : pair_list_json::serializer()(pair_list, beg, end, &err);
19 11 : if (err)
20 0 : return false;
21 :
22 11 : _matchers.clear();
23 36 : for (auto p : pair_list)
24 : {
25 : // Передаем end, а не p.second, чтобы в случае ошибки получить правильное место где она
26 25 : auto m = _builder->build_value(p.first, end, err);
27 25 : if ( m==nullptr || err ) return false;
28 25 : if (!m->configure(p.first, end, err))
29 0 : return false;
30 25 : _matchers.push_back(m);
31 25 : }
32 11 : return true;
33 : }
34 :
35 15 : bool array_match::match(const char* beg, const char* end, json::json_error& err)
36 : {
37 15 : if ( _matchers.empty() )
38 0 : return true;
39 :
40 15 : if (!json::parser::is_array(beg,end))
41 4 : return false;
42 :
43 : typedef std::pair<const char*, const char*> field_pair_t;
44 : typedef std::vector< field_pair_t > pair_list_t;
45 : typedef json::vector_of< json::iterator_pair<field_pair_t> > pair_list_json;
46 11 : pair_list_t pair_list;
47 11 : pair_list_json::serializer()(pair_list, beg, end, &err);
48 11 : if (err) return false;
49 :
50 23 : return std::all_of( std::begin(_matchers), std::end(_matchers), [&](const matcher_list::value_type& m)
51 : {
52 41 : return std::any_of(std::begin(pair_list), std::end(pair_list), [&](const pair_list_t::value_type& p)
53 : {
54 41 : return m->match(p.first, end, err);
55 64 : });
56 34 : });
57 : }
58 :
59 : }}
|