LCOV - code coverage report
Current view: top level - shared_model/converters/protobuf - json_proto_converter.hpp (source / functions) Hit Total Coverage
Test: coverage_cleared.info Lines: 15 15 100.0 %
Date: 2018-12-05 17:11:35 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /**
       2             :  * Copyright Soramitsu Co., Ltd. 2018 All Rights Reserved.
       3             :  * http://soramitsu.co.jp
       4             :  *
       5             :  * Licensed under the Apache License, Version 2.0 (the "License");
       6             :  * you may not use this file except in compliance with the License.
       7             :  * You may obtain a copy of the License at
       8             :  *
       9             :  *        http://www.apache.org/licenses/LICENSE-2.0
      10             :  *
      11             :  * Unless required by applicable law or agreed to in writing, software
      12             :  * distributed under the License is distributed on an "AS IS" BASIS,
      13             :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      14             :  * See the License for the specific language governing permissions and
      15             :  * limitations under the License.
      16             :  */
      17             : 
      18             : #ifndef IROHA_JSON_PROTO_CONVERTER_HPP
      19             : #define IROHA_JSON_PROTO_CONVERTER_HPP
      20             : 
      21             : #include <google/protobuf/util/json_util.h>
      22             : #include <string>
      23             : #include "backend/protobuf/block.hpp"
      24             : #include "commands.pb.h"
      25             : 
      26             : namespace shared_model {
      27             :   namespace converters {
      28             :     namespace protobuf {
      29             : 
      30             :       /**
      31             :        * Converts protobuf model object into json string
      32             :        * @tparam T is the type of converting message
      33             :        * @param message is the message to be converted
      34             :        * @return json string
      35             :        */
      36             :       template <typename T>
      37             :       std::string modelToJson(const T &message) {
      38          46 :         std::string result;
      39          46 :         google::protobuf::util::MessageToJsonString(message.getTransport(),
      40             :                                                     &result);
      41          46 :         return result;
      42          46 :       }
      43             : 
      44             :       /**
      45             :        * Converts json string into arbitrary protobuf object
      46             :        * @tparam T type of model which json converts to
      47             :        * @param json is the json string
      48             :        * @return optional of protobuf object which contains value if json
      49             :        * conversion was successful and none otherwise
      50             :        */
      51             :       template <typename T>
      52             :       boost::optional<T> jsonToProto(std::string json) {
      53           3 :         T result;
      54             :         auto status =
      55           3 :             google::protobuf::util::JsonStringToMessage(json, &result);
      56           3 :         if (status.ok()) {
      57           2 :           return result;
      58             :         }
      59           1 :         return boost::none;
      60           3 :       }
      61             : 
      62             :       /**
      63             :        * Converts json into arbitrary transaction shared model object
      64             :        * @tparam T type of shared model object converted from json
      65             :        * @param json is the json string containing protobuf object
      66             :        * @return optional of shared model object, containing the
      67             :        * object if conversion was successful and none otherwise
      68             :        */
      69             :       template <typename T>
      70             :       boost::optional<T> jsonToModel(std::string json) {
      71           2 :         auto tx = jsonToProto<typename T::TransportType>(json);
      72           2 :         if (tx) {
      73           1 :           return T(std::move(tx.value()));
      74             :         }
      75           1 :         return boost::none;
      76           2 :       }
      77             : 
      78             :     }  // namespace protobuf
      79             :   }    // namespace converters
      80             : }  // namespace shared_model
      81             : 
      82             : #endif  // IROHA_JSON_PROTO_CONVERTER_HPP

Generated by: LCOV version 1.13