0

I want to require two variables for the execution of my function, firstly a string shall not be undefined and then a mapping (address => uint) that shall be undefined. In the latter case, I have user numbers from 0...n, so 0 would be the first user. Is there an easy way to do this, or do I have to do complicated workarounds with eg. keccak hashes?

struct userData {
    string name;
    string role;   
    mapping (address => uint) accountToId;    
}

mapping (address => userData) addressToUserData; 

function register(string _name, string _role) public {
    require(_name !== false); // to avoid empty names
    require(addressToUserData[msg.sender] == false); // make sure mapping is undefined
    addressToUserData[msg.sender] = userData(_name, _role);
}

[I hope by now there is better solution than this]

Marcellvs
  • 423
  • 1
  • 4
  • 13

1 Answers1

0
function register(string memory _name, string memory _role) public {
  require(bytes (_name).length > 0); // to avoid empty names
  require(bytes (addressToUserData[msg.sender].name).length == 0); // make sure mapping is undefined
  addressToUserData[msg.sender] = userData(_name, _role);
}
Mikhail Vladimirov
  • 7,313
  • 1
  • 23
  • 38
  • Thank you for you hint with the bytes typecasting. However, it won't work if the mapping maps to an integer, which is the case in my code. For e.g. require(bytes (datasets[_dataId].accountToId[_account]).length == 0) There is an error: TypeError: Explicit type conversion not allowed from "uint256" to "bytes memory" – Marcellvs Apr 10 '19 at 17:31
  • Could you show your code? – Mikhail Vladimirov Apr 10 '19 at 19:39
  • Sure, thank you: link

    --> function addUserToLoan

    – Marcellvs Apr 10 '19 at 22:26