weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 7df36bd | 2023-10-28 19:32:43 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, Regents of the University of California. |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of NDN repo-ng (Next generation of NDN repository). |
| 6 | * See AUTHORS.md for complete list of repo-ng authors and contributors. |
| 7 | * |
| 8 | * repo-ng is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * repo-ng is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * repo-ng, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 19 | |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 20 | #ifndef REPO_REPO_COMMAND_HPP |
| 21 | #define REPO_REPO_COMMAND_HPP |
| 22 | |
| 23 | #include "common.hpp" |
| 24 | #include "repo-command-parameter.hpp" |
| 25 | |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 26 | namespace repo { |
| 27 | |
| 28 | class RepoCommand : boost::noncopyable |
| 29 | { |
| 30 | public: |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 31 | /** |
Davide Pesavento | 7df36bd | 2023-10-28 19:32:43 -0400 | [diff] [blame] | 32 | * \brief Represents an error in RepoCommandParameter. |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 33 | */ |
| 34 | class ArgumentError : public std::invalid_argument |
| 35 | { |
| 36 | public: |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 37 | using std::invalid_argument::invalid_argument; |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 38 | }; |
| 39 | |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 40 | class FieldValidator |
| 41 | { |
| 42 | public: |
| 43 | FieldValidator(); |
| 44 | |
Davide Pesavento | 7df36bd | 2023-10-28 19:32:43 -0400 | [diff] [blame] | 45 | /** |
| 46 | * \brief Declare a required field. |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 47 | */ |
| 48 | FieldValidator& |
| 49 | required(RepoParameterField field) |
| 50 | { |
| 51 | m_required[field] = true; |
| 52 | return *this; |
| 53 | } |
| 54 | |
Davide Pesavento | 7df36bd | 2023-10-28 19:32:43 -0400 | [diff] [blame] | 55 | /** |
| 56 | * \brief Declare an optional field. |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 57 | */ |
| 58 | FieldValidator& |
| 59 | optional(RepoParameterField field) |
| 60 | { |
| 61 | m_optional[field] = true; |
| 62 | return *this; |
| 63 | } |
| 64 | |
| 65 | /** \brief verify that all required fields are present, |
| 66 | * and all present fields are either required or optional |
| 67 | * \throw ArgumentError |
| 68 | */ |
| 69 | void |
| 70 | validate(const RepoCommandParameter& parameters) const; |
| 71 | |
| 72 | private: |
| 73 | std::vector<bool> m_required; |
| 74 | std::vector<bool> m_optional; |
| 75 | }; |
| 76 | |
| 77 | void |
| 78 | validateRequest(const RepoCommandParameter& parameters); |
| 79 | |
Davide Pesavento | 7df36bd | 2023-10-28 19:32:43 -0400 | [diff] [blame] | 80 | protected: |
| 81 | ~RepoCommand() = default; |
| 82 | |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 83 | private: |
| 84 | virtual void |
| 85 | check(const RepoCommandParameter& parameters) const |
| 86 | { |
| 87 | } |
| 88 | |
Davide Pesavento | 7df36bd | 2023-10-28 19:32:43 -0400 | [diff] [blame] | 89 | protected: |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 90 | FieldValidator m_requestValidator; |
| 91 | }; |
| 92 | |
Davide Pesavento | 7df36bd | 2023-10-28 19:32:43 -0400 | [diff] [blame] | 93 | class InsertCommand final : public RepoCommand |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 94 | { |
| 95 | public: |
| 96 | InsertCommand(); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 97 | }; |
| 98 | |
Davide Pesavento | 7df36bd | 2023-10-28 19:32:43 -0400 | [diff] [blame] | 99 | class InsertCheckCommand final : public RepoCommand |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 100 | { |
| 101 | public: |
| 102 | InsertCheckCommand(); |
| 103 | }; |
| 104 | |
Davide Pesavento | 7df36bd | 2023-10-28 19:32:43 -0400 | [diff] [blame] | 105 | class DeleteCommand final : public RepoCommand |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 106 | { |
| 107 | public: |
| 108 | DeleteCommand(); |
| 109 | |
| 110 | private: |
| 111 | void |
Davide Pesavento | 7df36bd | 2023-10-28 19:32:43 -0400 | [diff] [blame] | 112 | check(const RepoCommandParameter& parameters) const final; |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | } // namespace repo |
| 116 | |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 117 | #endif // REPO_REPO_COMMAND_HPP |