{"library":"oslo-versionedobjects","title":"Oslo Versioned Objects","description":"Oslo Versioned Objects is an OpenStack library that provides a framework for defining data objects with built-in versioning capabilities. It allows for seamless evolution of object schemas, particularly important in RPC (Remote Procedure Call) contexts where different service versions might communicate. It's currently at version 3.9.0 and is part of the OpenStack Oslo libraries, receiving updates in sync with OpenStack releases.","language":"python","status":"active","last_verified":"Thu Apr 16","install":{"commands":["pip install oslo-versionedobjects"],"cli":null},"imports":["from oslo_versionedobjects import base","from oslo_versionedobjects import fields","from oslo_versionedobjects import base"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import uuid\nfrom oslo_versionedobjects import base, fields\n\n# 1. Define your VersionedObject\nclass MyExampleObject(base.VersionedObject):\n    # The current version of this object schema.\n    # Increment this when making backwards-incompatible changes.\n    VERSION = '1.0'\n\n    # Define the fields for your object\n    fields = {\n        'id': fields.UUIDField(),\n        'name': fields.StringField(nullable=False),\n        'status': fields.StringField(default='active'),\n        'value': fields.IntegerField(nullable=True, default=0),\n    }\n\n    # Optional: Override __init__ to set defaults or perform custom logic\n    def __init__(self, context=None, **kwargs):\n        super().__init__(context, **kwargs)\n        # Call obj_set_defaults() to ensure default values from fields are applied.\n        self.obj_set_defaults()\n\n    # Optional: Add methods to your object\n    def activate(self):\n        if self.status != 'active':\n            self.status = 'active'\n            self.obj_make_compatible() # Mark object as changed for serialization\n            print(f\"Object {self.name} activated.\")\n        else:\n            print(f\"Object {self.name} is already active.\")\n\n# 2. Instantiate and use your object\n# Create an instance with some data\nobj_id = str(uuid.uuid4())\nmy_obj = MyExampleObject(id=obj_id, name=\"First Item\", value=100)\n\nprint(f\"Initial object: {my_obj.obj_name} v{my_obj.obj_version}\")\nprint(f\"ID: {my_obj.id}\")\nprint(f\"Name: {my_obj.name}\")\nprint(f\"Status: {my_obj.status}\")\nprint(f\"Value: {my_obj.value}\")\nprint(f\"Is changed? {my_obj.obj_what_changed()}\")\n\nmy_obj.activate()\nprint(f\"Status after activation: {my_obj.status}\")\nprint(f\"Is changed? {my_obj.obj_what_changed()}\")\n\n# 3. Serialize and Deserialize (demonstrates versioning capability)\nserializer = base.VersionedObjectSerializer()\n\n# Convert the object to a primitive (dictionary) for serialization\nprimitive = serializer.serialize_entity(None, my_obj)\nprint(\"\\nSerialized primitive:\")\nprint(primitive)\n\n# Simulate deserialization (e.g., after receiving over RPC)\ndeserialized_obj = serializer.deserialize_entity(None, MyExampleObject, primitive)\nprint(\"\\nDeserialized object:\")\nprint(f\"Name: {deserialized_obj.name}\")\nprint(f\"Status: {deserialized_obj.status}\")\nprint(f\"Value: {deserialized_obj.value}\")\nprint(f\"Are objects equal? {my_obj == deserialized_obj}\")\nprint(f\"Has deserialized object changed? {deserialized_obj.obj_what_changed()}\")","lang":"python","description":"This quickstart demonstrates how to define a basic VersionedObject with fields and methods. It then shows how to instantiate it, modify its state, and finally serialize it to a primitive representation (dictionary) and deserialize it back, illustrating the core versioning and serialization capabilities of the library.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}