LCOV - code coverage report
Current view: top level - shared_model/converters/protobuf - json_proto_converter.hpp (source / functions) Hit Total Coverage
Test: cleared_cor.info Lines: 15 15 100.0 %
Date: 2019-03-07 14:46:43 Functions: 7 7 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             : #ifndef IROHA_JSON_PROTO_CONVERTER_HPP
       7             : #define IROHA_JSON_PROTO_CONVERTER_HPP
       8             : 
       9             : #include <google/protobuf/util/json_util.h>
      10             : #include <string>
      11             : #include "backend/protobuf/block.hpp"
      12             : #include "commands.pb.h"
      13             : 
      14             : namespace shared_model {
      15             :   namespace converters {
      16             :     namespace protobuf {
      17             : 
      18             :       /**
      19             :        * Converts protobuf model object into json string
      20             :        * @tparam T is the type of converting message
      21             :        * @param message is the message to be converted
      22             :        * @return json string
      23             :        */
      24             :       template <typename T>
      25             :       std::string modelToJson(const T &message) {
      26           1 :         std::string result;
      27           1 :         google::protobuf::util::MessageToJsonString(message.getTransport(),
      28             :                                                     &result);
      29           1 :         return result;
      30           1 :       }
      31             : 
      32             :       /**
      33             :        * Converts json string into arbitrary protobuf object
      34             :        * @tparam T type of model which json converts to
      35             :        * @param json is the json string
      36             :        * @return optional of protobuf object which contains value if json
      37             :        * conversion was successful and none otherwise
      38             :        */
      39             :       template <typename T>
      40             :       boost::optional<T> jsonToProto(std::string json) {
      41           3 :         T result;
      42             :         auto status =
      43           3 :             google::protobuf::util::JsonStringToMessage(json, &result);
      44           3 :         if (status.ok()) {
      45           2 :           return result;
      46             :         }
      47           1 :         return boost::none;
      48           3 :       }
      49             : 
      50             :       /**
      51             :        * Converts json into arbitrary transaction shared model object
      52             :        * @tparam T type of shared model object converted from json
      53             :        * @param json is the json string containing protobuf object
      54             :        * @return optional of shared model object, containing the
      55             :        * object if conversion was successful and none otherwise
      56             :        */
      57             :       template <typename T>
      58             :       boost::optional<T> jsonToModel(std::string json) {
      59           2 :         auto tx = jsonToProto<typename T::TransportType>(json);
      60           2 :         if (tx) {
      61           1 :           return T(std::move(tx.value()));
      62             :         }
      63           1 :         return boost::none;
      64           2 :       }
      65             : 
      66             :     }  // namespace protobuf
      67             :   }    // namespace converters
      68             : }  // namespace shared_model
      69             : 
      70             : #endif  // IROHA_JSON_PROTO_CONVERTER_HPP

Generated by: LCOV version 1.13