Decode snapshot aliases from compatible payloads
This commit is contained in:
parent
defc416f9f
commit
b16f49665a
2 changed files with 115 additions and 4 deletions
|
|
@ -5,6 +5,7 @@ using System.Threading.Tasks;
|
|||
using GameCult.Caching;
|
||||
using GameCult.Caching.MessagePack;
|
||||
using GameCult.Networking;
|
||||
using MessagePack;
|
||||
|
||||
namespace GameCult.Mesh
|
||||
{
|
||||
|
|
@ -679,7 +680,9 @@ namespace GameCult.Mesh
|
|||
binding != null &&
|
||||
typeof(TDocument).IsAssignableFrom(binding.DocumentType);
|
||||
var canDeserializeAsSchemaAlias =
|
||||
string.Equals(record.SchemaId, descriptor.SchemaId, StringComparison.Ordinal);
|
||||
string.Equals(record.SchemaId, descriptor.SchemaId, StringComparison.Ordinal) ||
|
||||
(binding != null && IsSameCultDocumentSchema(binding.DocumentType, descriptor)) ||
|
||||
RawSnapshotPayloadMatchesSchema(record.Payload, descriptor);
|
||||
if (!canDeserializeWithBinding && !canDeserializeAsSchemaAlias)
|
||||
continue;
|
||||
|
||||
|
|
@ -689,12 +692,77 @@ namespace GameCult.Mesh
|
|||
$"CultNet raw document payloadEncoding must be \"messagepack\", not \"{record.PayloadEncoding}\".");
|
||||
}
|
||||
|
||||
documents.Add(canDeserializeWithBinding
|
||||
? (TDocument)binding!.PayloadDeserializer(record.Payload)
|
||||
: CultDocumentMessagePackSerialization.Deserialize<TDocument>(record.Payload));
|
||||
if (binding != null)
|
||||
{
|
||||
var decoded = binding.PayloadDeserializer(record.Payload);
|
||||
documents.Add(decoded is TDocument typed
|
||||
? typed
|
||||
: ConvertUntypedDocument<TDocument>(decoded));
|
||||
}
|
||||
else
|
||||
{
|
||||
documents.Add(CultDocumentMessagePackSerialization.Deserialize<TDocument>(record.Payload));
|
||||
}
|
||||
}
|
||||
|
||||
return documents;
|
||||
}
|
||||
|
||||
private static bool RawSnapshotPayloadMatchesSchema(
|
||||
byte[] payload,
|
||||
CultDocumentDescriptor descriptor)
|
||||
{
|
||||
var schemaVersion = TryReadSchemaVersion(payload);
|
||||
if (string.IsNullOrWhiteSpace(schemaVersion))
|
||||
return false;
|
||||
|
||||
if (string.Equals(schemaVersion, descriptor.SchemaVersion, StringComparison.Ordinal))
|
||||
return true;
|
||||
|
||||
var schemaName = InferSchemaName(schemaVersion!);
|
||||
return !string.IsNullOrWhiteSpace(schemaName) &&
|
||||
string.Equals(schemaName, descriptor.SchemaName, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
private static string? TryReadSchemaVersion(byte[] payload)
|
||||
{
|
||||
try
|
||||
{
|
||||
var array = MessagePackSerializer.Deserialize<object[]>(payload, CultNetSchemaMessageSerialization.Options);
|
||||
if (array.Length > 0 && array[0] is string schemaVersion)
|
||||
return schemaVersion;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// Fall through to map decoding; different runtimes may encode object-like payloads.
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var map = MessagePackSerializer.Deserialize<IReadOnlyDictionary<string, object?>>(payload, CultNetSchemaMessageSerialization.Options);
|
||||
if (map.TryGetValue("schemaVersion", out var schemaVersion) && schemaVersion is string schemaVersionText)
|
||||
return schemaVersionText;
|
||||
if (map.TryGetValue("schema_version", out var snakeSchemaVersion) && snakeSchemaVersion is string snakeSchemaVersionText)
|
||||
return snakeSchemaVersionText;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static string? InferSchemaName(string schemaVersion)
|
||||
{
|
||||
var marker = schemaVersion.LastIndexOf(".v", StringComparison.Ordinal);
|
||||
if (marker <= 0 || marker + 2 >= schemaVersion.Length)
|
||||
return null;
|
||||
|
||||
var version = schemaVersion.Substring(marker + 2);
|
||||
return version.All(char.IsDigit)
|
||||
? schemaVersion.Substring(0, marker)
|
||||
: null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2322,6 +2322,49 @@ public sealed class CultMeshStreamingTests
|
|||
request.MessageId.StartsWith("mesh-test-scoped-snapshot:", StringComparison.Ordinal));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task SnapshotHelpers_DecodeSameSchemaAliasFromForeignSchemaId()
|
||||
{
|
||||
var key = new CultRecordKey("mesh-note:snapshot-alias-foreign-schema");
|
||||
var snapshot = new CultNetSnapshotResponseRawMessage
|
||||
{
|
||||
MessageId = "foreign-schema",
|
||||
Documents = new[]
|
||||
{
|
||||
new CultNetRawDocumentRecord
|
||||
{
|
||||
SchemaId = "runtime.generated.mesh-note.ui.42",
|
||||
RecordKey = key.Value,
|
||||
StoredAt = DateTimeOffset.UtcNow.ToString("O"),
|
||||
PayloadEncoding = "messagepack",
|
||||
Payload = CultDocumentMessagePackSerialization.SerializeUntyped(
|
||||
new MeshNoteDocument
|
||||
{
|
||||
Schema = "tests.mesh_note.v1",
|
||||
Text = "foreign-schema-alias",
|
||||
Revision = 44
|
||||
},
|
||||
typeof(MeshNoteDocument))
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var documents = await CultMesh.FetchSnapshotDocumentsAsync<MeshNoteAliasDocument>(
|
||||
"cultnet://foreign-schema.test:3075",
|
||||
new CultMeshSnapshotRequestOptions
|
||||
{
|
||||
RecordKeys = new[] { key.Value },
|
||||
CreateClient = () => new MeshSnapshotSchemaClient(request =>
|
||||
{
|
||||
snapshot.MessageId = request.MessageId;
|
||||
return snapshot;
|
||||
})
|
||||
},
|
||||
new CultNetDocumentRegistry());
|
||||
|
||||
documents.Should().ContainSingle().Which.Text.Should().Be("foreign-schema-alias");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DocumentRegistryHelpers_CreateTypedNetworkRegistriesForAliases()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue