Skip to content

Commit

Permalink
Refactoring:
Browse files Browse the repository at this point in the history
- Remove JSON-vector functions
- Add doc to serializeSignature
- Rename block deserializeField to deserializeObjectField
- makeMap -> makeOptionalGet, Invoker -> makeMethodInvoke
  • Loading branch information
lebdron committed Sep 13, 2017
1 parent 56775c6 commit 46a1d45
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 92 deletions.
3 changes: 2 additions & 1 deletion irohad/ametsuchi/impl/flat_file_block_query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ namespace iroha {
s.on_completed();
return;
}
auto document = model::converters::vectorToJson(bytes.value());
auto document =
model::converters::stringToJson(bytesToString(bytes.value()));
if (not document.has_value()) {
s.on_completed();
return;
Expand Down
4 changes: 2 additions & 2 deletions irohad/ametsuchi/impl/storage_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ namespace iroha {
auto storage = static_cast<MutableStorageImpl *>(storage_ptr.get());
for (const auto &block : storage->block_store_) {
block_store_->add(block.first,
model::converters::jsonToVector(
serializer_.serialize(block.second)));
stringToBytes(model::converters::jsonToString(
serializer_.serialize(block.second))));
}
storage->index_->exec();
storage->transaction_->exec("COMMIT;");
Expand Down
7 changes: 4 additions & 3 deletions irohad/model/converters/impl/json_command_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,14 +433,15 @@ namespace iroha {
// Abstract
Document JsonCommandFactory::serializeAbstractCommand(
std::shared_ptr<Command> command) {
return makeInvoker(*this, command)(serializers_.at(typeid(*command)));
return makeMethodInvoke(*this,
command)(serializers_.at(typeid(*command)));
}

optional_ptr<model::Command>
JsonCommandFactory::deserializeAbstractCommand(const Value &document) {
return makeFieldDeserializer(document).String("command_type")
| makeMap(deserializers_)
| makeInvoker(*this, document);
| makeOptionalGet(deserializers_)
| makeMethodInvoke(*this, document);
}
} // namespace converters
} // namespace model
Expand Down
9 changes: 0 additions & 9 deletions irohad/model/converters/impl/json_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,6 @@ namespace iroha {
document.Accept(writer);
return sb.GetString();
}

nonstd::optional<Document> vectorToJson(
const std::vector<uint8_t>& vector) {
return stringToJson(bytesToString(vector));
}

std::vector<uint8_t> jsonToVector(const Document& document) {
return stringToBytes(jsonToString(document));
}
} // namespace converters
} // namespace model
} // namespace iroha
8 changes: 4 additions & 4 deletions irohad/model/converters/impl/json_query_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ namespace iroha {
const rapidjson::Document &document) {
auto des = makeFieldDeserializer(document);
return des.String("query_type")
| makeMap(deserializers_)
| makeInvoker(*this, document)
| makeOptionalGet(deserializers_)
| makeMethodInvoke(*this, document)
| des.Uint64(&Query::created_ts, "created_ts")
| des.String(&Query::creator_account_id, "creator_account_id")
| des.Uint64(&Query::query_counter, "query_counter")
Expand Down Expand Up @@ -130,8 +130,8 @@ namespace iroha {

doc.AddMember("signature", signature, allocator);

makeInvoker(*this, doc,
model_query)(serializers_.at(typeid(*model_query)));
makeMethodInvoke(*this, doc,
model_query)(serializers_.at(typeid(*model_query)));
return jsonToString(doc);
}

Expand Down
89 changes: 16 additions & 73 deletions irohad/model/converters/json_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace iroha {
* nullopt otherwise
*/
template <typename K, typename V>
auto makeMap(std::unordered_map<K, V> map) {
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)) {
Expand All @@ -56,61 +56,19 @@ namespace iroha {
}

/**
* Functor for invoking class method by pointer to member
* @tparam T - class for method call
* @tparam Args - member function arguments types
* 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>
class Invoker {
public:
/**
* @param object - object of given class
* @param args - arguments of member function
*/
Invoker(T &object, Args &&... args) : object(object), args(args...) {}

/**
* Invoke function on saved object. Helper function to get
* index sequence
* @tparam F - function type to be called
* @tparam Is - index sequence of arguments from tuple
* @param f - function to be called
* @return result of function call
*/
template <typename F, std::size_t... Is>
auto operator()(F f) {
return apply(f, std::index_sequence_for<Args...>{});
}
private:
/**
* Invoke function on saved object
* @tparam F - function type to be called
* @tparam Is - index sequence of arguments from tuple
* @param f - function to be called
* @return result of function call
*/
template <typename F, std::size_t... Is>
auto apply(F f, std::index_sequence<Is...>) {
return (object.*f)(std::get<Is>(args)...);
}

// object for function call
T &object;
// arguments for function call
std::tuple<Args...> args;
};

/**
* Factory method for Invoker functor
* @tparam T - class type for method call
* @tparam Args - member function arguments types
* @param object - object of given class
* @param args - arguments of member function
* @return Invoker instance for given arguments
*/
template <typename T, typename... Args>
auto makeInvoker(T &object, Args &&... args) {
return Invoker<T, Args...>(object, std::forward<Args>(args)...);
auto makeMethodInvoke(T &object, Args &&... args) {
return [&](auto f) {
return (object.*f)(std::forward<Args>(args)...);
};
}

/**
Expand Down Expand Up @@ -204,7 +162,7 @@ namespace iroha {
*/
template <typename T, typename V, typename B, typename D,
typename Transform = Transform<T, V>>
nonstd::optional<B> deserializeField(B block, V B::*member,
nonstd::optional<B> deserializeObjectField(B block, V B::*member,
const D &document,
const std::string &field,
Transform transform = Transform()) {
Expand Down Expand Up @@ -236,7 +194,7 @@ namespace iroha {
*/
template <typename T, typename V, typename B, typename D,
typename Transform = Transform<T, V>>
optional_ptr<B> deserializeField(std::shared_ptr<B> block, V B::*member,
optional_ptr<B> deserializeObjectField(std::shared_ptr<B> block, V B::*member,
const D &document,
const std::string &field,
Transform transform = Transform()) {
Expand Down Expand Up @@ -280,7 +238,7 @@ namespace iroha {
auto deserialize(V B::*member, const std::string &field,
Transform transform = Transform()) {
return [this, member, field, transform](auto block) {
return deserializeField<T>(block, member, document, field,
return deserializeObjectField<T>(block, member, document, field,
transform);
};
}
Expand Down Expand Up @@ -455,7 +413,7 @@ namespace iroha {
* Serialize signature to JSON with given allocator
* @param signature - signature for serialization
* @param allocator - allocator for JSON value
* @return
* @return JSON value with signature
*/
rapidjson::Value serializeSignature(
const Signature &signature,
Expand All @@ -475,21 +433,6 @@ namespace iroha {
* @return pretty printed JSON document
*/
std::string jsonToString(const rapidjson::Document &document);

/**
* Try to parse JSON from vector
* @param vector - vector for parsing
* @return JSON document on success, nullopt otherwise
*/
nonstd::optional<rapidjson::Document> vectorToJson(
const std::vector<uint8_t> &vector);

/**
* Pretty print JSON document to vector
* @param document - document for printing
* @return pretty printed JSON document
*/
std::vector<uint8_t> jsonToVector(const rapidjson::Document &document);
} // namespace converters
} // namespace model
} // namespace iroha
Expand Down

0 comments on commit 46a1d45

Please sign in to comment.