I was trying to build a nested map of structs in SmartContract. With a single level, after inserting data(insertData) the validate method is returning correct results with the below code containing few commented lines. But when i uncomment the childMap related things -the childMap in the ParentStruct, the parameters in insertData method and initialising the childMap then its returning wrong result. Please find the smart contract code below.
pragma solidity ^0.4.13;
contract SampleContract {
struct ChildStruct {
bool isPresent;
bytes32 name;
}
struct ParentStruct {
bool isPresent;
bytes32 name;
//mapping (bytes32 => ChildStruct) childMap;
}
mapping(bytes32 => ParentStruct) sampleStructs;
function insertData(bytes32 parentAddress, bytes32 parentName
//,bytes32 childAddress, bytes32 childName
)
public returns(bool success)
{
ParentStruct storage c = sampleStructs[parentAddress];
c.isPresent = true;
c.name = parentName;
//c.childMap[childAddress] = ChildStruct(true, childName);
return true;
}
function validate(bytes32 parentAddress)
public returns(bool isPresent, string name)
{
return
(sampleStructs[parentAddress].isPresent,
bytes32ToString(sampleStructs[parentAddress].name)
);
}
function bytes32ToString(bytes32 x) constant returns (string)
{
bytes memory bytesString = new bytes(32);
uint charCount = 0;
for (uint j = 0; j < 32; j++) {
byte char = byte(bytes32(uint(x) * 2 ** (8 * j)));
if (char != 0) {
bytesString[charCount] = char;
charCount++;
}
}
bytes memory bytesStringTrimmed = new bytes(charCount);
for (j = 0; j < charCount; j++) {
bytesStringTrimmed[j] = bytesString[j];
}
return string(bytesStringTrimmed);
}
}
Could you please help me if i'm missing anything ?