Collapse the ref-candidate predicate and read inherited member attributes
RecordCandidates and the new IsRecordCandidate share one rule, so a picker's candidates and a drop's acceptance cannot disagree. Member attributes are read with Attribute.GetCustomAttributes(member, true): PropertyInfo.GetCustomAttributes ignores inherit, so a base abstract property's inspector attributes never reached the override. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
parent
4a4e212d9c
commit
a6382475e4
2 changed files with 67 additions and 6 deletions
|
|
@ -33,7 +33,7 @@ namespace GameCult.Caching
|
|||
|
||||
internal CultInspectorMetadata(MemberInfo? member)
|
||||
{
|
||||
Attributes = member == null ? Array.Empty<Attribute>() : member.GetCustomAttributes(true).OfType<Attribute>().ToArray();
|
||||
Attributes = member == null ? Array.Empty<Attribute>() : Attribute.GetCustomAttributes(member, true);
|
||||
var label = Find<CultInspectorLabelAttribute>()?.Label;
|
||||
Label = string.IsNullOrWhiteSpace(label) ? null : label;
|
||||
Hidden = Find<CultInspectorHiddenAttribute>() != null;
|
||||
|
|
@ -206,7 +206,7 @@ namespace GameCult.Caching
|
|||
private CultInspectorClaim ResolveCore(Type valueType, MemberInfo? member)
|
||||
{
|
||||
Type? drawer = null;
|
||||
foreach (var attribute in member?.GetCustomAttributes(true).OfType<Attribute>() ?? Enumerable.Empty<Attribute>())
|
||||
foreach (var attribute in member == null ? Array.Empty<Attribute>() : Attribute.GetCustomAttributes(member, true))
|
||||
{
|
||||
for (var type = attribute.GetType(); type != null && type != typeof(Attribute); type = type.BaseType)
|
||||
{
|
||||
|
|
@ -513,14 +513,24 @@ namespace GameCult.Caching
|
|||
// The records a CultRecordRef<T> may point at: every record whose document is a T, by label.
|
||||
public IReadOnlyList<CultStoredDocument> RecordCandidates(Type recordRefType, IEnumerable<CultStoredDocument> records)
|
||||
{
|
||||
var target = ShapeOf(recordRefType).RecordTarget
|
||||
?? throw new ArgumentException($"{recordRefType.Name} is not a CultRecordRef<T>.", nameof(recordRefType));
|
||||
var target = RecordTargetOf(recordRefType);
|
||||
return records
|
||||
.Where(record => target.IsInstanceOfType(record.Document) && record.Key.Value.Length > 0)
|
||||
.Where(record => IsCandidateFor(target, record))
|
||||
.OrderBy(RecordLabel, StringComparer.OrdinalIgnoreCase)
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
// Whether record may be the value of a recordRefType: the one rule the picker's candidates and a drop share.
|
||||
public bool IsRecordCandidate(Type recordRefType, CultStoredDocument record) =>
|
||||
IsCandidateFor(RecordTargetOf(recordRefType), record ?? throw new ArgumentNullException(nameof(record)));
|
||||
|
||||
private Type RecordTargetOf(Type recordRefType) =>
|
||||
ShapeOf(recordRefType).RecordTarget
|
||||
?? throw new ArgumentException($"{recordRefType.Name} is not a CultRecordRef<T>.", nameof(recordRefType));
|
||||
|
||||
private static bool IsCandidateFor(Type target, CultStoredDocument record) =>
|
||||
target.IsInstanceOfType(record.Document) && record.Key.Value.Length > 0;
|
||||
|
||||
public object CreateRecordRef(Type recordRefType, string key)
|
||||
{
|
||||
if (ShapeOf(recordRefType).Kind != CultInspectorValueKind.RecordRef)
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ namespace GameCult.Caching.Tests
|
|||
public class CultInspectorModelTests
|
||||
{
|
||||
private static readonly CultDocumentRegistry Registry =
|
||||
CultDocumentRegistry.ForTypes(new[] { typeof(InspectItem), typeof(InspectOther), typeof(InspectFixed) });
|
||||
CultDocumentRegistry.ForTypes(new[] { typeof(InspectItem), typeof(InspectOther), typeof(InspectFixed), typeof(InspectLabeled) });
|
||||
|
||||
private static CultInspectorModel Model() => CultCacheMessagePack.CreateInspectorModel(Registry);
|
||||
|
||||
|
|
@ -128,6 +128,35 @@ namespace GameCult.Caching.Tests
|
|||
Assert.That(CultInspectorModel.RecordKey(new CultRecordRef<InspectItem>(a)), Is.EqualTo(a.Value));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void OneRuleDecidesWhichRecordsMayBeARefsValue()
|
||||
{
|
||||
var model = Model();
|
||||
CultStoredDocument Record(string key, object document) =>
|
||||
new(new CultRecordKey(key), "test", Registry.GetRequired(document.GetType()), document);
|
||||
var leaf = Record("leaf", new InspectLabeled { Name = "a" });
|
||||
var sibling = Record("other", new InspectOther { Name = "b" });
|
||||
var unkeyed = Record("", new InspectLabeled { Name = "c" });
|
||||
var records = new[] { sibling, leaf, unkeyed };
|
||||
var baseRef = typeof(CultRecordRef<InspectLabeledBase>);
|
||||
|
||||
Assert.That(model.IsRecordCandidate(baseRef, leaf), Is.True, "a subtype of the target");
|
||||
Assert.That(model.IsRecordCandidate(baseRef, sibling), Is.False, "a sibling type");
|
||||
Assert.That(model.IsRecordCandidate(baseRef, unkeyed), Is.False, "an empty key");
|
||||
Assert.That(model.RecordCandidates(baseRef, records), Is.EqualTo(records.Where(record => model.IsRecordCandidate(baseRef, record))));
|
||||
Assert.That(() => model.IsRecordCandidate(typeof(string), leaf), Throws.ArgumentException);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ABaseMembersLabelReachesItsOverride()
|
||||
{
|
||||
var member = Model().MembersOf(typeof(InspectLabeled)).Single(candidate => candidate.Name == nameof(InspectLabeled.Tag));
|
||||
|
||||
Assert.That(member.Metadata.Label, Is.EqualTo("Base Label"));
|
||||
var claims = new CultInspectorDrawerClaims(new[] { typeof(LabelClaimDrawer) }, typeof(IFakeDrawer));
|
||||
Assert.That(claims.Resolve(typeof(string), member.Member).Drawer, Is.EqualTo(typeof(LabelClaimDrawer)), "attribute claims see the base attribute too");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DictionaryKeysRefuseNullEmptyReferencesAndDuplicates()
|
||||
{
|
||||
|
|
@ -330,6 +359,28 @@ namespace GameCult.Caching.Tests
|
|||
public string Name = string.Empty;
|
||||
}
|
||||
|
||||
public abstract class InspectLabeledBase
|
||||
{
|
||||
[CultInspectorLabel("Base Label")]
|
||||
public abstract string Tag { get; set; }
|
||||
}
|
||||
|
||||
[CultDocument("tests.inspect_labeled", "tests.inspect_labeled.v1")]
|
||||
[MessagePackObject]
|
||||
public sealed class InspectLabeled : InspectLabeledBase
|
||||
{
|
||||
[Key(0)] [CultName]
|
||||
public string Name = string.Empty;
|
||||
|
||||
[Key(1)]
|
||||
public override string Tag { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
[CultInspectorDrawer(typeof(CultInspectorLabelAttribute))]
|
||||
public sealed class LabelClaimDrawer : IFakeDrawer
|
||||
{
|
||||
}
|
||||
|
||||
[CultDocument("tests.inspect_asset", "tests.inspect_asset.v1")]
|
||||
[MessagePackObject]
|
||||
public sealed class InspectAsset
|
||||
|
|
|
|||
Loading…
Reference in a new issue