LCOV - code coverage report
Current view: top level - shared_model/validators - answer.hpp (source / functions) Hit Total Coverage
Test: cleared_cor.info Lines: 14 16 87.5 %
Date: 2019-03-07 14:46:43 Functions: 15 15 100.0 %

          Line data    Source code
       1             : /**
       2             :  * Copyright Soramitsu Co., Ltd. All Rights Reserved.
       3             :  * SPDX-License-Identifier: Apache-2.0
       4             :  */
       5             : 
       6             : #ifndef IROHA_ANSWER_HPP
       7             : #define IROHA_ANSWER_HPP
       8             : 
       9             : #include <ciso646>
      10             : #include <map>
      11             : #include <vector>
      12             : 
      13             : #include <boost/range/numeric.hpp>
      14             : #include "utils/string_builder.hpp"
      15             : 
      16             : namespace shared_model {
      17             :   namespace validation {
      18             : 
      19             :     using ConcreteReasonType = std::string;
      20             :     using GroupedReasons = std::vector<ConcreteReasonType>;
      21             :     using ReasonsGroupName = std::string;
      22             :     using ReasonsGroupType = std::pair<ReasonsGroupName, GroupedReasons>;
      23             : 
      24             :     /**
      25             :      * Class which represents the answer to stateless validation: whether
      26             :      * validation is done right and if not it explains the reason
      27             :      */
      28             :     class Answer {
      29             :      public:
      30             :       operator bool() const {
      31       52333 :         return not reasons_map_.empty();
      32             :       }
      33             : 
      34             :       /**
      35             :        * @return string representation of errors
      36             :        */
      37             :       std::string reason() const {
      38         125 :         return boost::accumulate(
      39         125 :             reasons_map_,
      40         125 :             std::string{},
      41             :             [](auto &&acc, const auto &command_reasons) {
      42         138 :               acc += detail::PrettyStringBuilder()
      43         138 :                          .init(command_reasons.first)
      44         138 :                          .appendAll(command_reasons.second,
      45             :                                     [](auto &element) { return element; })
      46         138 :                          .finalize()
      47         138 :                   + "\n";
      48         138 :               return std::forward<decltype(acc)>(acc);
      49           0 :             });
      50           0 :       }
      51             : 
      52             :       /**
      53             :        * Check if any error has been recorded to the answer
      54             :        * @return true if there are any errors, false otherwise
      55             :        */
      56             :       bool hasErrors() {
      57        5695 :         return not reasons_map_.empty();
      58             :       }
      59             : 
      60             :       /**
      61             :        * Adds error to map
      62             :        * @param reasons
      63             :        */
      64             :       void addReason(ReasonsGroupType &&reasons) {
      65         191 :         reasons_map_.insert(std::move(reasons));
      66         191 :       }
      67             : 
      68             :       std::map<ReasonsGroupName, GroupedReasons> getReasonsMap() {
      69           2 :         return reasons_map_;
      70             :       };
      71             : 
      72             :      private:
      73             :       std::map<ReasonsGroupName, GroupedReasons> reasons_map_;
      74             :     };
      75             : 
      76             :   }  // namespace validation
      77             : }  // namespace shared_model
      78             : 
      79             : #endif  // IROHA_ANSWER_HPP

Generated by: LCOV version 1.13