Prefer typed peer snapshot aliases by record key
This commit is contained in:
parent
a3d957dad2
commit
6218900734
2 changed files with 72 additions and 0 deletions
|
|
@ -2316,6 +2316,9 @@ namespace GameCult.Mesh
|
|||
var record = response.Documents.FirstOrDefault(candidate =>
|
||||
string.Equals(candidate.SchemaId, schemaId, StringComparison.Ordinal) &&
|
||||
string.Equals(candidate.RecordKey, recordKey, StringComparison.Ordinal))
|
||||
?? response.Documents.FirstOrDefault(candidate =>
|
||||
string.Equals(candidate.RecordKey, recordKey, StringComparison.Ordinal) &&
|
||||
TryDecodeSnapshotDocument(candidate, out TDocument? _))
|
||||
?? response.Documents.FirstOrDefault(candidate =>
|
||||
string.Equals(candidate.SchemaId, schemaId, StringComparison.Ordinal))
|
||||
?? response.Documents.FirstOrDefault(candidate =>
|
||||
|
|
@ -2332,6 +2335,32 @@ namespace GameCult.Mesh
|
|||
$"CultNet raw document payloadEncoding must be \"messagepack\", not \"{record.PayloadEncoding}\".");
|
||||
}
|
||||
|
||||
return DecodeSnapshotDocument<TDocument>(record);
|
||||
}
|
||||
|
||||
private static bool TryDecodeSnapshotDocument<TDocument>(
|
||||
CultNetRawDocumentRecord record,
|
||||
out TDocument? document)
|
||||
where TDocument : class
|
||||
{
|
||||
document = null;
|
||||
if (!string.Equals(record.PayloadEncoding, "messagepack", StringComparison.Ordinal))
|
||||
return false;
|
||||
|
||||
try
|
||||
{
|
||||
document = DecodeSnapshotDocument<TDocument>(record);
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static TDocument DecodeSnapshotDocument<TDocument>(CultNetRawDocumentRecord record)
|
||||
where TDocument : class
|
||||
{
|
||||
return (TDocument)CultDocumentMessagePackSerialization.DeserializeUntyped(
|
||||
typeof(TDocument),
|
||||
record.Payload);
|
||||
|
|
|
|||
|
|
@ -2260,6 +2260,49 @@ public sealed class CultMeshStreamingTests
|
|||
CultMesh.Verse("starbridge", "unity-pilot"));
|
||||
|
||||
(await aliasSchemaHandle.LatestAsync()).Text.Should().Be("record-key-fallback");
|
||||
|
||||
var mixedAliasSchemaHandle = CultMesh.DocumentFromPeerSnapshot<MeshNoteDocument>(
|
||||
_ => Task.FromResult(new CultNetSnapshotResponseRawMessage
|
||||
{
|
||||
MessageId = "alias-schema-with-neighbor",
|
||||
Documents = new[]
|
||||
{
|
||||
new CultNetRawDocumentRecord
|
||||
{
|
||||
SchemaId = CultDocumentRegistry.Shared.GetRequired<MeshNoteDocument>().SchemaId,
|
||||
RecordKey = "mesh-note:other",
|
||||
StoredAt = DateTimeOffset.UtcNow.ToString("O"),
|
||||
PayloadEncoding = "messagepack",
|
||||
Payload = CultDocumentMessagePackSerialization.SerializeUntyped(
|
||||
new MeshNoteDocument
|
||||
{
|
||||
Schema = "tests.mesh_note.v1",
|
||||
Text = "wrong-schema-neighbor",
|
||||
Revision = 11
|
||||
},
|
||||
typeof(MeshNoteDocument))
|
||||
},
|
||||
new CultNetRawDocumentRecord
|
||||
{
|
||||
SchemaId = "remote.generated.mesh_note_alias.v100",
|
||||
RecordKey = key.Value,
|
||||
StoredAt = DateTimeOffset.UtcNow.ToString("O"),
|
||||
PayloadEncoding = "messagepack",
|
||||
Payload = CultDocumentMessagePackSerialization.SerializeUntyped(
|
||||
new MeshNoteDocument
|
||||
{
|
||||
Schema = "tests.mesh_note.v1",
|
||||
Text = "compatible-record-key",
|
||||
Revision = 12
|
||||
},
|
||||
typeof(MeshNoteDocument))
|
||||
}
|
||||
}
|
||||
}),
|
||||
key.Value,
|
||||
CultMesh.Verse("starbridge", "unity-pilot"));
|
||||
|
||||
(await mixedAliasSchemaHandle.LatestAsync()).Text.Should().Be("compatible-record-key");
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
|
|
|||
Loading…
Reference in a new issue