diff --git a/src/GameCult.Caching/CultInspectorModel.cs b/src/GameCult.Caching/CultInspectorModel.cs index 99d82205..c84698b4 100644 --- a/src/GameCult.Caching/CultInspectorModel.cs +++ b/src/GameCult.Caching/CultInspectorModel.cs @@ -33,7 +33,7 @@ namespace GameCult.Caching internal CultInspectorMetadata(MemberInfo? member) { - Attributes = member == null ? Array.Empty() : member.GetCustomAttributes(true).OfType().ToArray(); + Attributes = member == null ? Array.Empty() : Attribute.GetCustomAttributes(member, true); var label = Find()?.Label; Label = string.IsNullOrWhiteSpace(label) ? null : label; Hidden = Find() != 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() ?? Enumerable.Empty()) + foreach (var attribute in member == null ? Array.Empty() : 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 may point at: every record whose document is a T, by label. public IReadOnlyList RecordCandidates(Type recordRefType, IEnumerable records) { - var target = ShapeOf(recordRefType).RecordTarget - ?? throw new ArgumentException($"{recordRefType.Name} is not a CultRecordRef.", 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.", 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) diff --git a/tests/GameCult.Caching.Tests/CultInspectorModelTests.cs b/tests/GameCult.Caching.Tests/CultInspectorModelTests.cs index 5c2c171b..c0036ea4 100644 --- a/tests/GameCult.Caching.Tests/CultInspectorModelTests.cs +++ b/tests/GameCult.Caching.Tests/CultInspectorModelTests.cs @@ -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(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); + + 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