String32 kwargs (v0.1)

Loading...
contract Kwargs {
    //Stores key/value pairs in this contract. Then retrieve them one by one from the other.
    //Costs more, but allows more generic contracts.
    function Kwargs(address _creator, address _retriever) {
        creator = _creator;
        retriever = _retriever;
    }
    
    modifier isCreator () { if (msg.sender == creator) _ }
    modifier isRetriever () { if (msg.sender == retriever) _ }
    
    function addArgument(string32 _key, string32 _value) isCreator {
        dict[_key] = _value;
    }
    
    function retrieveArgument (string32 _key) isRetriever constant returns (string32 value) {
        return dict[_key];
    }
    
    address creator;
    address retriever;
    //Future: identify different mappings for different types.
    mapping(string32 => string32) dict;
}

//example usage

//This contract is modular. It is supposed to be swapped as is required.
contract dataRetrievalFunctions {
    function retrieve(address _kwargContract, string32 _func) returns (string32 value) {
        Kwargs kwargs = Kwargs(address(_kwargContract));
        string32 v;
        
        if (_func == "getName") {
            v = kwargs.retrieveArgument("name");
        }
        
        if (_func == "getLocation") {
            v = kwargs.retrieveArgument("location");
        }
        
        return v;
    }
}

//example of a drf doing *other* things as dapp becomes more complex.
/*contract dataRetrievalFunctions {
    function retrieve(address _kwargContract, string32 _func) returns (string32 value) {
        Kwargs kwargs = Kwargs(address(_kwargContract));
        string32 v;
        
        if (_func == "getName") {
            //concatenation not yet supported, but an example.
            v = kwargs.retrieveArgument("name") + " " + kwargs.retrieveArgument("surname");
        }
        
        if (_func == "getLocation") {
            v = kwargs.retrieveArgument("location");
        }
        
        return v;
    }
}*/

//This remains as is. You don't have to change the code.
//If you want to change what gets returned if you call "getName" you simply slot in a new drf contract.
contract Admin {
    function Admin (address _drf) {
        drf = dataRetrievalFunctions(_drf);
        owner = msg.sender;
    }
    
    modifier isOwner () { if (msg.sender == owner) _ }
    
    //eg called from front-end:
    //dataCapture.addArgument(<address>, "name", "simon");
    //dataCapture.addArgument(<address>, "surname", "de la Rouviere");
    //dataCapture.addArgument(<address>, "town", "Stellenbosch");
    //dataCapture.addArgument(<address>, "location", "South Africa");
    function addArgument(address _kwargContract, string32 _key, string32 _value) {
        Kwargs kwargs = Kwargs(address(_kwargContract));
        kwargs.addArgument(_key, _value);
    }
    
    function getName(address _kwargContract) returns (string32 name) {
        return drf.retrieve(_kwargContract, "getName");
    }
    
    function getLocation(address _kwargContract) returns (string32 location) {
        return drf.retrieve(_kwargContract, "getLocation");
    }
    
    function changeDRF (address _drfContract) isOwner {
        drf = dataRetrievalFunctions(_drfContract);
    }
    
    address owner;
    dataRetrievalFunctions drf;
}

Solidity contract for Ethereum

Basic attempt at making a contract that acts as kwargs (keyword arguments), allowing a function be generic, and pass through arbitrary amount of arguments. Future versions could include modular types as well (not just strings).

Preferences

Note: Your changes won't be saved, because not Logged-in.

Create your own Contract