Tenemos un struct
como sigue:
struct ObjectStruct {
bytes32 state;
address owner;
bool isObject;
}
mapping(bytes32 => ObjectStruct) public objectStructs;
bytes32[] public objectList;
Ahora, queremos modificar bytes32 state;
el campo para que sea dinámico y así podamos "agregarle" varios "sub_estados", por ejemplo. objectStructs[_object_id].state.location = _location;
o objectStructs[_object_id].state.price= _price;
etc
¿Cómo podemos definir un "campo de estado dinámico" de tal manera que podamos agregarle cualquier "sub_estado"?
Lo definí de la siguiente manera:
struct ObjectStruct {
bytes32 location;
bytes32[] state;
address owner;
bool isObject;
}
Y luego :
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);
entonces, invocamos function newObject
de la siguiente manera:
newObject(100, "location:Paris,price:50,sold:yes", '0xE07b6e5a2026CC916A4E2Beb03767ae0ED6af773');
Estoy equivocado ?
No existe tal 'campo de estado dinámico' como explicas en tu publicación. Pero puede usar un mapeo dentro de la estructura que lo hará por usted.
struct StateStruct {
bytes32 description;
mapping(bytes32 => bytes32) sub_state;
}
struct ObjectStruct {
StateStruct state;
address owner;
bool isObject;
}
entonces usa
objectStructs[id].state.sub_state["location"] = "Paris";
objectStructs[id].state.sub_state["sold"] = "Yes";
objectStructs[id].state.sub_state["price"] = 50;
Preguntador
mirg
Preguntador
mirg
(bytes32[] states,bytes32[] state_values)
iterar a través de la matriz y almacenar el subestado;objectStructs[_object_id].state.sub_state[states[i]] = state_values[i]
el evento también se puede cambiar pasando las matrices en lugar de una sola cadena (eso depende de usted en realidad).Preguntador
Preguntador