uAgents
uAgents copied to clipboard
Upgrade storage for easier handling of model types
It would be better to add methods to allow easier storage and access of model types to avoid having to do things like this:
tables = {int(num): TableStatus(**status) for (num, status) in ctx.storage._data.items()}
I just stumbled across JsonPickle which may help with the storage problem. I was able to deserialise complex objects without issue and where json.dumps(obj.__dict__) was just not good enough.
I think we can close this one for now and reopen if it get's important again.
Currently a developer can take care of the data transformations himself so something like the following is enough on an application level + more efficient as we don't have to implement introspection in the KeyValue storage.
# setting model data
dump = model_obj.json() # or model_obj.model_dump_json() for pydantic v2
ctx.storage.set("data", dump)
# getting model data
dump = ctx.storage.get("data")
model_data = YourModel.parse_raw(dump). # or YourModel.model_validate_json() for pydantic v2
And since we have the StorageAPI in uAgents now we paved the way for other storage mechanisms to be implemented anyway. It's just a matter of resources to provide new examples.