LCOV - code coverage report
Current view: top level - irohad/model/converters/impl - json_query_factory.cpp (source / functions) Hit Total Coverage
Test: cleared_cor.info Lines: 147 164 89.6 %
Date: 2019-03-07 14:46:43 Functions: 27 28 96.4 %

          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_query_factory.hpp"
       7             : 
       8             : #include "logger/logger.hpp"
       9             : #include "model/queries/get_account.hpp"
      10             : #include "model/queries/get_account_assets.hpp"
      11             : #include "model/queries/get_account_detail.hpp"
      12             : #include "model/queries/get_asset_info.hpp"
      13             : #include "model/queries/get_roles.hpp"
      14             : #include "model/queries/get_signatories.hpp"
      15             : #include "model/queries/get_transactions.hpp"
      16             : 
      17             : using namespace rapidjson;
      18             : 
      19             : namespace iroha {
      20             :   namespace model {
      21             :     namespace converters {
      22             :       JsonQueryFactory::JsonQueryFactory(logger::LoggerPtr log)
      23          15 :           : log_{std::move(log)} {
      24         150 :         deserializers_ = {
      25          15 :             {"GetAccount", &JsonQueryFactory::deserializeGetAccount},
      26          15 :             {"GetAccountAssets",
      27          15 :              &JsonQueryFactory::deserializeGetAccountAssets},
      28          15 :             {"GetAccountDetail",
      29          15 :              &JsonQueryFactory::deserializeGetAccountDetail},
      30          15 :             {"GetAccountTransactions",
      31          15 :              &JsonQueryFactory::deserializeGetAccountTransactions},
      32          15 :             {"GetAccountAssetTransactions",
      33          15 :              &JsonQueryFactory::deserializeGetAccountAssetTransactions},
      34          15 :             {"GetTransactions", &JsonQueryFactory::deserializeGetTransactions},
      35          15 :             {"GetAccountSignatories",
      36          15 :              &JsonQueryFactory::deserializeGetSignatories},
      37          15 :             {"GetRoles", &JsonQueryFactory::deserializeGetRoles},
      38          15 :             {"GetRolePermissions",
      39          15 :              &JsonQueryFactory::deserializeGetRolePermissions},
      40          15 :             {"GetAssetInfo", &JsonQueryFactory::deserializeGetAssetInfo}};
      41             :         // Serializers
      42          15 :         serializers_ = {
      43          15 :             {typeid(GetAccount), &JsonQueryFactory::serializeGetAccount},
      44          15 :             {typeid(GetSignatories),
      45          15 :              &JsonQueryFactory::serializeGetSignatories},
      46          15 :             {typeid(GetAccountAssets),
      47          15 :              &JsonQueryFactory::serializeGetAccountAssets},
      48          15 :             {typeid(GetAccountDetail),
      49          15 :              &JsonQueryFactory::serializeGetAccountDetail},
      50          15 :             {typeid(GetAccountTransactions),
      51          15 :              &JsonQueryFactory::serializeGetAccountTransactions},
      52          15 :             {typeid(GetAccountAssetTransactions),
      53          15 :              &JsonQueryFactory::serializeGetAccountAssetTransactions},
      54          15 :             {typeid(GetTransactions),
      55          15 :              &JsonQueryFactory::serializeGetTransactions},
      56          15 :             {typeid(GetAssetInfo), &JsonQueryFactory::serializeGetAssetInfo},
      57          15 :             {typeid(GetRoles), &JsonQueryFactory::serializeGetRoles},
      58          15 :             {typeid(GetRolePermissions),
      59          15 :              &JsonQueryFactory::serializeGetRolePermissions}};
      60          15 :       }
      61             : 
      62             :       optional_ptr<Query> JsonQueryFactory::deserialize(
      63             :           const std::string &query_json) {
      64          19 :         return stringToJson(query_json) |
      65             :             [this](auto &&json) { return this->deserialize(json); };
      66           0 :       }
      67             : 
      68             :       optional_ptr<Query> JsonQueryFactory::deserialize(
      69             :           const rapidjson::Document &document) {
      70          18 :         auto des = makeFieldDeserializer(document);
      71          18 :         return des.String("query_type") | makeOptionalGet(deserializers_)
      72          18 :             | makeMethodInvoke(*this, document)
      73          18 :             | des.Uint64(&Query::created_ts, "created_ts")
      74          18 :             | des.String(&Query::creator_account_id, "creator_account_id")
      75          18 :             | des.Uint64(&Query::query_counter, "query_counter")
      76          18 :             | des.Object(&Query::signature, "signature") |
      77             :             [](auto query) { return boost::make_optional(std::move(query)); };
      78           0 :       }
      79             : 
      80             :       optional_ptr<Query> JsonQueryFactory::deserializeGetAccount(
      81             :           const Value &obj_query) {
      82           4 :         auto des = makeFieldDeserializer(obj_query);
      83           4 :         return make_optional_ptr<GetAccount>()
      84           4 :             | des.String(&GetAccount::account_id, "account_id") | toQuery;
      85           0 :       }
      86             : 
      87             :       optional_ptr<Query> JsonQueryFactory::deserializeGetSignatories(
      88             :           const Value &obj_query) {
      89           2 :         auto des = makeFieldDeserializer(obj_query);
      90           2 :         return make_optional_ptr<GetSignatories>()
      91           2 :             | des.String(&GetSignatories::account_id, "account_id") | toQuery;
      92           0 :       }
      93             : 
      94             :       optional_ptr<Query> JsonQueryFactory::deserializeGetAccountTransactions(
      95             :           const Value &obj_query) {
      96           2 :         auto des = makeFieldDeserializer(obj_query);
      97           2 :         return make_optional_ptr<GetAccountTransactions>()
      98           2 :             | des.String(&GetAccountTransactions::account_id, "account_id")
      99           2 :             | toQuery;
     100           0 :       }
     101             : 
     102             :       optional_ptr<Query>
     103             :       JsonQueryFactory::deserializeGetAccountAssetTransactions(
     104             :           const Value &obj_query) {
     105           1 :         auto des = makeFieldDeserializer(obj_query);
     106           1 :         return make_optional_ptr<GetAccountAssetTransactions>()
     107           1 :             | des.String(&GetAccountAssetTransactions::account_id, "account_id")
     108           1 :             | des.String(&GetAccountAssetTransactions::asset_id, "asset_id")
     109           1 :             | toQuery;
     110           0 :       }
     111             : 
     112             :       optional_ptr<Query> JsonQueryFactory::deserializeGetAccountDetail(
     113             :           const Value &obj_query) {
     114           1 :         auto des = makeFieldDeserializer(obj_query);
     115           1 :         return make_optional_ptr<GetAccountDetail>()
     116           1 :             | des.String(&GetAccountDetail::account_id, "account_id") | toQuery;
     117           0 :       }
     118             : 
     119             :       optional_ptr<Query> JsonQueryFactory::deserializeGetTransactions(
     120             :           const Value &obj_query) {
     121           2 :         auto des = makeFieldDeserializer(obj_query);
     122           2 :         return make_optional_ptr<GetTransactions>()
     123           2 :             | des.Array(&GetTransactions::tx_hashes, "tx_hashes") | toQuery;
     124           0 :       }
     125             : 
     126             :       optional_ptr<Query> JsonQueryFactory::deserializeGetAccountAssets(
     127             :           const Value &obj_query) {
     128           3 :         auto des = makeFieldDeserializer(obj_query);
     129           3 :         return make_optional_ptr<GetAccountAssets>()
     130           3 :             | des.String(&GetAccountAssets::account_id, "account_id") | toQuery;
     131           0 :       }
     132             : 
     133             :       optional_ptr<Query> JsonQueryFactory::deserializeGetAssetInfo(
     134             :           const rapidjson::Value &obj_query) {
     135           1 :         auto des = makeFieldDeserializer(obj_query);
     136           1 :         return make_optional_ptr<GetAssetInfo>()
     137           1 :             | des.String(&GetAssetInfo::asset_id, "asset_id") | toQuery;
     138           0 :       }
     139             : 
     140             :       optional_ptr<Query> JsonQueryFactory::deserializeGetRoles(
     141             :           const rapidjson::Value &obj_query) {
     142           1 :         return make_optional_ptr<GetRoles>() | toQuery;
     143           0 :       }
     144             : 
     145             :       optional_ptr<Query> JsonQueryFactory::deserializeGetRolePermissions(
     146             :           const rapidjson::Value &obj_query) {
     147           1 :         auto des = makeFieldDeserializer(obj_query);
     148           1 :         return make_optional_ptr<GetRolePermissions>()
     149           1 :             | des.String(&GetRolePermissions::role_id, "role_id") | toQuery;
     150           0 :       }
     151             : 
     152             :       // --- Serialization:
     153             : 
     154             :       std::string JsonQueryFactory::serialize(
     155             :           std::shared_ptr<const Query> model_query) {
     156          14 :         Document doc;
     157          14 :         auto &allocator = doc.GetAllocator();
     158          14 :         doc.SetObject();
     159          14 :         doc.AddMember(
     160          14 :             "creator_account_id", model_query->creator_account_id, allocator);
     161          14 :         doc.AddMember("query_counter", model_query->query_counter, allocator);
     162          14 :         doc.AddMember("created_ts", model_query->created_ts, allocator);
     163          14 :         Value signature;
     164          14 :         signature.SetObject();
     165          14 :         signature.CopyFrom(
     166          14 :             serializeSignature(model_query->signature, allocator), allocator);
     167             : 
     168          14 :         doc.AddMember("signature", signature, allocator);
     169             : 
     170          14 :         makeMethodInvoke(
     171          14 :             *this, doc, model_query)(serializers_.at(typeid(*model_query)));
     172          14 :         return jsonToString(doc);
     173          14 :       }
     174             : 
     175             :       void JsonQueryFactory::serializeGetAccount(
     176             :           Document &json_doc, std::shared_ptr<const Query> query) {
     177           2 :         auto &allocator = json_doc.GetAllocator();
     178           2 :         json_doc.AddMember("query_type", "GetAccount", allocator);
     179           2 :         auto get_account = std::static_pointer_cast<const GetAccount>(query);
     180           2 :         json_doc.AddMember("account_id", get_account->account_id, allocator);
     181           2 :       }
     182             : 
     183             :       void JsonQueryFactory::serializeGetAccountAssets(
     184             :           Document &json_doc, std::shared_ptr<const Query> query) {
     185           2 :         auto &allocator = json_doc.GetAllocator();
     186           2 :         json_doc.AddMember("query_type", "GetAccountAssets", allocator);
     187             :         auto casted_query =
     188           2 :             std::static_pointer_cast<const GetAccountAssets>(query);
     189           2 :         json_doc.AddMember("account_id", casted_query->account_id, allocator);
     190           2 :       }
     191             : 
     192             :       void JsonQueryFactory::serializeGetAccountDetail(
     193             :           Document &json_doc, std::shared_ptr<const Query> query) {
     194           0 :         auto &allocator = json_doc.GetAllocator();
     195           0 :         json_doc.AddMember("query_type", "GetAccountDetail", allocator);
     196             :         auto casted_query =
     197           0 :             std::static_pointer_cast<const GetAccountDetail>(query);
     198           0 :         json_doc.AddMember("account_id", casted_query->account_id, allocator);
     199           0 :       }
     200             : 
     201             :       void JsonQueryFactory::serializeGetSignatories(
     202             :           Document &json_doc, std::shared_ptr<const Query> query) {
     203           2 :         auto &allocator = json_doc.GetAllocator();
     204           2 :         json_doc.AddMember("query_type", "GetAccountSignatories", allocator);
     205             :         auto get_account =
     206           2 :             std::static_pointer_cast<const GetSignatories>(query);
     207           2 :         json_doc.AddMember("account_id", get_account->account_id, allocator);
     208           2 :       }
     209             : 
     210             :       void JsonQueryFactory::serializeGetAccountTransactions(
     211             :           Document &json_doc, std::shared_ptr<const Query> query) {
     212           2 :         auto &allocator = json_doc.GetAllocator();
     213           2 :         json_doc.AddMember("query_type", "GetAccountTransactions", allocator);
     214             :         auto get_account =
     215           2 :             std::static_pointer_cast<const GetAccountTransactions>(query);
     216           2 :         json_doc.AddMember("account_id", get_account->account_id, allocator);
     217           2 :       }
     218             : 
     219             :       void JsonQueryFactory::serializeGetAccountAssetTransactions(
     220             :           Document &json_doc, std::shared_ptr<const Query> query) {
     221           1 :         auto &allocator = json_doc.GetAllocator();
     222           1 :         json_doc.AddMember(
     223           1 :             "query_type", "GetAccountAssetTransactions", allocator);
     224             :         auto get_account_asset =
     225           1 :             std::static_pointer_cast<const GetAccountAssetTransactions>(query);
     226           1 :         json_doc.AddMember(
     227           1 :             "account_id", get_account_asset->account_id, allocator);
     228           1 :         json_doc.AddMember("asset_id", get_account_asset->asset_id, allocator);
     229           1 :       }
     230             : 
     231             :       void JsonQueryFactory::serializeGetTransactions(
     232             :           Document &json_doc, std::shared_ptr<const Query> query) {
     233           2 :         auto &allocator = json_doc.GetAllocator();
     234           2 :         json_doc.AddMember("query_type", "GetTransactions", allocator);
     235             :         auto get_transactions =
     236           2 :             std::static_pointer_cast<const GetTransactions>(query);
     237           2 :         Value json_tx_hashes;
     238           2 :         json_tx_hashes.SetArray();
     239           2 :         const auto &tx_hashes = get_transactions->tx_hashes;
     240           2 :         std::for_each(tx_hashes.begin(),
     241           2 :                       tx_hashes.end(),
     242             :                       [&json_tx_hashes, &allocator](auto tx_hash) {
     243           3 :                         Value json_tx_hash;
     244           3 :                         json_tx_hash.Set(tx_hash.to_hexstring(), allocator);
     245           3 :                         json_tx_hashes.PushBack(json_tx_hash, allocator);
     246           3 :                       });
     247           2 :         json_doc.AddMember("tx_hashes", json_tx_hashes, allocator);
     248           2 :       }
     249             : 
     250             :       void JsonQueryFactory::serializeGetAssetInfo(
     251             :           rapidjson::Document &json_doc, std::shared_ptr<const Query> query) {
     252           1 :         auto &allocator = json_doc.GetAllocator();
     253           1 :         json_doc.AddMember("query_type", "GetAssetInfo", allocator);
     254           1 :         auto cmd = std::static_pointer_cast<const GetAssetInfo>(query);
     255           1 :         json_doc.AddMember("asset_id", cmd->asset_id, allocator);
     256           1 :       }
     257             : 
     258             :       void JsonQueryFactory::serializeGetRolePermissions(
     259             :           rapidjson::Document &json_doc, std::shared_ptr<const Query> query) {
     260           1 :         auto &allocator = json_doc.GetAllocator();
     261           1 :         json_doc.AddMember("query_type", "GetRolePermissions", allocator);
     262           1 :         auto cmd = std::static_pointer_cast<const GetRolePermissions>(query);
     263           1 :         json_doc.AddMember("role_id", cmd->role_id, allocator);
     264           1 :       }
     265             : 
     266             :       void JsonQueryFactory::serializeGetRoles(
     267             :           rapidjson::Document &json_doc, std::shared_ptr<const Query> query) {
     268           1 :         auto &allocator = json_doc.GetAllocator();
     269           1 :         json_doc.AddMember("query_type", "GetRoles", allocator);
     270           1 :       }
     271             : 
     272             :     }  // namespace converters
     273             :   }    // namespace model
     274             : }  // namespace iroha

Generated by: LCOV version 1.13