Skip to content

Commit

Permalink
Merge branch 'feature/shared_model_common_builders' into develop
Browse files Browse the repository at this point in the history
Signed-off-by: Nikita Alekseev <[email protected]>
  • Loading branch information
nickaleks committed Feb 22, 2018
2 parents b7d8b60 + d634732 commit 079b33f
Show file tree
Hide file tree
Showing 31 changed files with 2,018 additions and 0 deletions.
1 change: 1 addition & 0 deletions shared_model/backend/protobuf/common_objects/asset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define IROHA_SHARED_MODEL_PROTO_ASSET_HPP

#include "backend/protobuf/common_objects/trivial_proto.hpp"
#include "backend/protobuf/util.hpp"
#include "interfaces/common_objects/asset.hpp"
#include "responses.pb.h"
#include "utils/lazy_initializer.hpp"
Expand Down
81 changes: 81 additions & 0 deletions shared_model/builders/common_objects/account_asset_builder.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* Copyright Soramitsu Co., Ltd. 2018 All Rights Reserved.
* http://soramitsu.co.jp
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef IROHA_ACCOUNT_ASSET_BUILDER_HPP
#define IROHA_ACCOUNT_ASSET_BUILDER_HPP

#include "builders/common_objects/common.hpp"
#include "interfaces/common_objects/account_asset.hpp"

// TODO: 14.02.2018 nickaleks Add check for uninitialized fields IR-972

namespace shared_model {
namespace builder {
/**
* AccountAssetBuilder is a class, used for construction of AccountAsset
* objects
* @tparam BuilderImpl is a type, which defines builder for implementation
* of shared_model. Since we return abstract classes, it is necessary for
* them to be instantiated with some concrete implementation
* @tparam Validator is a type, whose responsibility is
* to perform stateless validation on model fields
*/
template <typename BuilderImpl, typename Validator>
class AccountAssetBuilder
: public CommonObjectBuilder<interface::AccountAsset,
BuilderImpl,
Validator> {
public:
AccountAssetBuilder accountId(
const interface::types::AccountIdType &account_id) {
AccountAssetBuilder copy(*this);
copy.builder_ = this->builder_.accountId(account_id);
return copy;
}

AccountAssetBuilder assetId(
const interface::types::AssetIdType &asset_id) {
AccountAssetBuilder copy(*this);
copy.builder_ = this->builder_.assetId(asset_id);
return copy;
}

AccountAssetBuilder balance(const interface::Amount &amount) {
AccountAssetBuilder copy(*this);
copy.builder_ = this->builder_.balance(amount);
return copy;
}

protected:
virtual std::string builderName() const override {
return "Account Asset Builder";
}

virtual validation::ReasonsGroupType validate(
const interface::AccountAsset &object) override {
validation::ReasonsGroupType reasons;
this->validator_.validateAccountId(reasons, object.accountId());
this->validator_.validateAssetId(reasons, object.assetId());
// Do not validate balance, since its amount can be 0, which is
// forbidden by validation

return reasons;
}
};
} // namespace builder
} // namespace shared_model
#endif // IROHA_ACCOUNT_ASSET_BUILDER_HPP
86 changes: 86 additions & 0 deletions shared_model/builders/common_objects/account_builder.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* Copyright Soramitsu Co., Ltd. 2018 All Rights Reserved.
* http://soramitsu.co.jp
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef IROHA_ACCOUNT_BUILDER_HPP
#define IROHA_ACCOUNT_BUILDER_HPP

#include "builders/common_objects/common.hpp"
#include "interfaces/common_objects/account.hpp"

// TODO: 14.02.2018 nickaleks Add check for uninitialized fields IR-972

namespace shared_model {
namespace builder {

/**
* AccountBuilder is a class, used for construction of Account objects
* @tparam BuilderImpl is a type, which defines builder for implementation
* of shared_model. Since we return abstract classes, it is necessary for
* them to be instantiated with some concrete implementation
* @tparam Validator is a type, whose responsibility is
* to perform stateless validation on model fields
*/
template <typename BuilderImpl, typename Validator>
class AccountBuilder : public CommonObjectBuilder<interface::Account,
BuilderImpl,
Validator> {
public:
AccountBuilder accountId(
const interface::types::AccountIdType &account_id) {
AccountBuilder copy(*this);
copy.builder_ = this->builder_.accountId(account_id);
return copy;
}

AccountBuilder domainId(
const interface::types::DomainIdType &domain_id) {
AccountBuilder copy(*this);
copy.builder_ = this->builder_.domainId(domain_id);
return copy;
}

AccountBuilder quorum(const interface::types::QuorumType &quorum) {
AccountBuilder copy(*this);
copy.builder_ = this->builder_.quorum(quorum);
return copy;
}

AccountBuilder jsonData(const interface::types::JsonType &json_data) {
AccountBuilder copy(*this);
copy.builder_ = this->builder_.jsonData(json_data);
return copy;
}

protected:
virtual std::string builderName() const override {
return "Account Builder";
}

virtual validation::ReasonsGroupType validate(
const interface::Account &object) override {
validation::ReasonsGroupType reasons;
this->validator_.validateAccountId(reasons, object.accountId());
this->validator_.validateDomainId(reasons, object.domainId());
this->validator_.validateQuorum(reasons, object.quorum());

return reasons;
}
};
} // namespace builder
} // namespace shared_model

