Show unkeyed structs by their writable surface, not a lock rule

The unkeyed struct rule found auto-properties by the compiler's
<Name>k__BackingField name and marked the whole struct Unsupported when
one was get-only or a field was readonly. Record structs fell out with a
generic reason, and settable or init properties beside a field were
silently hidden. Editing is in place, so the lock protected nothing.

An unkeyed struct now shows its public fields (readonly ones read-only)
and its public properties that have a setter (init-only ones read-only).
Get-only properties are computed or kept by the in-place edit, so they are
not rows; vector swizzles stay out of the inspector.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-14 14:34:56 +02:00
commit 7d59021b57
3 changed files with 35 additions and 26 deletions

View file

@ -535,21 +535,20 @@ namespace GameCult.Caching
var keyed = KeyedMembers(type);
if (keyed.Length > 0)
return new CultInspectorShape(type, CultInspectorValueKind.Nested) { Members = keyed };
// An unkeyed struct is edited in place through its public fields. State it cannot write in place (a readonly field,
// a get-only auto-property) makes it unsupported until a drawer claims it.
// An unkeyed struct is edited in place: its public fields, readonly ones read-only, then its public properties with
// a setter, init-only ones read-only. Get-only properties are computed (a vector's swizzles) or kept by the
// in-place edit, so they are not rows.
if (type.IsValueType)
{
var locked = type.GetFields(Public).FirstOrDefault(field => field.IsInitOnly)?.Name
?? type.GetProperties(Public).FirstOrDefault(property => property.GetSetMethod() == null &&
type.GetField($"<{property.Name}>k__BackingField", BindingFlags.NonPublic | BindingFlags.Instance) != null)?.Name;
if (locked != null)
return Unsupported(type, $"{type.Name}.{locked} is readonly or get-only, so {type.Name} cannot be edited in place; it needs a drawer.");
var fields = type.GetFields(Public)
.OrderBy(field => field.MetadataToken)
.Select((field, slot) => new CultInspectorMember(field, field.FieldType, slot, true, MetadataOf(field)))
var members = type.GetFields(Public).OrderBy(field => field.MetadataToken)
.Select(field => (Member: (MemberInfo)field, Type: field.FieldType, Assignable: !field.IsInitOnly))
.Concat(type.GetProperties(Public).OrderBy(property => property.MetadataToken)
.Where(property => property.GetIndexParameters().Length == 0 && property.GetGetMethod() != null && property.GetSetMethod() != null)
.Select(property => (Member: (MemberInfo)property, Type: property.PropertyType, Assignable: !IsInitOnly(property.GetSetMethod()!))))
.Select((member, slot) => new CultInspectorMember(member.Member, member.Type, slot, member.Assignable, MetadataOf(member.Member)))
.ToArray();
if (fields.Length > 0)
return new CultInspectorShape(type, CultInspectorValueKind.Nested) { Members = fields };
if (members.Length > 0)
return new CultInspectorShape(type, CultInspectorValueKind.Nested) { Members = members };
}
return Unsupported(type, "no drawer for " + type.FullName);
@ -592,6 +591,11 @@ namespace GameCult.Caching
private static int Ancestry(Type? type) => type == null ? 0 : 1 + Ancestry(type.BaseType);
// `init` is a setter whose return carries the IsExternalInit modreq; the language contract, matched by name because
// netstandard2.1 does not ship the type.
private static bool IsInitOnly(MethodInfo setter) =>
setter.ReturnParameter.GetRequiredCustomModifiers().Any(modifier => modifier.FullName == "System.Runtime.CompilerServices.IsExternalInit");
private static Type TypeOf(MemberInfo member) => member is FieldInfo field ? field.FieldType : ((PropertyInfo)member).PropertyType;
private static bool Assignable(MemberInfo member, bool allowPrivate)

View file

@ -45,7 +45,7 @@ catalog slots, `CultInspector*` metadata, whether a member is assignable), the
shape of any value (string, integer, float, bool, enum, `CultRecordRef<T>` with
its candidate records, rank-1 list or array, dictionary, `[Union]` with only
its declared subtypes, nested keyed object, struct edited in place through its
public fields, or unsupported with a reason), drawer claim resolution
public fields and settable properties, or unsupported with a reason), drawer claim resolution
(`CultInspectorDrawerClaims`), and the edit decisions: the key a dictionary
entry keeps and the notice when a key is refused (null, an empty record
reference, or a duplicate: serialized the same as another key or equal by the
@ -59,8 +59,9 @@ renderer and drawers to show and edit the same documents under the same rules.
## Drawers
Built in: every model shape above. A struct without `[Key]` members folds out
to its public fields and is edited in place; a struct with a readonly field or
a get-only auto-property is unsupported until a drawer claims it. Unity object
to its public fields and its public properties with a setter, and is edited in
place; readonly fields and `init` properties show read-only, and get-only
properties are not shown (record structs work as written). Unity object
references draw as object fields. A value no drawer claims and no shape covers
shows a red error row with the model's reason, as does a multi-dimensional
array.

View file

@ -231,11 +231,14 @@ namespace GameCult.Caching.Tests
Assert.That(open.Kind, Is.EqualTo(CultInspectorValueKind.Nested));
Assert.That(open.Members.Select(member => member.Name), Is.EqualTo(new[] { "x", "y" }));
Assert.That(open.Members.All(member => member.IsAssignable), Is.True);
foreach (var (locked, member) in new[] { (typeof(InspectReadonlyField), "y"), (typeof(InspectGetOnly), "Id") })
{
Assert.That(model.ShapeOf(locked).Kind, Is.EqualTo(CultInspectorValueKind.Unsupported), locked.Name);
Assert.That(model.ShapeOf(locked).Reason, Does.Contain(locked.Name + "." + member).And.Contain("readonly or get-only"));
}
(string, bool)[] Rows(Type type) => model.ShapeOf(type).Members.Select(member => (member.Name, member.IsReadOnly)).ToArray();
Assert.That(Rows(typeof(InspectMixedStruct)), Is.EqualTo(new[] { ("x", false), ("y", true), ("Settable", false), ("Initable", true) }),
"fields, then settable properties; readonly and init are read-only; get-only is not a row");
Assert.That(Rows(typeof(InspectRecordStruct)), Is.EqualTo(new[] { ("A", false), ("B", false) }));
Assert.That(Rows(typeof(InspectReadonlyRecordStruct)), Is.EqualTo(new[] { ("A", true), ("B", true) }));
object boxed = new InspectRecordStruct(1, 2f);
model.ShapeOf(typeof(InspectRecordStruct)).Members[0].SetValue(boxed, 5);
Assert.That(((InspectRecordStruct)boxed).A, Is.EqualTo(5), "a property edit lands in the boxed struct in place");
Assert.That(model.ShapeOf(typeof(IDictionary<string, int>)).Kind, Is.EqualTo(CultInspectorValueKind.Dictionary));
Assert.That(model.BuildDictionary(typeof(IDictionary<string, int>), new[] { new KeyValuePair<object?, object?>("a", 1) }),
@ -342,17 +345,18 @@ namespace GameCult.Caching.Tests
public float Length => x + y;
}
public struct InspectReadonlyField
public struct InspectMixedStruct
{
public float x;
public readonly float y;
public int Settable { get; set; }
public int Initable { get; init; }
public int Computed => Settable + 1;
}
public struct InspectGetOnly
{
public float x;
public int Id { get; }
}
public record struct InspectRecordStruct(int A, float B);
public readonly record struct InspectReadonlyRecordStruct(int A, float B);
[MessagePackObject]
public readonly record struct InspectKey([property: Key(0)] int Value)