11

I have a mapping(string => Person) public map:

struct Person {
        string name;
        string description;
        address primaryAddress;
        string linkToWebsite;
        string linkToPicture;
        address secondaryAddress;
        uint age;
}

, this struct is only made of strings, addresses and uints, but when compiling I get this error.

Internal compiler error: Accessors for mapping with dynamically-sized keys not yet implemented.

BUT,

From Solidity Features:

Strings as Mapping Keys

Strings are allowed as keys for mappings.

contract C {   mapping (string => uint) counter;   
function inc(string _s) { counter[_s]++; } }

Any help with it?

arodriguezdonaire
  • 3,027
  • 3
  • 20
  • 37

3 Answers3

21

The thing was that my mapping was public, and there are no public accessors implemented for it yet. The solution is just to not declare the mapping as public.

arodriguezdonaire
  • 3,027
  • 3
  • 20
  • 37
  • Why do you need a public accessor? Shouldn't it generate the getter?

    "It is possible to mark mappings public and have Solidity create a getter. The _KeyType will become a required parameter for the getter and it will return _ValueType."

    source

    – obesechicken13 Mar 06 '18 at 20:18
  • 1
    I declared my mapping as private to solve this – Alex Mar 10 '18 at 10:19
5

Strings are allowed as keys in mapping. For example, this code works just fine testing in Pyethereum:

 from ethereum import tester as t

 code = '''
    contract Example {
       struct Person {
         string name;
       }
       mapping(string => Person) map;

       function setName(string name, string value) {
          map[name] = Person({name: value});
       }

       function getName(string key) constant returns (string) {
         return map[key].name;
       }

   }
  '''

   state = t.state()
   contract =  state.abi_contract(code, language='solidity')
   contract.setName('dave', 'hello')
   print contract.getName('dave')
   => 'hello'

There must be something going on in your actual contract code

dbryson
  • 6,403
  • 2
  • 27
  • 37
1

without seeing your struct it's hard to guess, is this helping ?

contract structtestmapping {
    struct myStruct {
        uint id;
    }
    mapping(string => myStruct) mapmystruct;
    myStruct s = mapmystruct['mystring'];

}

your struct might contain dynamically sized elements as the code here suggests ?

euri10
  • 4,640
  • 5
  • 24
  • 55
  • this is not helpful for me, I want to access to the mapping with a string key which is given by parameter, but thank you anyway. I have strings and addresses in the struct. – arodriguezdonaire Mar 29 '16 at 14:35
  • a string is considered a dynamically sized element? – arodriguezdonaire Mar 29 '16 at 15:33
  • I think so if this is correct, wouldn't bet my hand on it though https://github.com/ethereum/wiki/wiki/Solidity-Features#re-introduce-string-type : Re-introduce string type

    PT string is added as a type which behaves exactly like bytes with the following differences:

    index access is not allowed
    it does not have a length member
    
    

    In the ABI encoding (wiki already changed), string is a dynamic type whose "number of elements" field is the number of bytes, not the number of characters. The encoding of the string is assumed to be UTF-8, but is not yet used inside Solidity.

    – euri10 Mar 29 '16 at 15:45
  • Is it possible then to fix the size of the strings in my struct? – arodriguezdonaire Mar 29 '16 at 15:47
  • Also @dbryson has a string in his struct and his solution works – arodriguezdonaire Mar 29 '16 at 15:48
  • why dont you post your struct, might be easier for everyone ;) – euri10 Mar 29 '16 at 15:50
  • done, you have it – arodriguezdonaire Mar 29 '16 at 15:52
  • using your struct in this simple contract with what @dbryson posted I got no compile error in Mix 1.0.1, solidity 0.3.0, I'm at a loss sorry (https://gist.github.com/euri10/068956d92e9c084fa04995110b48ab26) – euri10 Mar 29 '16 at 17:50