Skip to content

Commit

Permalink
Review fixes:
Browse files Browse the repository at this point in the history
- Moved general methods to common/types.hpp
- Updated docs and comments, added usage examples
- Create variables for complex lambdas, remove unnecessary captures
  • Loading branch information
lebdron committed Sep 13, 2017
1 parent 2ea30f4 commit f509d83
Show file tree
Hide file tree
Showing 11 changed files with 178 additions and 209 deletions.
15 changes: 8 additions & 7 deletions iroha-cli/impl/keys_manager_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
* limitations under the License.
*/

#include "impl/keys_manager_impl.hpp"

#include <utility>
#include "keys_manager_impl.hpp"
#include <fstream>

using iroha::operator|;

namespace iroha_cli {
/**
* Return function which will try to deserialize specified value to specified
Expand All @@ -30,13 +33,11 @@ namespace iroha_cli {
* @return keypair on success, otherwise nullopt
*/
template<typename T, typename V>
auto deserializeKeypairField(T iroha::ed25519::keypair_t::*field, V value) {
return [field, value](auto keypair) {
auto deserializeKeypairField(T iroha::ed25519::keypair_t::*field,
const V &value) {
return [=](auto keypair) mutable {
return iroha::hexstringToArray<T::size()>(value)
| [&](auto value) {
keypair.*field = value;
return nonstd::make_optional(keypair);
};
| iroha::assignObjectField(keypair, field);
};
}

Expand Down
34 changes: 17 additions & 17 deletions irohad/model/converters/impl/json_block_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ namespace iroha {
nonstd::optional<Block> JsonBlockFactory::deserialize(
const Document &document) {
auto des = makeFieldDeserializer(document);
auto des_transactions = [this](auto array) {
auto acc_transactions = [this](auto init, auto &x) {
return init
| [this, &x](auto transactions) {
return factory_.deserialize(x)
| [&transactions](auto transaction) {
transactions.push_back(transaction);
return nonstd::make_optional(transactions);
};
};
};
return std::accumulate(
array.begin(), array.end(),
nonstd::make_optional<Block::TransactionsType>(),
acc_transactions);
};
return nonstd::make_optional<model::Block>()
| des.Uint64(&Block::created_ts, "created_ts")
| des.Uint64(&Block::height, "height")
Expand All @@ -73,23 +89,7 @@ namespace iroha {
| des.String(&Block::prev_hash, "prev_hash")
| des.String(&Block::merkle_root, "merkle_root")
| des.Array(&Block::sigs, "signatures")
| des.Array(&Block::transactions, "transactions", [this](
auto array) {
return std::accumulate(
array.begin(), array.end(),
nonstd::make_optional<Block::TransactionsType>(),
[this](auto init, auto &x) {
return init
| [this, &x](auto transactions) {
return factory_.deserialize(x)
| [&transactions](auto transaction) {
transactions.push_back(transaction);
return nonstd::make_optional(
transactions);
};
};
});
});
| des.Array(&Block::transactions, "transactions", des_transactions);
}

} // namespace converters
Expand Down
2 changes: 1 addition & 1 deletion irohad/model/converters/impl/json_query_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ namespace iroha {
| des.String(&Query::creator_account_id, "creator_account_id")
| des.Uint64(&Query::query_counter, "query_counter")
| des.Object(&Query::signature, "signature")
| [this, &document](auto query) {
| [this](auto query) {
query->query_hash = hash_provider_.get_hash(query);
return nonstd::make_optional(query);
};
Expand Down
35 changes: 17 additions & 18 deletions irohad/model/converters/impl/json_transaction_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,28 @@ namespace iroha {
nonstd::optional<Transaction> JsonTransactionFactory::deserialize(
const Value &document) {
auto des = makeFieldDeserializer(document);
auto des_commands = [this](auto array) {
auto acc_commands = [this](auto init, auto &x) {
return init
| [this, &x](auto commands) {
return factory_.deserializeAbstractCommand(x)
| [&commands](auto command) {
commands.push_back(command);
return nonstd::make_optional(commands);
};
};
};
return std::accumulate(
array.begin(), array.end(),
nonstd::make_optional<Transaction::CommandsType>(), acc_commands);
};
return nonstd::make_optional<Transaction>()
| des.Uint64(&Transaction::created_ts, "created_ts")
| des.String(&Transaction::creator_account_id, "creator_account_id")
| des.Uint64(&Transaction::tx_counter, "tx_counter")
| des.Array(&Transaction::signatures, "signatures")
| des.Array(&Transaction::commands, "commands",
[this](auto array) {
return std::accumulate(
array.begin(), array.end(),
nonstd::make_optional<Transaction::CommandsType>(),
[this](auto init, auto &x) {
return init
| [this, &x](auto commands) {
return factory_.deserializeAbstractCommand(x)
| [&commands](auto command) {
commands.push_back(command);
return nonstd::make_optional(
commands);
};
};
});
})
| [this, &document](auto transaction) {
| des.Array(&Transaction::commands, "commands", des_commands)
| [this](auto transaction) {
transaction.tx_hash = hash_provider_.get_hash(transaction);
return nonstd::make_optional(transaction);
};
Expand Down
97 changes: 15 additions & 82 deletions irohad/model/converters/json_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <string>
#include <unordered_map>

// Enable std::string support in rapidjson
#define RAPIDJSON_HAS_STDSTRING 1
#include <rapidjson/document.h>
#include <rapidjson/prettywriter.h>
Expand All @@ -36,41 +37,6 @@
namespace iroha {
namespace model {
namespace converters {
/**
* Create map get function for value retrieval by key
* @tparam K - map key type
* @tparam V - map value type
* @param map - map for value retrieval
* @return function which takes key, returns value if key exists,
* nullopt otherwise
*/
template <typename K, typename V>
auto makeOptionalGet(std::unordered_map<K, V> map) {
return [&map](auto key) -> nonstd::optional<V> {
auto it = map.find(key);
if (it != std::end(map)) {
return it->second;
}
return nonstd::nullopt;
};
}

/**
* Return function which invokes class method by pointer to member with
* provided arguments
* @tparam T - provided class type
* @tparam Args - provided arguments types
* @param object - class object
* @param args - function arguments
* @return described function
*/
template <typename T, typename... Args>
auto makeMethodInvoke(T &object, Args &&... args) {
return [&](auto f) {
return (object.*f)(std::forward<Args>(args)...);
};
}

/**
* Convert functor which specifies output type
* @tparam V - output type
Expand Down Expand Up @@ -127,42 +93,9 @@ namespace iroha {
explicit FieldDeserializer(const D &document) : document(document) {}

/**
* Assign the value to the block member
* @tparam V - block member type
* @tparam B - block type
* @param block - block value for member assignment
* @param member - pointer to member in block
* @return block with deserialized member on success, nullopt otherwise
*/
template <typename V, typename B>
auto assignObjectField(B block, V B::*member) {
return [=](auto transformed) mutable {
block.*member = transformed;
return nonstd::make_optional(block);
};
}

/**
* Assign the value to the block member. Block is wrapped in shared
* pointer
* @tparam V - block member type
* @tparam B - block type
* @param block - block value for member assignment
* @param member - pointer to member in block
* @return block with deserialized member on success, nullopt otherwise
*/
template <typename V, typename B>
auto assignObjectField(std::shared_ptr<B> block, V B::*member) {
return [=](auto transformed) mutable {
(*block).*member = transformed;
return nonstd::make_optional(block);
};
}

/**
* Create function, which will deserialize document field with given
* verification and getter, transform the value to required type, and
* assign it to block member
* Create function, which deserializes document field,
* transforms the value to required type, and
* assigns it to block member
* @tparam T - getter return type
* @tparam V - block member type
* @tparam B - block type
Expand All @@ -180,7 +113,7 @@ namespace iroha {
return [this, member, field, transform](auto block) {
return deserializeField<T>(document, field)
| transform
| this->assignObjectField(block, member);
| assignObjectField(block, member);
};
}

Expand Down Expand Up @@ -308,19 +241,19 @@ namespace iroha {
struct Convert<Block::SignaturesType> {
template <typename T>
auto operator()(T &&x) {
return std::accumulate(
x.begin(), x.end(),
nonstd::make_optional<Block::SignaturesType>(),
[](auto init, auto &x) {
return init
| [&x](auto signatures) {
return Convert<Signature>()(x)
| [&signatures](auto signature) {
auto acc_signatures = [](auto init, auto &x) {
return init
| [&x](auto signatures) {
return Convert<Signature>()(x)
| [&signatures](auto signature) {
signatures.push_back(signature);
return nonstd::make_optional(signatures);
};
};
});
};
};
return std::accumulate(x.begin(), x.end(),
nonstd::make_optional<Block::SignaturesType>(),
acc_signatures);
}
};

Expand Down
57 changes: 0 additions & 57 deletions libs/common.hpp

This file was deleted.

4 changes: 2 additions & 2 deletions libs/common/byteutils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace iroha {

/**
* Create blob_t from string of specified size
* @tparam size - expected size of string
* @tparam size - size of blob_t, expected size of string
* @param s - string to convert
* @return blob, if conversion was successful, otherwise nullopt
*/
Expand All @@ -47,7 +47,7 @@ namespace iroha {
}

/**
* Try to transform string to array of given size
* Convert hexstring to array of given size
* @tparam size - output array size
* @param string - input string for transform
* @return array of given size if size matches, nullopt otherwise
Expand Down
Loading

0 comments on commit f509d83

Please sign in to comment.