LCOV - code coverage report
Current view: top level - shared_model/builders/protobuf/builder_templates - blocks_query_template.hpp (source / functions) Hit Total Coverage
Test: coverage_cleared.info Lines: 20 21 95.2 %
Date: 2018-12-05 17:11:35 Functions: 76 90 84.4 %

          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_PROTO_BLOCKS_QUERY_BUILDER_TEMPLATE_HPP
       7             : #define IROHA_PROTO_BLOCKS_QUERY_BUILDER_TEMPLATE_HPP
       8             : 
       9             : #include "backend/protobuf/queries/proto_blocks_query.hpp"
      10             : #include "builders/protobuf/unsigned_proto.hpp"
      11             : #include "interfaces/common_objects/types.hpp"
      12             : #include "interfaces/queries/blocks_query.hpp"
      13             : #include "interfaces/transaction.hpp"
      14             : #include "queries.pb.h"
      15             : #include "validators/default_validator.hpp"
      16             : 
      17             : namespace shared_model {
      18             :   namespace proto {
      19             : 
      20             :     /**
      21             :      * Template blocks query builder for creating new types of builders by
      22             :      * means of replacing template parameters
      23             :      * @tparam S -- field counter for checking that all required fields are
      24             :      * set
      25             :      * @tparam SV -- stateless validator called when build method is invoked
      26             :      * @tparam BT -- build type of built object returned by build method
      27             :      */
      28             :     template <int S = 0,
      29             :               typename SV = validation::DefaultUnsignedBlocksQueryValidator,
      30             :               typename BT = UnsignedWrapper<BlocksQuery>>
      31             :     class DEPRECATED TemplateBlocksQueryBuilder {
      32             :      private:
      33             :       template <int, typename, typename>
      34             :       friend class TemplateBlocksQueryBuilder;
      35             : 
      36             :       enum RequiredFields {
      37             :         CreatedTime,
      38             :         CreatorAccountId,
      39             :         QueryCounter,
      40             :         TOTAL
      41             :       };
      42             : 
      43             :       template <int s>
      44             :       using NextBuilder = TemplateBlocksQueryBuilder<S | (1 << s), SV, BT>;
      45             : 
      46             :       using ProtoBlocksQuery = iroha::protocol::BlocksQuery;
      47             : 
      48             :       template <int Sp>
      49             :       TemplateBlocksQueryBuilder(
      50             :           const TemplateBlocksQueryBuilder<Sp, SV, BT> &o)
      51           2 :           : query_(o.query_), stateless_validator_(o.stateless_validator_) {}
      52             : 
      53             :       /**
      54             :        * Make transformation on copied content
      55             :        * @tparam Transformation - callable type for changing the copy
      56             :        * @param t - transform function for proto object
      57             :        * @return new builder with updated state
      58             :        */
      59             :       template <int Fields, typename Transformation>
      60             :       auto transform(Transformation t) const {
      61           6 :         NextBuilder<Fields> copy = *this;
      62           6 :         t(copy.query_);
      63           6 :         return copy;
      64           6 :       }
      65             : 
      66             :      public:
      67             :       TemplateBlocksQueryBuilder(const SV &validator = SV())
      68           6 :           : stateless_validator_(validator) {}
      69             : 
      70             :       auto createdTime(interface::types::TimestampType created_time) const {
      71             :         return transform<CreatedTime>([&](auto &qry) {
      72           4 :           auto *meta = qry.mutable_meta();
      73           4 :           meta->set_created_time(created_time);
      74           4 :         });
      75             :       }
      76             : 
      77             :       auto creatorAccountId(
      78             :           const interface::types::AccountIdType &creator_account_id) const {
      79             :         return transform<CreatorAccountId>([&](auto &qry) {
      80           6 :           auto *meta = qry.mutable_meta();
      81           6 :           meta->set_creator_account_id(creator_account_id);
      82           6 :         });
      83             :       }
      84             : 
      85             :       auto queryCounter(interface::types::CounterType query_counter) const {
      86             :         return transform<QueryCounter>([&](auto &qry) {
      87           4 :           auto *meta = qry.mutable_meta();
      88           4 :           meta->set_query_counter(query_counter);
      89           4 :         });
      90             :       }
      91             : 
      92             :       auto build() const {
      93             :         static_assert(S == (1 << TOTAL) - 1, "Required fields are not set");
      94           6 :         auto result = BlocksQuery(iroha::protocol::BlocksQuery(query_));
      95           6 :         auto answer = stateless_validator_.validate(result);
      96           6 :         if (answer.hasErrors()) {
      97           0 :           throw std::invalid_argument(answer.reason());
      98             :         }
      99           6 :         return BT(std::move(result));
     100           6 :       }
     101             : 
     102             :       static const int total = RequiredFields::TOTAL;
     103             : 
     104             :      private:
     105             :       ProtoBlocksQuery query_;
     106             :       SV stateless_validator_;
     107             :     };
     108             :   }  // namespace proto
     109             : }  // namespace shared_model
     110             : 
     111             : #endif  // IROHA_PROTO_BLOCKS_QUERY_BUILDER_TEMPLATE_HPP

Generated by: LCOV version 1.13