Resolve Python collection reads by schema alias
This commit is contained in:
parent
258a710273
commit
4f4a4699e5
2 changed files with 80 additions and 0 deletions
|
|
@ -81,6 +81,15 @@ class CultMeshNode:
|
|||
def get_required(self, document: DocumentDefinition[Any], key: str) -> Any:
|
||||
return self.database.get_required(document, key)
|
||||
|
||||
def get_all(self, document: DocumentDefinition[Any]) -> list[Any]:
|
||||
return self.database.get_all(document)
|
||||
|
||||
def get_by_name(self, document: DocumentDefinition[Any], name: str) -> Any:
|
||||
return self.database.get_by_name(document, name)
|
||||
|
||||
def get_by_index(self, document: DocumentDefinition[Any], index: str, value: str) -> Any:
|
||||
return self.database.get_by_index(document, index, value)
|
||||
|
||||
def get_global(self, document: DocumentDefinition[Any]) -> Any:
|
||||
return self.database.get_global(document)
|
||||
|
||||
|
|
@ -390,6 +399,23 @@ class CultMeshDatabase:
|
|||
raise CultCacheError(f"Missing {document.type}:{key}")
|
||||
return value
|
||||
|
||||
def get_all(self, document: DocumentDefinition[Any]) -> list[Any]:
|
||||
registered = self._resolve_document_alias(document)
|
||||
return [
|
||||
self._convert_document_value(value, registered, document)
|
||||
for value in self.cache.get_all(registered)
|
||||
]
|
||||
|
||||
def get_by_name(self, document: DocumentDefinition[Any], name: str) -> Any:
|
||||
registered = self._resolve_document_alias(document)
|
||||
value = self.cache.get_by_name(registered, name)
|
||||
return None if value is None else self._convert_document_value(value, registered, document)
|
||||
|
||||
def get_by_index(self, document: DocumentDefinition[Any], index: str, value: str) -> Any:
|
||||
registered = self._resolve_document_alias(document)
|
||||
result = self.cache.get_by_index(registered, index, value)
|
||||
return None if result is None else self._convert_document_value(result, registered, document)
|
||||
|
||||
def get_global(self, document: DocumentDefinition[Any]) -> Any:
|
||||
return self.get(document, self.cache.GLOBAL_KEY)
|
||||
|
||||
|
|
|
|||
|
|
@ -2134,6 +2134,10 @@ class CultCacheTests(unittest.TestCase):
|
|||
alias_value = node.get_required(alias, "note:alias")
|
||||
self.assertIsInstance(alias_value, UiNote)
|
||||
self.assertEqual(alias_value.body, "from-alias-put")
|
||||
alias_collection = node.get_all(alias)
|
||||
self.assertEqual(len(alias_collection), 1)
|
||||
self.assertIsInstance(alias_collection[0], UiNote)
|
||||
self.assertEqual(alias_collection[0].body, "from-alias-put")
|
||||
self.assertEqual(seen[0].document.type, document.type)
|
||||
self.assertIsInstance(seen[0].value, CanonicalNote)
|
||||
self.assertEqual(seen[0].value.body, "from-alias-put")
|
||||
|
|
@ -2151,6 +2155,7 @@ class CultCacheTests(unittest.TestCase):
|
|||
delete = node.delete_raw_message(alias, "note:alias", message_id="alias-delete").to_wire()
|
||||
self.assertEqual(delete["schemaId"], "mesh.alias_note.v1")
|
||||
self.assertIsNone(node.get(alias, "note:alias"))
|
||||
self.assertEqual(node.get_all(alias), [])
|
||||
|
||||
def test_cultnet_document_delete_helper_matches_schema_v0_shape(self) -> None:
|
||||
message = document_delete(
|
||||
|
|
@ -3093,6 +3098,55 @@ class CultCacheTests(unittest.TestCase):
|
|||
self.assertEqual([change.record_key for change in index_changes], ["item:1", "item:1"])
|
||||
self.assertEqual(index_changes[1].value, {"name": "Elixir", "kind": "rare"})
|
||||
|
||||
def test_cultmesh_database_collection_reads_resolve_same_schema_aliases(self) -> None:
|
||||
@dataclass
|
||||
class CanonicalItem:
|
||||
name: str
|
||||
kind: str
|
||||
|
||||
@dataclass
|
||||
class UiItem:
|
||||
name: str
|
||||
kind: str
|
||||
|
||||
document = define_database_entry_type(
|
||||
"mesh.alias_collection_item",
|
||||
[("name", 0), ("kind", 1)],
|
||||
cls=CanonicalItem,
|
||||
name="name",
|
||||
indexes={"kind": "kind"},
|
||||
schema_id="mesh.alias_collection_item.v1",
|
||||
schema_name="mesh.alias_collection_item",
|
||||
schema_version="mesh.alias_collection_item.v1",
|
||||
)
|
||||
alias = define_database_entry_type(
|
||||
"mesh.alias_collection_item.ui",
|
||||
[("name", 0), ("kind", 1)],
|
||||
cls=UiItem,
|
||||
name="name",
|
||||
indexes={"kind": "kind"},
|
||||
schema_id="mesh.alias_collection_item.v1",
|
||||
schema_name="mesh.alias_collection_item",
|
||||
schema_version="mesh.alias_collection_item.v1",
|
||||
)
|
||||
node = CultMesh.create_node(runtime_id="mesh-alias-collection")
|
||||
node.database.register_document(document)
|
||||
|
||||
node.database.put(alias, "item:potion", UiItem("Potion", "consumable"))
|
||||
node.database.put(document, "item:sword", CanonicalItem("Sword", "weapon"))
|
||||
|
||||
all_items = node.database.get_all(alias)
|
||||
potion = node.database.get_by_name(alias, "Potion")
|
||||
sword = node.get_by_index(alias, "kind", "weapon")
|
||||
|
||||
self.assertEqual([item.name for item in all_items], ["Potion", "Sword"])
|
||||
self.assertTrue(all(isinstance(item, UiItem) for item in all_items))
|
||||
self.assertIsInstance(potion, UiItem)
|
||||
self.assertEqual(potion.kind, "consumable")
|
||||
self.assertIsInstance(sword, UiItem)
|
||||
self.assertEqual(sword.name, "Sword")
|
||||
self.assertIsNone(node.database.get_by_name(alias, "Missing"))
|
||||
|
||||
def test_cultmesh_database_watch_by_name_and_index_validate_lookup_shape(self) -> None:
|
||||
document = define_database_entry_type("mesh.unnamed_watch", [("body", 0)])
|
||||
node = CultMesh.create_node(runtime_id="mesh-watch-validation")
|
||||
|
|
|
|||
Loading…
Reference in a new issue