Skip to content

Commit

Permalink
Properly handle inventory metadata field
Browse files Browse the repository at this point in the history
  • Loading branch information
SaladDais committed Dec 20, 2023
1 parent e6ac994 commit a7f40b0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
8 changes: 4 additions & 4 deletions hippolyzer/lib/base/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def to_writer(self, writer: StringIO):
continue

val = getattr(self, field_name)
if val is None:
if val is None and not field.metadata.get("include_none"):
continue

# Some kind of nested structure like sale_info
Expand Down Expand Up @@ -386,7 +386,7 @@ class InventoryObject(InventoryContainerBase):
ID_ATTR: ClassVar[str] = "obj_id"

obj_id: UUID = schema_field(SchemaUUID)
metadata: Optional[Dict[str, Any]] = schema_field(SchemaLLSD, default=None)
metadata: Optional[Dict[str, Any]] = schema_field(SchemaLLSD, default=None, include_none=True)

__hash__ = InventoryNodeBase.__hash__

Expand All @@ -401,7 +401,7 @@ class InventoryCategory(InventoryContainerBase):
pref_type: str = schema_field(SchemaStr, llsd_name="preferred_type")
owner_id: UUID = schema_field(SchemaUUID)
version: int = schema_field(SchemaInt)
metadata: Optional[Dict[str, Any]] = schema_field(SchemaLLSD, default=None)
metadata: Optional[Dict[str, Any]] = schema_field(SchemaLLSD, default=None, include_none=True)

__hash__ = InventoryNodeBase.__hash__

Expand All @@ -422,7 +422,7 @@ class InventoryItem(InventoryNodeBase):
sale_info: InventorySaleInfo = schema_field(InventorySaleInfo)
asset_id: Optional[UUID] = schema_field(SchemaUUID, default=None)
shadow_id: Optional[UUID] = schema_field(SchemaUUID, default=None)
metadata: Optional[Dict[str, Any]] = schema_field(SchemaLLSD, default=None)
metadata: Optional[Dict[str, Any]] = schema_field(SchemaLLSD, default=None, include_none=True)

__hash__ = InventoryNodeBase.__hash__

Expand Down
11 changes: 6 additions & 5 deletions hippolyzer/lib/base/legacy_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,20 @@ class SchemaLLSD(SchemaFieldSerializer[_T]):
"""Arbitrary LLSD embedded in a field"""
@classmethod
def deserialize(cls, val: str) -> _T:
return llsd.parse_notation(val.encode("utf8"))
return llsd.parse_xml(val.encode("utf8"))

@classmethod
def serialize(cls, val: _T) -> str:
return llsd.format_notation(val).decode("utf8")
return llsd.format_xml(val).decode("utf8")


def schema_field(spec: Type[Union[SchemaBase, SchemaFieldSerializer]], *, default=dataclasses.MISSING, init=True,
repr=True, hash=None, compare=True, llsd_name=None, llsd_only=False) -> dataclasses.Field: # noqa
repr=True, hash=None, compare=True, llsd_name=None, llsd_only=False,
include_none=False) -> dataclasses.Field: # noqa
"""Describe a field in the inventory schema and the shape of its value"""
return dataclasses.field( # noqa
metadata={"spec": spec, "llsd_name": llsd_name, "llsd_only": llsd_only}, default=default,
init=init, repr=repr, hash=hash, compare=compare,
metadata={"spec": spec, "llsd_name": llsd_name, "llsd_only": llsd_only, "include_none": include_none},
default=default, init=init, repr=repr, hash=hash, compare=compare,
)


Expand Down
6 changes: 6 additions & 0 deletions tests/base/test_legacy_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
\t\tparent_id\t00000000-0000-0000-0000-000000000000
\t\ttype\tcategory
\t\tname\tContents|
\t\tmetadata\t<llsd><undef /></llsd>
|
\t}
\tinv_item\t0
\t{
Expand Down Expand Up @@ -39,6 +41,8 @@
\t}
\t\tname\tNew Script|
\t\tdesc\t2020-04-20 04:20:39 lsl2 script|
\t\tmetadata\t<llsd><map><key>experience</key><uuid>a2e76fcd-9360-4f6d-a924-000000000003</uuid></map></llsd>
|
\t\tcreation_date\t1587367239
\t}
"""
Expand All @@ -61,6 +65,7 @@ def test_item_access(self):
item = self.model.nodes[UUID('dd163122-946b-44df-99f6-a6030e2b9597')]
self.assertEqual(item.name, "New Script")
self.assertEqual(item.sale_info.sale_type, "not")
self.assertDictEqual(item.metadata, {"experience": UUID("a2e76fcd-9360-4f6d-a924-000000000003")})
self.assertEqual(item.model, self.model)

def test_access_children(self):
Expand Down Expand Up @@ -112,6 +117,7 @@ def test_llsd_serialization(self):
'inv_type': 'script',
'item_id': UUID('dd163122-946b-44df-99f6-a6030e2b9597'),
'name': 'New Script',
'metadata': {"experience": UUID("a2e76fcd-9360-4f6d-a924-000000000003")},
'parent_id': UUID('f4d91477-def1-487a-b4f3-6fa201c17376'),
'permissions': {
'base_mask': 2147483647,
Expand Down

0 comments on commit a7f40b0

Please sign in to comment.