Write an unset CultRecordRef as nil and read it back unset
The formatter wrote default(CultRecordRef<T>) as nil but read nil back as "", so re-upserting a loaded record changed its bytes (0xC0 became 0xA0). One in-memory unset value: CultRecordKey.Value is never null, so default(CultRecordRef<T>) equals a ref built from "". One wire form: an empty key writes nil, and nil or a legacy "" reads back as default. Load then save is byte-stable; a store holding "" converges to nil on its next save. Rust, TypeScript and Python have no reference type: a reference member is a string in the consumer's own payload, so there is no sibling codec to align. The store-composition contract now names nil as the one unset wire form. The new test loads a record with an unset ref through a fresh cache, re-upserts and flushes, and requires identical payload bytes; it fails on the old formatter. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
parent
0f2c1f04ef
commit
452f928bb7
5 changed files with 75 additions and 7 deletions
|
|
@ -38,6 +38,48 @@ namespace GameCult.Caching.Tests
|
|||
Assert.That(Convert.ToHexString(payload), Is.EqualTo(InteropNotePayloadHex));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task UnsetRefReloadsAndResavesByteIdentical()
|
||||
{
|
||||
var path = Path.Combine(Path.GetTempPath(), $"cultlib-unsetref-{Guid.NewGuid():N}.cc");
|
||||
var registry = CultDocumentRegistry.ForTypes(new[] { typeof(UnsetRefHolder) });
|
||||
byte[] Payload() => CultDocumentMessagePackSerialization.DeserializeSnapshot(File.ReadAllBytes(path)).Records.Single().Payload;
|
||||
try
|
||||
{
|
||||
using (var seed = new CultCache(registry))
|
||||
{
|
||||
seed.AddBackingStore(new SingleFileMessagePackBackingStore(path));
|
||||
await seed.UpsertAsync(typeof(UnsetRefHolder), new UnsetRefHolder { Name = "holder", Set = new CultRecordRef<UnsetRefHolder>(new CultRecordKey("other")) },
|
||||
new CultRecordKey("holder"));
|
||||
await seed.FlushAsync();
|
||||
}
|
||||
|
||||
var written = Payload();
|
||||
CultRecordRef<UnsetRefHolder> unset;
|
||||
using (var reopened = new CultCache(registry))
|
||||
{
|
||||
reopened.AddBackingStore(new SingleFileMessagePackBackingStore(path));
|
||||
await reopened.PullAllBackingStoresAsync();
|
||||
var loaded = reopened.Get<UnsetRefHolder>(new CultRecordKey("holder"))!;
|
||||
unset = loaded.Unset;
|
||||
await reopened.UpsertAsync(typeof(UnsetRefHolder), loaded, new CultRecordKey("holder"));
|
||||
await reopened.FlushAsync();
|
||||
}
|
||||
|
||||
Assert.That(Payload(), Is.EqualTo(written), "an unset reference loads and re-saves byte-identical");
|
||||
Assert.That(unset, Is.EqualTo(default(CultRecordRef<UnsetRefHolder>)));
|
||||
Assert.That(unset.Key, Is.EqualTo(new CultRecordKey("")), "default and \"\" are one empty key");
|
||||
var legacy = MessagePackSerializer.Serialize("", CultDocumentMessagePackSerialization.Options);
|
||||
var read = MessagePackSerializer.Deserialize<CultRecordRef<UnsetRefHolder>>(legacy, CultDocumentMessagePackSerialization.Options);
|
||||
Assert.That(MessagePackSerializer.Serialize(read, CultDocumentMessagePackSerialization.Options), Is.EqualTo(new[] { MessagePackCode.Nil }),
|
||||
"a \"\" reference reads as unset and writes nil");
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (File.Exists(path)) File.Delete(path);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task RefKeyedDictionaryRoundTrips()
|
||||
{
|
||||
|
|
@ -171,5 +213,14 @@ namespace GameCult.Caching.Tests
|
|||
[Key(0)] [CultName] public string Name = string.Empty;
|
||||
[Key(1)] public Dictionary<CultRecordRef<RefKeyedHolder>, float> Weights = new();
|
||||
}
|
||||
|
||||
[CultDocument("tests.unset_ref_holder", "tests.unset_ref_holder.v1")]
|
||||
[MessagePackObject]
|
||||
public sealed class UnsetRefHolder
|
||||
{
|
||||
[Key(0)] [CultName] public string Name = string.Empty;
|
||||
[Key(1)] public CultRecordRef<UnsetRefHolder> Unset;
|
||||
[Key(2)] public CultRecordRef<UnsetRefHolder> Set;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue