GameCult.Thing.Contract: every empty memory is an empty RecordPayload; wire nil is refused on read
Present(ReadOnlyMemory<byte>) is deleted. The constructor copies any view, default included, as an empty payload; a nil on the wire (which decodes to a default view) is noted by the constructor and refused in OnAfterDeserialize, so the nil-positions rows hold. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
686b32831b
commit
e25311a0c0
3 changed files with 43 additions and 15 deletions
|
|
@ -104,16 +104,32 @@ public sealed class CommandDecl
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A typed record as its MessagePack bytes. Every empty memory (<c>ReadOnlyMemory<byte>.Empty</c>, a default view,
|
||||
/// an empty array) is an empty payload, as Rust's empty <c>Vec<u8></c> is. A nil on the wire is not an empty
|
||||
/// payload: it decodes to a default view the constructor cannot tell from an empty one, so the constructor notes it and
|
||||
/// the decode door refuses it once the value is read.
|
||||
/// </summary>
|
||||
[MessagePackObject]
|
||||
public sealed class RecordPayload
|
||||
public sealed class RecordPayload : IMessagePackSerializationCallbackReceiver
|
||||
{
|
||||
private readonly bool _viewWasDefault;
|
||||
|
||||
[Key(0)] public string Schema { get; }
|
||||
[Key(1)] public ReadOnlyMemory<byte> Bytes { get; }
|
||||
|
||||
public RecordPayload(string schema, ReadOnlyMemory<byte> bytes)
|
||||
{
|
||||
Schema = ThingDomain.Present(schema, "Schema");
|
||||
Bytes = ThingDomain.Present(bytes, "Bytes").ToArray();
|
||||
_viewWasDefault = bytes.Equals(default(ReadOnlyMemory<byte>));
|
||||
Bytes = bytes.ToArray();
|
||||
}
|
||||
|
||||
void IMessagePackSerializationCallbackReceiver.OnBeforeSerialize() { }
|
||||
|
||||
void IMessagePackSerializationCallbackReceiver.OnAfterDeserialize()
|
||||
{
|
||||
if (_viewWasDefault) throw new ThingDomainException("Bytes", "present");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -49,15 +49,6 @@ public static class ThingDomain
|
|||
public static T Present<T>(T? value, string slot) where T : class =>
|
||||
value ?? throw new ThingDomainException(slot, "present");
|
||||
|
||||
/// <summary>
|
||||
/// A required byte slot. A null on the wire decodes to a default view with no backing store, so the default view is
|
||||
/// refused as nil; an empty array is a present, empty payload.
|
||||
/// </summary>
|
||||
public static ReadOnlyMemory<byte> Present(ReadOnlyMemory<byte> value, string slot) =>
|
||||
value.Equals(default(ReadOnlyMemory<byte>))
|
||||
? throw new ThingDomainException(slot, "present")
|
||||
: value;
|
||||
|
||||
/// <summary>
|
||||
/// A required list slot: a read-only copy, so the caller's list cannot change the value afterwards. A null list or
|
||||
/// a null item is refused naming the slot.
|
||||
|
|
|
|||
Loading…
Reference in a new issue