1

I have the below struct in a contract:

struct ServiceListStruct {
string cloudID;
address gatewayID;
string serviceProducerID;
mapping (string => string) serviceProducerMetadata;
string serviceConsumerMetaAttr;
string accessControlModel;
}
ServiceListStruct[] public BCServiceList;

ServiceListStruct sls;

Also a function to add struct elements to the array as below:

function addEntry(string _cloudID, address _gatewayID, string _serviceProducerID, string _serviceProducerMetadata, string _serviceConsumerMetaAttr, string _accessControlModel) public returns (uint)
{
require(msg.sender==_gatewayID);
sls.cloudID = _cloudID;
sls.gatewayID = _gatewayID;
sls.serviceProducerID = _serviceProducerID;
sls.serviceConsumerMetaAttr = _serviceConsumerMetaAttr;
sls.accessControlModel = _accessControlModel;
//
string memory s1;
string memory s2 = _serviceProducerMetadata;
string  memory s3;
bytes memory s2bytes = bytes(_serviceProducerMetadata);
while(s2bytes.length != 0)
{
   (s1,s2) = splitString(s2,";");
   (s1,s3) = splitString(s1,":");
   sls.serviceProducerMetadata[s1] =s3;
   s2bytes = bytes(s2);
}
BCServiceList.push(sls);
return 1;
}

The logic of split string function is as below:

function splitString(string _s, string _seperator) public returns(string, string)
{
var s_slice = _s.toSlice();
var seperator_slice = _seperator.toSlice();
string memory result = "";
var result_slice = result.toSlice();
result_slice = s_slice.split(seperator_slice);
return (result_slice.toString(), s_slice.toString());
}

it works perfectly fine, i checked it too.

if the input is "computer","p" to split string function, it returns "com" and "uter"

Here is the input i used for the addentry function - "LC2","0x14723a09acff6d2a60dcdf7aa4aff308fddc160c","SP2","location:inside;reading:degree;protocol:http","scattr","ngac" and tried to call the function from the same address in remix. the transaction was ssuccessful. Then I tried to look at the contents of the entry in struct array at position 0 and mapping key "reading" using the below function:

function getmappingentry(uint index, string s1) public returns (string, string)
{
    return (BCServiceList[index].cloudID, BCServiceList[index].serviceProducerMetadata[s1]);
}

The output is

{
    "0": "string: LC2",
    "1": "string: "
}

According to my understanding it should return

{
    "0": "string: LC2",
    "1": "string: degree"
}

Can someone help me understand what is missing here?

Thanks in advance!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.