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
|
|
@ -3,16 +3,19 @@ using MessagePack.Formatters;
|
|||
|
||||
namespace GameCult.Caching;
|
||||
|
||||
// An unset reference (the empty key) is nil on the wire and default(CultRecordRef<T>) in memory; a "" read from older
|
||||
// stores is the same unset reference, so load then save is byte-stable.
|
||||
public sealed class CultRecordRefFormatter<T> : IMessagePackFormatter<CultRecordRef<T>>
|
||||
{
|
||||
public void Serialize(ref MessagePackWriter writer, CultRecordRef<T> value, MessagePackSerializerOptions options)
|
||||
{
|
||||
options.Resolver.GetFormatterWithVerify<string>().Serialize(ref writer, value.Key.Value, options);
|
||||
if (value.Key.Value.Length == 0) writer.WriteNil();
|
||||
else writer.Write(value.Key.Value);
|
||||
}
|
||||
|
||||
public CultRecordRef<T> Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
|
||||
{
|
||||
var key = options.Resolver.GetFormatterWithVerify<string>().Deserialize(ref reader, options) ?? string.Empty;
|
||||
return new CultRecordRef<T>(new CultRecordKey(key));
|
||||
var key = reader.TryReadNil() ? null : reader.ReadString();
|
||||
return string.IsNullOrEmpty(key) ? default : new CultRecordRef<T>(new CultRecordKey(key!));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue