Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Merge pull request #7377 from EOSIO/feature/7254-handle-variant-aliases
Browse files Browse the repository at this point in the history
Allow aliases of variants in ABI
  • Loading branch information
b1bart authored May 20, 2019
2 parents 42c5f7d + e5a8940 commit 7f4b299
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
1 change: 0 additions & 1 deletion libraries/chain/abi_serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ namespace eosio { namespace chain {
structs[st.name] = st;

for( const auto& td : abi.types ) {
EOS_ASSERT(_is_type(td.type, ctx), invalid_type_inside_abi, "invalid type ${type}", ("type",td.type));
EOS_ASSERT(!_is_type(td.new_type_name, ctx), duplicate_abi_type_def_exception, "type already exists", ("new_type_name",td.new_type_name));
typedefs[td.new_type_name] = td.type;
}
Expand Down
55 changes: 53 additions & 2 deletions unittests/abi_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1955,8 +1955,8 @@ BOOST_AUTO_TEST_CASE(abi_type_redefine)
}
)=====";

auto is_type_exception = [](fc::exception const & e) -> bool { return e.to_detail_string().find("invalid type") != std::string::npos; };
BOOST_CHECK_EXCEPTION( abi_serializer abis(fc::json::from_string(repeat_abi).as<abi_def>(), max_serialization_time), invalid_type_inside_abi, is_type_exception );
auto is_type_exception = [](fc::exception const & e) -> bool { return e.to_detail_string().find("Circular reference in type account_name") != std::string::npos; };
BOOST_CHECK_EXCEPTION( abi_serializer abis(fc::json::from_string(repeat_abi).as<abi_def>(), max_serialization_time), abi_circular_def_exception, is_type_exception );

} FC_LOG_AND_RETHROW() }

Expand Down Expand Up @@ -2304,6 +2304,57 @@ BOOST_AUTO_TEST_CASE(variants)
} FC_LOG_AND_RETHROW()
}

BOOST_AUTO_TEST_CASE(aliased_variants)
{
using eosio::testing::fc_exception_message_starts_with;

auto aliased_variant = R"({
"version": "eosio::abi/1.1",
"types": [
{ "new_type_name": "foo", "type": "foo_variant" }
],
"variants": [
{"name": "foo_variant", "types": ["int8", "string"]}
],
})";

try {
// round-trip abi through multiple formats
// json -> variant -> abi_def -> bin
auto bin = fc::raw::pack(fc::json::from_string(aliased_variant).as<abi_def>());
// bin -> abi_def -> variant -> abi_def
abi_serializer abis(variant(fc::raw::unpack<abi_def>(bin)).as<abi_def>(), max_serialization_time );

verify_round_trip_conversion(abis, "foo", R"(["int8",21])", "0015");
} FC_LOG_AND_RETHROW()
}

BOOST_AUTO_TEST_CASE(variant_of_aliases)
{
using eosio::testing::fc_exception_message_starts_with;

auto aliased_variant = R"({
"version": "eosio::abi/1.1",
"types": [
{ "new_type_name": "foo_0", "type": "int8" },
{ "new_type_name": "foo_1", "type": "string" }
],
"variants": [
{"name": "foo", "types": ["foo_0", "foo_1"]}
],
})";

try {
// round-trip abi through multiple formats
// json -> variant -> abi_def -> bin
auto bin = fc::raw::pack(fc::json::from_string(aliased_variant).as<abi_def>());
// bin -> abi_def -> variant -> abi_def
abi_serializer abis(variant(fc::raw::unpack<abi_def>(bin)).as<abi_def>(), max_serialization_time );

verify_round_trip_conversion(abis, "foo", R"(["foo_0",21])", "0015");
} FC_LOG_AND_RETHROW()
}

BOOST_AUTO_TEST_CASE(extend)
{
using eosio::testing::fc_exception_message_starts_with;
Expand Down

0 comments on commit 7f4b299

Please sign in to comment.