[Ethereum] How to access content of mapping in a struct

mappingsoliditystruct

I'm aware of this post:

Accessing a mapping within a struct via Truffle console

However, it seems to me, it does not provide a clear answer. Also, my question is more generic and not just for truffle. Also, there's similar code in here (section "structs") but couldn't find any answer to the below questions.


Case 1:

Assume we have the following pseudo-code in solidity:

mapping (uint => BB) public map_1; 

struct BB{

  mapping (uint => address) map_2;
}

Question : Given a key, how can I access the content of map_2 in Geth or a user interface?


Case 2:

Assume we have the following pseudo-code in solidity:

mapping (uint => BB) public map_1; 

struct AA{
uint val;
}

struct BB{

  mapping (uint => AA) map_2;
}

My question for this case remains the same as the one for case 1.

Best Answer

You can access the case 1s mapping by given the both keys. The key for map_1 and key for map_2. Use pragma solidity ^0.4.20; when compiling

Case 1

contract test {
    struct BB {
        mapping(uint => string) map_2;
    }
    mapping(uint => BB) map_1;

    function test() public {
        map_1[1].map_2[1] = "hello"; //setting temp value
    }

    function maping() public view returns(string) {
        return map_1[1].map_2[1]; //accessing the value
    }
}

Case 2

contract test {
    struct BB {
        mapping(uint => AA) map_2;
    }
    struct AA{
        uint val;
    }
    mapping(uint => BB) map_1;

    function test() public {
        map_1[1].map_2[1].val = 10;
    }

    function maping() public view returns(uint) {
        return map_1[1].map_2[1].val;
    }
}