LCOV - code coverage report
Current view: top level - irohad/model/converters/impl - json_transaction_factory.cpp (source / functions) Hit Total Coverage
Test: cleared_cor.info Lines: 39 42 92.9 %
Date: 2019-03-07 14:46:43 Functions: 6 6 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             : #include "model/converters/json_transaction_factory.hpp"
       7             : 
       8             : #include <algorithm>
       9             : #include "model/converters/json_common.hpp"
      10             : 
      11             : using namespace rapidjson;
      12             : 
      13             : namespace iroha {
      14             :   namespace model {
      15             :     namespace converters {
      16             : 
      17             :       Document JsonTransactionFactory::serialize(
      18             :           const Transaction &transaction) {
      19           2 :         Document document;
      20           2 :         auto &allocator = document.GetAllocator();
      21           2 :         document.SetObject();
      22             : 
      23           2 :         Value signatures;
      24           2 :         signatures.SetArray();
      25           3 :         for (const auto &signature : transaction.signatures) {
      26           1 :           signatures.PushBack(serializeSignature(signature, allocator),
      27           1 :                               allocator);
      28             :         }
      29           2 :         document.AddMember("signatures", signatures, allocator);
      30             : 
      31           2 :         document.AddMember("created_ts", transaction.created_ts, allocator);
      32           2 :         document.AddMember(
      33           2 :             "creator_account_id", transaction.creator_account_id, allocator);
      34           2 :         document.AddMember("quorum", transaction.quorum, allocator);
      35             : 
      36           2 :         Value commands;
      37           2 :         commands.SetArray();
      38           3 :         for (auto &&command : transaction.commands) {
      39           1 :           commands.PushBack(
      40           1 :               Document(&allocator)
      41           1 :                   .CopyFrom(factory_.serializeAbstractCommand(command),
      42           1 :                             allocator),
      43           1 :               allocator);
      44             :         }
      45             : 
      46           2 :         document.AddMember("commands", commands, allocator);
      47             : 
      48           2 :         return document;
      49           2 :       }
      50             : 
      51             :       boost::optional<Transaction> JsonTransactionFactory::deserialize(
      52             :           const Value &document) {
      53           3 :         auto des = makeFieldDeserializer(document);
      54             :         auto des_commands = [this](auto array) {
      55             :           auto acc_commands = [this](auto init, auto &x) {
      56             :             return init | [this, &x](auto commands) {
      57           2 :               return factory_.deserializeAbstractCommand(x) |
      58             :                   [&commands](auto command) {
      59           1 :                     commands.push_back(command);
      60           1 :                     return boost::make_optional(std::move(commands));
      61             :                   };
      62           0 :             };
      63             :           };
      64           2 :           return std::accumulate(
      65           2 :               array.begin(),
      66           2 :               array.end(),
      67           2 :               boost::make_optional(Transaction::CommandsType()),
      68           2 :               acc_commands);
      69           0 :         };
      70           3 :         return boost::make_optional(Transaction())
      71           3 :             | des.Uint64(&Transaction::created_ts, "created_ts")
      72           3 :             | des.String(&Transaction::creator_account_id, "creator_account_id")
      73           3 :             | des.Array(&Transaction::signatures, "signatures")
      74           3 :             | des.Uint(&Transaction::quorum, "quorum")
      75           3 :             | des.Array(&Transaction::commands, "commands", des_commands);
      76           0 :       }
      77             : 
      78             :     }  // namespace converters
      79             :   }    // namespace model
      80             : }  // namespace iroha

Generated by: LCOV version 1.13