0

We have a struct as follows :

struct ObjectStruct {
        bytes32 state;
        address owner; 
        bool isObject;
    }
mapping(bytes32 => ObjectStruct) public objectStructs;
    bytes32[] public objectList;

Now, we want to modify bytes32 state; field such that it would be dynamic and so we would be able to "add" several "sub_state" to it, ex. objectStructs[_object_id].state.location = _location; or objectStructs[_object_id].state.price= _price; etc.

How we can define such a "dynamic state field" such that we would be able to add any "sub_state" to it ?

I defined it as follows:

struct ObjectStruct {
        bytes32 location;
        bytes32[] state;
        address owner; 
        bool isObject;
    }

And then :

function newObject(bytes32 _object_id, bytes32 _state, address _owner) public returns(bool success) {
        require(!isObject(_object_id));

        objectStructs[_object_id].state = _state;
        objectStructs[_object_id].owner = _owner;
        objectStructs[_object_id].isObject = true;
        objectList.push(_object_id);

        LogNewObject(msg.sender, _object_id, _state, _owner);
        return true;
    }

event LogNewObject(address sender, bytes32 _object_id, bytes32 state, address owner);

then, we invoke function newObject as follows :

newObject(100, "location:Paris,price:50,sold:yes", '0xE07b6e5a2026CC916A4E2Beb03767ae0ED6af773');

I am wrong ?

1 Answer 1

1

There is not such a 'dynamic state field' as you explain in your post. But you can use a mapping inside the struct that will do that for you.

struct StateStruct {
    bytes32 description;
    mapping(bytes32 => bytes32) sub_state;
}

struct ObjectStruct {
    StateStruct state;
    address owner; 
    bool isObject;
}

then use

objectStructs[id].state.sub_state["location"] = "Paris";
objectStructs[id].state.sub_state["sold"] = "Yes";
objectStructs[id].state.sub_state["price"] = 50;
6
  • Thank you so much for your helpful response, but I did not understand exactly where "myState" in your "struct ObjectStruct" is defined ? Thanks
    – Questioner
    May 4, 2018 at 10:23
  • sorry is my typo, corrected now
    – qbsp
    May 4, 2018 at 10:23
  • Thanks again. Just could you please let me know by this struct, how to define "function newObject" and "event LogNewObject" parameters ? Thank you.
    – Questioner
    May 4, 2018 at 10:31
  • you need to define the parameters in the state as array (bytes32[] states,bytes32[] state_values) iterate through the array and store the substate objectStructs[_object_id].state.sub_state[states[i]] = state_values[i] the event can be changed passing the arrays as well instead of a single string (that's up to you actually).
    – qbsp
    May 4, 2018 at 10:34
  • Thanks, I appreciate it if you could let me know how to set value of "states" and "state_values" at time of invoking function? it's a bit complicated :) Thanks again
    – Questioner
    May 4, 2018 at 10:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.