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() + "]";
solAssert(arrayType->baseType(), "");
Json::Value subtype = formatType("", *arrayType->baseType(), _forLibrary);
if (subtype["type"].isString() && !subtype.isMember("subtype"))
ret["type"] = subtype["type"].asString() + suffix;
else
if (subtype.isMember("components"))
{
ret["type"] = suffix;
solAssert(!subtype.isMember("subtype"), "");
ret["subtype"] = subtype["type"];
ret["type"] = subtype["type"].asString() + suffix;
ret["components"] = subtype["components"];
}
else
ret["type"] = subtype["type"].asString() + suffix;
}
}
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))
{
solAssert(member.type, "");
auto t = member.type->interfaceType(_forLibrary);
solAssert(t, "");
ret["type"].append(formatType(member.name, *t, _forLibrary));
ret["components"].append(formatType(member.name, *t, _forLibrary));
}
}
else

View File

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