Convert Python node aliases through payload schema
This commit is contained in:
parent
a2ec06f90e
commit
defc416f9f
2 changed files with 32 additions and 10 deletions
|
|
@ -334,7 +334,7 @@ class CultMeshDatabase:
|
|||
|
||||
def put(self, document: DocumentDefinition[Any], key: str, value: Any) -> None:
|
||||
registered = self._resolve_document_alias(document)
|
||||
parsed = document.decode_payload(document.encode_payload(value))
|
||||
parsed = self._convert_document_value(value, document, registered)
|
||||
previous = self.cache.get(registered, key)
|
||||
self.cache.put(registered, key, parsed)
|
||||
self._publish_local_change(registered, key, "added" if previous is None else "updated", parsed, previous)
|
||||
|
|
@ -353,7 +353,7 @@ class CultMeshDatabase:
|
|||
shard_epoch: int | None = None,
|
||||
) -> CultNetMessage:
|
||||
registered = self._resolve_document_alias(document)
|
||||
parsed = document.decode_payload(document.encode_payload(value))
|
||||
parsed = self._convert_document_value(value, document, registered)
|
||||
catalog_entry = registered.catalog_entry()
|
||||
previous = self.cache.get(registered, key)
|
||||
envelope = CultCacheEnvelope.create(
|
||||
|
|
@ -379,8 +379,9 @@ class CultMeshDatabase:
|
|||
return message
|
||||
|
||||
def get(self, document: DocumentDefinition[Any], key: str) -> Any:
|
||||
value = self.cache.get(self._resolve_document_alias(document), key)
|
||||
return None if value is None else document.decode_payload(document.encode_payload(value))
|
||||
registered = self._resolve_document_alias(document)
|
||||
value = self.cache.get(registered, key)
|
||||
return None if value is None else self._convert_document_value(value, registered, document)
|
||||
|
||||
def get_required(self, document: DocumentDefinition[Any], key: str) -> Any:
|
||||
value = self.get(document, key)
|
||||
|
|
@ -848,6 +849,14 @@ class CultMeshDatabase:
|
|||
return registered
|
||||
return document
|
||||
|
||||
def _convert_document_value(
|
||||
self,
|
||||
value: Any,
|
||||
source: DocumentDefinition[Any],
|
||||
target: DocumentDefinition[Any],
|
||||
) -> Any:
|
||||
return target.decode_payload(source.encode_payload(value))
|
||||
|
||||
def _documents_alias(
|
||||
self,
|
||||
left: DocumentDefinition[Any],
|
||||
|
|
|
|||
|
|
@ -2020,9 +2020,18 @@ class CultCacheTests(unittest.TestCase):
|
|||
self.assertEqual(delete["shardEpoch"], 1)
|
||||
|
||||
def test_cultmesh_node_resolves_same_schema_aliases_for_local_state(self) -> None:
|
||||
@dataclass
|
||||
class CanonicalNote:
|
||||
body: str
|
||||
|
||||
@dataclass
|
||||
class UiNote:
|
||||
body: str
|
||||
|
||||
document = define_database_entry_type(
|
||||
"mesh.alias_note",
|
||||
[("body", 0)],
|
||||
cls=CanonicalNote,
|
||||
schema_id="mesh.alias_note.v1",
|
||||
schema_name="mesh.alias_note",
|
||||
schema_version="mesh.alias_note.v1",
|
||||
|
|
@ -2030,6 +2039,7 @@ class CultCacheTests(unittest.TestCase):
|
|||
alias = define_database_entry_type(
|
||||
"mesh.alias_note.ui",
|
||||
[("body", 0)],
|
||||
cls=UiNote,
|
||||
schema_id="mesh.alias_note.v1",
|
||||
schema_name="mesh.alias_note",
|
||||
schema_version="mesh.alias_note.v1",
|
||||
|
|
@ -2039,21 +2049,24 @@ class CultCacheTests(unittest.TestCase):
|
|||
seen: list[CultMeshDatabaseChange] = []
|
||||
node.database.watch_record(alias, "note:alias", seen.append)
|
||||
|
||||
node.put(alias, "note:alias", {"body": "from-alias-put"})
|
||||
self.assertEqual(node.get_required(document, "note:alias")["body"], "from-alias-put")
|
||||
self.assertEqual(node.get_required(alias, "note:alias")["body"], "from-alias-put")
|
||||
node.put(alias, "note:alias", UiNote("from-alias-put"))
|
||||
self.assertEqual(node.get_required(document, "note:alias").body, "from-alias-put")
|
||||
alias_value = node.get_required(alias, "note:alias")
|
||||
self.assertIsInstance(alias_value, UiNote)
|
||||
self.assertEqual(alias_value.body, "from-alias-put")
|
||||
self.assertEqual(seen[0].document.type, document.type)
|
||||
self.assertEqual(seen[0].value["body"], "from-alias-put")
|
||||
self.assertIsInstance(seen[0].value, CanonicalNote)
|
||||
self.assertEqual(seen[0].value.body, "from-alias-put")
|
||||
|
||||
put = node.put_raw_message(
|
||||
alias,
|
||||
"note:alias",
|
||||
{"body": "from-alias-raw-put"},
|
||||
UiNote("from-alias-raw-put"),
|
||||
message_id="alias-put",
|
||||
).to_wire()
|
||||
self.assertEqual(put["document"]["schemaId"], "mesh.alias_note.v1")
|
||||
self.assertEqual(put["document"]["recordKey"], "note:alias")
|
||||
self.assertEqual(node.get_required(alias, "note:alias")["body"], "from-alias-raw-put")
|
||||
self.assertEqual(node.get_required(alias, "note:alias").body, "from-alias-raw-put")
|
||||
|
||||
delete = node.delete_raw_message(alias, "note:alias", message_id="alias-delete").to_wire()
|
||||
self.assertEqual(delete["schemaId"], "mesh.alias_note.v1")
|
||||
|
|
|
|||
Loading…
Reference in a new issue