Context
I am storing the content of the following mapping into a file:
<code>pragma solidity >=0.8.25 <0.9.0;
import "test/TestConstants.sol";
library IterableMapping {
// Iterable mapping from string[] to uint;
struct Map {
string[] keys;
mapping(string => uint256) values;
mapping(string => uint256) indexOf;
mapping(string => bool) inserted;
}
function get(Map storage map, string memory key) public view returns (uint256) {
return map.values[key];
}
function getKeys(Map storage map) public view returns (string[] memory) {
return map.keys;
}
function getValues(Map storage map) public view returns (uint256[] memory) {
uint256[] memory listOfValues = new uint256[](_MAX_NR_OF_TEST_LOG_VALUES_PER_LOG_FILE);
for (uint256 i = 0; i < map.keys.length - 1; i++) {
listOfValues[i] = map.values[map.keys[i]];
}
return listOfValues;
}
function getKeyAtIndex(Map storage map, uint256 index) public view returns (string memory) {
return map.keys[index];
}
function size(Map storage map) public view returns (uint256) {
return map.keys.length;
}
function set(Map storage map, string memory key, uint256 val) public {
if (map.inserted[key]) {
map.values[key] = val;
} else {
map.inserted[key] = true;
map.values[key] = val;
map.indexOf[key] = map.keys.length;
map.keys.push(key);
}
}
function remove(Map storage map, string memory key) public {
if (!map.inserted[key]) {
return;
}
delete map.inserted[key];
delete map.values[key];
uint256 index = map.indexOf[key];
string memory lastKey = map.keys[map.keys.length - 1];
map.indexOf[lastKey] = index;
delete map.indexOf[key];
map.keys[index] = lastKey;
map.keys.pop();
}
}
</code>
<code>pragma solidity >=0.8.25 <0.9.0;
import "test/TestConstants.sol";
library IterableMapping {
// Iterable mapping from string[] to uint;
struct Map {
string[] keys;
mapping(string => uint256) values;
mapping(string => uint256) indexOf;
mapping(string => bool) inserted;
}
function get(Map storage map, string memory key) public view returns (uint256) {
return map.values[key];
}
function getKeys(Map storage map) public view returns (string[] memory) {
return map.keys;
}
function getValues(Map storage map) public view returns (uint256[] memory) {
uint256[] memory listOfValues = new uint256[](_MAX_NR_OF_TEST_LOG_VALUES_PER_LOG_FILE);
for (uint256 i = 0; i < map.keys.length - 1; i++) {
listOfValues[i] = map.values[map.keys[i]];
}
return listOfValues;
}
function getKeyAtIndex(Map storage map, uint256 index) public view returns (string memory) {
return map.keys[index];
}
function size(Map storage map) public view returns (uint256) {
return map.keys.length;
}
function set(Map storage map, string memory key, uint256 val) public {
if (map.inserted[key]) {
map.values[key] = val;
} else {
map.inserted[key] = true;
map.values[key] = val;
map.indexOf[key] = map.keys.length;
map.keys.push(key);
}
}
function remove(Map storage map, string memory key) public {
if (!map.inserted[key]) {
return;
}
delete map.inserted[key];
delete map.values[key];
uint256 index = map.indexOf[key];
string memory lastKey = map.keys[map.keys.length - 1];
map.indexOf[lastKey] = index;
delete map.indexOf[key];
map.keys[index] = lastKey;
map.keys.pop();
}
}
</code>
pragma solidity >=0.8.25 <0.9.0;
import "test/TestConstants.sol";
library IterableMapping {
// Iterable mapping from string[] to uint;
struct Map {
string[] keys;
mapping(string => uint256) values;
mapping(string => uint256) indexOf;
mapping(string => bool) inserted;
}
function get(Map storage map, string memory key) public view returns (uint256) {
return map.values[key];
}
function getKeys(Map storage map) public view returns (string[] memory) {
return map.keys;
}
function getValues(Map storage map) public view returns (uint256[] memory) {
uint256[] memory listOfValues = new uint256[](_MAX_NR_OF_TEST_LOG_VALUES_PER_LOG_FILE);
for (uint256 i = 0; i < map.keys.length - 1; i++) {
listOfValues[i] = map.values[map.keys[i]];
}
return listOfValues;
}
function getKeyAtIndex(Map storage map, uint256 index) public view returns (string memory) {
return map.keys[index];
}
function size(Map storage map) public view returns (uint256) {
return map.keys.length;
}
function set(Map storage map, string memory key, uint256 val) public {
if (map.inserted[key]) {
map.values[key] = val;
} else {
map.inserted[key] = true;
map.values[key] = val;
map.indexOf[key] = map.keys.length;
map.keys.push(key);
}
}
function remove(Map storage map, string memory key) public {
if (!map.inserted[key]) {
return;
}
delete map.inserted[key];
delete map.values[key];
uint256 index = map.indexOf[key];
string memory lastKey = map.keys[map.keys.length - 1];
map.indexOf[lastKey] = index;
delete map.indexOf[key];
map.keys[index] = lastKey;
map.keys.pop();
}
}
and when I try to read that mapping back from a file with:
<code>(string memory hitRateFilePath, bytes memory data) = _testFileLogging.updateLogFile(_map.getKeys(), _map.getValues());
// IterableMapping.Map something = abi.decode(data, (IterableMapping));
// IterableMapping.Map memory something = abi.decode(data, (IterableMapping));
// IterableMapping.Map storage something = abi.decode(data, (IterableMapping));
// IterableMapping.Map calldata something = abi.decode(data, (IterableMapping));
// IterableMapping something = abi.decode(data, (IterableMapping));
// IterableMapping memory something = abi.decode(data, (IterableMapping));
// IterableMapping storage something = abi.decode(data, (IterableMapping));
// IterableMapping calldata something = abi.decode(data, (IterableMapping));
</code>
<code>(string memory hitRateFilePath, bytes memory data) = _testFileLogging.updateLogFile(_map.getKeys(), _map.getValues());
// IterableMapping.Map something = abi.decode(data, (IterableMapping));
// IterableMapping.Map memory something = abi.decode(data, (IterableMapping));
// IterableMapping.Map storage something = abi.decode(data, (IterableMapping));
// IterableMapping.Map calldata something = abi.decode(data, (IterableMapping));
// IterableMapping something = abi.decode(data, (IterableMapping));
// IterableMapping memory something = abi.decode(data, (IterableMapping));
// IterableMapping storage something = abi.decode(data, (IterableMapping));
// IterableMapping calldata something = abi.decode(data, (IterableMapping));
</code>
(string memory hitRateFilePath, bytes memory data) = _testFileLogging.updateLogFile(_map.getKeys(), _map.getValues());
// IterableMapping.Map something = abi.decode(data, (IterableMapping));
// IterableMapping.Map memory something = abi.decode(data, (IterableMapping));
// IterableMapping.Map storage something = abi.decode(data, (IterableMapping));
// IterableMapping.Map calldata something = abi.decode(data, (IterableMapping));
// IterableMapping something = abi.decode(data, (IterableMapping));
// IterableMapping memory something = abi.decode(data, (IterableMapping));
// IterableMapping storage something = abi.decode(data, (IterableMapping));
// IterableMapping calldata something = abi.decode(data, (IterableMapping));
I get the error:
<code>Compiler run failed:
Error (9574): Type library IterableMapping is not implicitly convertible to expected type struct IterableMapping.Map storage pointer.
--> test/functional/DecentralisedInvestmentManager/triggerReturnAll/TestWithRandomNrOfInvestments.sol:108:5:
|
108 | IterableMapping.Map storage something = abi.decode(data, (IterableMapping));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error:
Compilation failed
``
## Question
How can I decode the data that is written to file with:
```sol
function updateLogFile(
string[] memory keys,
uint256[] memory values) public returns (string memory hitRateFilePath, bytes memory data) {
// initialiseHitRates();
// Output hit rates to file if they do not exist yet.
string memory serialisedTextString = convertHitRatesToString(keys,values);
hitRateFilePath = createLogFileIfItDoesNotExist(_LOG_TIME_CREATOR, serialisedTextString);
// Read the latest hitRates from file.
data = readDataFromFile(hitRateFilePath);
return (hitRateFilePath, data);
}
</code>
<code>Compiler run failed:
Error (9574): Type library IterableMapping is not implicitly convertible to expected type struct IterableMapping.Map storage pointer.
--> test/functional/DecentralisedInvestmentManager/triggerReturnAll/TestWithRandomNrOfInvestments.sol:108:5:
|
108 | IterableMapping.Map storage something = abi.decode(data, (IterableMapping));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error:
Compilation failed
``
## Question
How can I decode the data that is written to file with:
```sol
function updateLogFile(
string[] memory keys,
uint256[] memory values) public returns (string memory hitRateFilePath, bytes memory data) {
// initialiseHitRates();
// Output hit rates to file if they do not exist yet.
string memory serialisedTextString = convertHitRatesToString(keys,values);
hitRateFilePath = createLogFileIfItDoesNotExist(_LOG_TIME_CREATOR, serialisedTextString);
// Read the latest hitRates from file.
data = readDataFromFile(hitRateFilePath);
return (hitRateFilePath, data);
}
</code>
Compiler run failed:
Error (9574): Type library IterableMapping is not implicitly convertible to expected type struct IterableMapping.Map storage pointer.
--> test/functional/DecentralisedInvestmentManager/triggerReturnAll/TestWithRandomNrOfInvestments.sol:108:5:
|
108 | IterableMapping.Map storage something = abi.decode(data, (IterableMapping));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error:
Compilation failed
``
## Question
How can I decode the data that is written to file with:
```sol
function updateLogFile(
string[] memory keys,
uint256[] memory values) public returns (string memory hitRateFilePath, bytes memory data) {
// initialiseHitRates();
// Output hit rates to file if they do not exist yet.
string memory serialisedTextString = convertHitRatesToString(keys,values);
hitRateFilePath = createLogFileIfItDoesNotExist(_LOG_TIME_CREATOR, serialisedTextString);
// Read the latest hitRates from file.
data = readDataFromFile(hitRateFilePath);
return (hitRateFilePath, data);
}
back into the mapping?