tylerliu | bcd8348 | 2020-10-07 14:45:28 -0700 | [diff] [blame] | 1 | // |
| 2 | // Created by Tyler on 10/6/20. |
| 3 | // |
| 4 | |
| 5 | #include <iosfwd> |
| 6 | #include "assignment-or.hpp" |
| 7 | |
| 8 | namespace ndn { |
| 9 | namespace ndncert { |
| 10 | |
tylerliu | bcd8348 | 2020-10-07 14:45:28 -0700 | [diff] [blame] | 11 | NDNCERT_REGISTER_FUNCFACTORY(AssignmentOr, "or"); |
| 12 | |
| 13 | AssignmentOr::AssignmentOr() |
| 14 | : NameAssignmentFuncFactory("or") |
| 15 | { |
| 16 | } |
| 17 | |
| 18 | NameAssignmentFunc |
| 19 | AssignmentOr::getFunction(std::list<NameAssignmentFunc> funcs){ |
| 20 | if (funcs.size() == 1) return *funcs.begin(); |
tylerliu | 4251013 | 2020-10-07 15:48:02 -0700 | [diff] [blame^] | 21 | return [funcs](const std::vector<std::tuple<std::string, std::string>> params){ |
| 22 | std::vector<PartialName> nameList; |
| 23 | for (const auto& func : funcs) { |
| 24 | auto result = func(params); |
| 25 | nameList.insert(nameList.end(), result.begin(), result.end()); |
| 26 | } |
| 27 | |
| 28 | return nameList; |
| 29 | }; |
tylerliu | bcd8348 | 2020-10-07 14:45:28 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | NameAssignmentFunc |
| 33 | AssignmentOr::getFunction(const std::string &factoryParam) { |
| 34 | std::list<NameAssignmentFunc> paramList; |
| 35 | std::stringstream ss; |
| 36 | ss << factoryParam; |
| 37 | JsonSection section; |
| 38 | try { |
| 39 | boost::property_tree::read_json(ss, section); |
| 40 | } |
| 41 | catch (const std::exception& error) { |
| 42 | BOOST_THROW_EXCEPTION(std::runtime_error(std::string("Failed to parse configuration for name assignment function or, ") + error.what())); |
| 43 | } |
| 44 | if (section.begin() == section.end()) { |
| 45 | BOOST_THROW_EXCEPTION(std::runtime_error("No JSON configuration found for name assignment function")); |
| 46 | } |
| 47 | for (const auto& item: section) { |
| 48 | auto factory = NameAssignmentFuncFactory::createNameAssignmentFuncFactory(item.first); |
| 49 | if (!factory) { |
| 50 | BOOST_THROW_EXCEPTION(std::runtime_error("Invalid assignment factory type")); |
| 51 | } |
| 52 | try { |
| 53 | paramList.push_back(factory->getFunction(item.second.data())); |
| 54 | } catch (const std::exception& e) { |
| 55 | BOOST_THROW_EXCEPTION(std::runtime_error("Error on creating function")); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return getFunction(paramList); |
| 60 | } |
| 61 | |
tylerliu | bcd8348 | 2020-10-07 14:45:28 -0700 | [diff] [blame] | 62 | } |
| 63 | } |