[Ethereum] Default getter and setter for mapping in solidity

mappingsoliditystoragestructtruffle

In my smart contract, I have a public mapping as mapping (uint=>User) public users where User is a Struct. Is there any way to get the values of this mapping using a default getter?

For Example : If we have public uint count, we can simply call it by using Object.count. Is it possible with mapping as well?

Please help!

Best Answer

First of all, there is no such thing as default setter (mentioned in title of your question).

Now about default getter. Solidity documentation gives the following example:

struct Data {
    uint a;
    bytes3 b;
    mapping (uint => uint) map;
}
mapping (uint => mapping(bool => Data[])) public data;

will generate the following getter:

function data(uint arg1, bool arg2, uint arg3) public returns (uint a, bytes3 b) {
    a = data[arg1][arg2][arg3].a;
    b = data[arg1][arg2][arg3].b;
}

So, default getter returns not the structure, but rather tuple of all atomic structure fields.

In your case getter will look like:

function users (uint id) public returns (/* all atomic fields of User structure */)
Related Topic