#endif // IROHA_ACCOUNT_BUILDER_HPP
71 changes: 71 additions & 0 deletions shared_model/builders/common_objects/amount_builder.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* Copyright Soramitsu Co., Ltd. 2018 All Rights Reserved.
* http://soramitsu.co.jp
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef IROHA_AMOUNT_BUILDER_HPP
#define IROHA_AMOUNT_BUILDER_HPP

#include "builders/common_objects/common.hpp"
#include "interfaces/common_objects/amount.hpp"

// TODO: 14.02.2018 nickaleks Add check for uninitialized fields IR-972

namespace shared_model {
namespace builder {

/**
* AmountBuilder is a class, used for construction of Amount objects
* @tparam BuilderImpl is a type, which defines builder for implementation
* of shared_model. Since we return abstract classes, it is necessary for
* them to be instantiated with some concrete implementation
* @tparam Validator is a type, whose responsibility is
* to perform stateless validation on model fields
*/
template <typename BuilderImpl, typename Validator>
class AmountBuilder : public CommonObjectBuilder<interface::Amount,
BuilderImpl,
Validator> {
public:
AmountBuilder intValue(const boost::multiprecision::uint256_t &value) {
AmountBuilder copy(*this);
copy.builder_ = this->builder_.intValue(value);
return copy;
}

AmountBuilder precision(
const interface::types::PrecisionType &precision) {
AmountBuilder copy(*this);
copy.builder_ = this->builder_.precision(precision);
return copy;
}

protected:
virtual std::string builderName() const override {
return "Amount Builder";
}

virtual validation::ReasonsGroupType validate(
const interface::Amount &object) override {
validation::ReasonsGroupType reasons;
this->validator_.validateAmount(reasons, object);

return reasons;
}
};
} // namespace builder
} // namespace shared_model

#endif // IROHA_AMOUNT_BUILDER_HPP
78 changes: 78 additions & 0 deletions shared_model/builders/common_objects/asset_builder.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* Copyright Soramitsu Co., Ltd. 2018 All Rights Reserved.
* http://soramitsu.co.jp
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef IROHA_ASSET_BUILDER_HPP
#define IROHA_ASSET_BUILDER_HPP

#include "builders/common_objects/common.hpp"
#include "interfaces/common_objects/asset.hpp"
#include "interfaces/common_objects/types.hpp"

// TODO: 14.02.2018 nickaleks Add check for uninitialized fields IR-972

namespace shared_model {
namespace builder {

/**
* AssetBuilder is a class, used for construction of Asset objects
* @tparam BuilderImpl is a type, which defines builder for implementation
* of shared_model. Since we return abstract classes, it is necessary for
* them to be instantiated with some concrete implementation
* @tparam Validator is a type, whose responsibility is
* to perform stateless validation on model fields
*/
template <typename BuilderImpl, typename Validator>
class AssetBuilder
: public CommonObjectBuilder<interface::Asset, BuilderImpl, Validator> {
public:
AssetBuilder assetId(const interface::types::AccountIdType &asset_id) {
AssetBuilder copy(*this);
copy.builder_ = this->builder_.assetId(asset_id);
return copy;
}

AssetBuilder domainId(const interface::types::DomainIdType &domain_id) {
AssetBuilder copy(*this);
copy.builder_ = this->builder_.domainId(domain_id);
return copy;
}

AssetBuilder precision(
const interface::types::PrecisionType &precision) {
AssetBuilder copy(*this);
copy.builder_ = this->builder_.precision(precision);
return copy;
}

protected:
virtual std::string builderName() const override {
return "Asset Builder";
}

virtual validation::ReasonsGroupType validate(const interface::Asset &object) override {
validation::ReasonsGroupType reasons;
this->validator_.validateAssetId(reasons, object.assetId());
this->validator_.validateDomainId(reasons, object.domainId());
this->validator_.validatePrecision(reasons, object.precision());

return reasons;
}
};
} // namespace builder
} // namespace shared_model

#endif // IROHA_ASSET_BUILDER_HPP
Loading

0 comments on commit 079b33f

Please sign in to comment.