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_TRANSPORT_BUILDER_HPP
19 : #define IROHA_TRANSPORT_BUILDER_HPP
20 :
21 : #include "common/result.hpp"
22 :
23 : namespace shared_model {
24 : namespace proto {
25 :
26 : /**
27 : * Class for building any shared model objects from corresponding transport
28 : * representation (e.g. protobuf object)
29 : * @tparam T Build type
30 : * @tparam SV Stateless validator type
31 : */
32 : template <typename T, typename SV>
33 : class DEPRECATED TransportBuilder {
34 : public:
35 : TransportBuilder(const SV &validator = SV())
36 4 : : stateless_validator_(validator) {}
37 :
38 : /**
39 : * Builds result from transport object
40 : * @return value if transport object is valid and error message otherwise
41 : */
42 : iroha::expected::Result<T, std::string> build(
43 : typename T::TransportType transport) {
44 4 : auto result = T(transport);
45 4 : auto answer = stateless_validator_.validate(result);
46 4 : if (answer.hasErrors()) {
47 2 : return iroha::expected::makeError(answer.reason());
48 : }
49 2 : return iroha::expected::makeValue(T(std::move(transport)));
50 4 : }
51 :
52 : private:
53 : SV stateless_validator_;
54 : };
55 :
56 : } // namespace proto
57 : } // namespace shared_model
58 : #endif // IROHA_TRANSPORT_BUILDER_HPP
|