Line data Source code
1 : /**
2 : * Copyright Soramitsu Co., Ltd. 2017 All Rights Reserved.
3 : * http://soramitsu.co.jp
4 : *
5 : * Licensed under the Apache License, Version 2.0 (the "License");
6 : * you may not use this file except in compliance with the License.
7 : * You may obtain a copy of the License at
8 : *
9 : * http://www.apache.org/licenses/LICENSE-2.0
10 : *
11 : * Unless required by applicable law or agreed to in writing, software
12 : * distributed under the License is distributed on an "AS IS" BASIS,
13 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 : * See the License for the specific language governing permissions and
15 : * limitations under the License.
16 : */
17 :
18 : #ifndef IROHA_ANSWER_HPP
19 : #define IROHA_ANSWER_HPP
20 :
21 : #include <ciso646>
22 : #include <map>
23 : #include <vector>
24 :
25 : #include <boost/range/numeric.hpp>
26 : #include "utils/string_builder.hpp"
27 :
28 : namespace shared_model {
29 : namespace validation {
30 :
31 : using ConcreteReasonType = std::string;
32 : using GroupedReasons = std::vector<ConcreteReasonType>;
33 : using ReasonsGroupName = std::string;
34 : using ReasonsGroupType = std::pair<ReasonsGroupName, GroupedReasons>;
35 :
36 : /**
37 : * Class which represents the answer to stateless validation: whether
38 : * validation is done right and if not it explains the reason
39 : */
40 : class Answer {
41 : public:
42 : operator bool() const {
43 14790 : return not reasons_map_.empty();
44 : }
45 :
46 : /**
47 : * @return string representation of errors
48 : */
49 : std::string reason() const {
50 120 : return boost::accumulate(
51 120 : reasons_map_,
52 120 : std::string{},
53 : [](auto &&acc, const auto &command_reasons) {
54 133 : acc += detail::PrettyStringBuilder()
55 133 : .init(command_reasons.first)
56 133 : .appendAll(command_reasons.second,
57 : [](auto &element) { return element; })
58 133 : .finalize()
59 133 : + "\n";
60 133 : return std::forward<decltype(acc)>(acc);
61 0 : });
62 0 : }
63 :
64 : /**
65 : * Check if any error has been recorded to the answer
66 : * @return true if there are any errors, false otherwise
67 : */
68 : bool hasErrors() {
69 10680 : return not reasons_map_.empty();
70 : }
71 :
72 : /**
73 : * Adds error to map
74 : * @param reasons
75 : */
76 : void addReason(ReasonsGroupType &&reasons) {
77 179 : reasons_map_.insert(std::move(reasons));
78 179 : }
79 :
80 : std::map<ReasonsGroupName, GroupedReasons> getReasonsMap() {
81 2 : return reasons_map_;
82 : };
83 :
84 : private:
85 : std::map<ReasonsGroupName, GroupedReasons> reasons_map_;
86 : };
87 :
88 : } // namespace validation
89 : } // namespace shared_model
90 :
91 : #endif // IROHA_ANSWER_HPP
|