pragma solidity ^0.4.2;
contract Patient {
string public name;
string public dateOfBirth;
string public gender;
// FAMILY^GIVEN^MIDDLE
function SetName(string _name) {
if(bytes(_name).length <= 0) {
throw;
}
name = _name;
}
// YYYYMMDD
function SetDateOfBirth(string _dateOfBirth) {
var dobBytes = bytes(_dateOfBirth);
// check length
if(dobBytes.length != 8) {
throw;
}
// check for numeric
for(var i=0; i < 8; i++) {
if(dobBytes[i] < '0' || dobBytes[i] > '9') {
throw;
}
}
// validate year, month, day
dateOfBirth = _dateOfBirth;
}
// M,F,U,O
function SetGender(string _gender) {
var genderBytes = bytes(_gender);
if(genderBytes.length != 1) {
throw;
}
if(genderBytes[0] != 'M' &&
genderBytes[0] != 'F' &&
genderBytes[0] != 'O' &&
genderBytes[0] != 'U') {
throw;
}
gender = _gender;
}
}
The logic should be easy to understand for those with programming skills. A few notes about this:
1) In each case, the string must be cast into bytes to perform validation. Solidity unfortunately has poor support for strings - you cannot check individual characters or even the length! The solidity-stringutils library provides much needed string functions.
2) Solidity currently has very limited support for exceptions - you cannot give them a name or add any data to them. To the caller, an exception is returned with a cryptic error like "VM Exception: invalid JUMP at 034277a5bb8a98c36ad8ebf8d38277272ce38b35e6a39916f77bd6c3903e8c45/692a70d2e424a56d2c6c27aa97d1a86395877b3a:2119"
Hopefully this gives you an idea of how data can be stored in ethereum and logic applied to that data. Next up is looking at ethereum events which allows enables notification of changes in ethereum to the outside world and basic queries.
No comments:
Post a Comment