Structure type json using "components".

This commit is contained in:
chriseth 2017-06-30 10:22:35 +02:00 committed by Alex Beregszaszi
parent 36a90289e6
commit 7e1b9c1652
2 changed files with 151 additions and 114 deletions

View File

@ -136,25 +136,25 @@ Json::Value ABI::formatType(string const& _name, Type const& _type, bool _forLib
suffix = string("[") + arrayType->length().str() + "]"; suffix = string("[") + arrayType->length().str() + "]";
solAssert(arrayType->baseType(), ""); solAssert(arrayType->baseType(), "");
Json::Value subtype = formatType("", *arrayType->baseType(), _forLibrary); Json::Value subtype = formatType("", *arrayType->baseType(), _forLibrary);
if (subtype["type"].isString() && !subtype.isMember("subtype")) if (subtype.isMember("components"))
ret["type"] = subtype["type"].asString() + suffix;
else
{ {
ret["type"] = suffix; ret["type"] = subtype["type"].asString() + suffix;
solAssert(!subtype.isMember("subtype"), ""); ret["components"] = subtype["components"];
ret["subtype"] = subtype["type"];
} }
else
ret["type"] = subtype["type"].asString() + suffix;
} }
} }
else if (StructType const* structType = dynamic_cast<StructType const*>(&_type)) else if (StructType const* structType = dynamic_cast<StructType const*>(&_type))
{ {
ret["type"] = Json::arrayValue; ret["type"] = string();
ret["components"] = Json::arrayValue;
for (auto const& member: structType->members(nullptr)) for (auto const& member: structType->members(nullptr))
{ {
solAssert(member.type, ""); solAssert(member.type, "");
auto t = member.type->interfaceType(_forLibrary); auto t = member.type->interfaceType(_forLibrary);
solAssert(t, ""); solAssert(t, "");
ret["type"].append(formatType(member.name, *t, _forLibrary)); ret["components"].append(formatType(member.name, *t, _forLibrary));
} }
} }
else else

View File

@ -950,32 +950,39 @@ BOOST_AUTO_TEST_CASE(return_structs)
} }
)"; )";
char const* interface = R"( char const* interface = R"(
[ [{
{
"constant" : false, "constant" : false,
"payable": false,
"inputs" : [], "inputs" : [],
"name" : "f", "name" : "f",
"outputs": [{ "outputs" : [
{
"name" : "x", "name" : "x",
"type" : "uint256" "type" : "uint256"
}, { },
"name": "s", {
"type": [{ "components" : [
{
"name" : "a", "name" : "a",
"type" : "uint256" "type" : "uint256"
}, { },
"name": "sub", {
"subtype": [{ "components" : [
{
"name" : "x", "name" : "x",
"type" : "uint256[2]" "type" : "uint256[2]"
}],
"type": "[]"
}]
}],
"type" : "function"
} }
] ],
"name" : "sub",
"type" : "[]"
}
],
"name" : "s",
"type" : ""
}
],
"payable" : false,
"type" : "function"
}]
)"; )";
checkInterface(text, interface); checkInterface(text, interface);
} }
@ -990,28 +997,33 @@ BOOST_AUTO_TEST_CASE(return_structs_with_contracts)
} }
)"; )";
char const* interface = R"( char const* interface = R"(
[ [{
{
"constant": false, "constant": false,
"payable": false,
"inputs": [], "inputs": [],
"name": "f", "name": "f",
"outputs" : [{ "outputs": [
"name" : "s", {
"type" : [{ "components": [
{
"name": "x", "name": "x",
"type": "address[]" "type": "address[]"
}, { },
{
"name": "y", "name": "y",
"type": "address" "type": "address"
}] }
}, { ],
"name": "s",
"type": ""
},
{
"name": "c", "name": "c",
"type": "address" "type": "address"
}],
"type" : "function"
} }
] ],
"payable": false,
"type": "function"
}]
)"; )";
checkInterface(text, interface); checkInterface(text, interface);
} }
@ -1028,31 +1040,44 @@ BOOST_AUTO_TEST_CASE(event_structs)
char const *interface = R"( char const *interface = R"(
[{ [{
"anonymous": false, "anonymous": false,
"inputs" : [{ "inputs": [
{
"components": [
{
"name": "x",
"type": "uint256[2]"
}
],
"indexed": false, "indexed": false,
"name": "t", "name": "t",
"type" : [{ "type": ""
"name" : "x", },
"type" : "uint256[2]" {
}] "components": [
}, { {
"indexed" : false,
"name" : "s",
"type" : [{
"name": "a", "name": "a",
"type": "uint256" "type": "uint256"
}, { },
"name" : "sub", {
"subtype" : [{ "components": [
{
"name": "x", "name": "x",
"type": "uint256[2]" "type": "uint256[2]"
}], }
],
"name": "sub",
"type": "[]" "type": "[]"
}, { },
{
"name": "b", "name": "b",
"type": "bytes" "type": "bytes"
}] }
}], ],
"indexed": false,
"name": "s",
"type": ""
}
],
"name": "E", "name": "E",
"type": "event" "type": "event"
}] }]
@ -1073,33 +1098,45 @@ BOOST_AUTO_TEST_CASE(structs_in_libraries)
char const* interface = R"( char const* interface = R"(
[{ [{
"constant": false, "constant": false,
"inputs" : [{ "inputs": [
"name" : "s", {
"type" : [{ "components": [
{
"name": "a", "name": "a",
"type": "uint256" "type": "uint256"
}, { },
"name" : "sub", {
"subtype" : [{ "components": [
{
"name": "x", "name": "x",
"type": "uint256[2]" "type": "uint256[2]"
}], }
],
"name": "sub",
"type": "[]" "type": "[]"
}, { },
{
"name": "b", "name": "b",
"type": "bytes" "type": "bytes"
}] }
}], ],
"name": "s",
"type": ""
}
],
"name": "g", "name": "g",
"outputs": [], "outputs": [],
"payable": false, "payable": false,
"type": "function" "type": "function"
}, { },
{
"constant": false, "constant": false,
"inputs" : [{ "inputs": [
{
"name": "s", "name": "s",
"type": "L.S storage" "type": "L.S storage"
}], }
],
"name": "f", "name": "f",
"outputs": [], "outputs": [],
"payable": false, "payable": false,