Skip to content

Commit afbfe14

Browse files
author
Joanna May
authored
Merge pull request #4 from chickensoft-games/feat/enumerable-id-types
feat: enumerate all id types
2 parents 47c30b3 + c356f7c commit afbfe14

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

Chickensoft.Introspection.Tests/test/TypeGraphTest.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ public partial class SubtypeC : Model { }
2828
[Meta]
2929
public partial class NoSubtypes { }
3030

31+
[Fact]
32+
public void EnumeratesRegisteredTypes() {
33+
Types.Graph.IdentifiableTypes.ShouldContain(typeof(Model));
34+
Types.Graph.IdentifiableTypes.ShouldContain(typeof(SubtypeA));
35+
Types.Graph.IdentifiableTypes.ShouldContain(typeof(SubtypeA.SubtypeB));
36+
Types.Graph.IdentifiableTypes.ShouldContain(typeof(SubtypeC));
37+
38+
// Not an identifiable type.
39+
Types.Graph.IdentifiableTypes.ShouldNotContain(typeof(NoSubtypes));
40+
}
41+
3142
[Fact]
3243
public void GetIdentifiableType() =>
3344
Types.Graph.GetIdentifiableType("test_model").ShouldBe(typeof(Model));

Chickensoft.Introspection/src/TypeGraph.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ namespace Chickensoft.Introspection;
1010
internal class TypeGraph : ITypeGraph {
1111
#region Caches
1212
private readonly ConcurrentDictionary<Type, ITypeMetadata> _types = new();
13-
private readonly ConcurrentDictionary<Type, IIdentifiableTypeMetadata>
14-
_identifiableTypes = new();
1513
private readonly ConcurrentDictionary<string, Dictionary<int, Type>>
1614
_identifiableTypesByIdAndVersion = new();
1715
private readonly ConcurrentDictionary<string, int>
@@ -34,7 +32,6 @@ private readonly ConcurrentDictionary<Type, Dictionary<Type, Attribute[]>>
3432

3533
internal void Reset() {
3634
_types.Clear();
37-
_identifiableTypes.Clear();
3835
_identifiableTypesByIdAndVersion.Clear();
3936
_identifiableLatestVersionsById.Clear();
4037
_typesByBaseType.Clear();
@@ -44,6 +41,10 @@ internal void Reset() {
4441
}
4542

4643
#region ITypeGraph
44+
public IEnumerable<Type> IdentifiableTypes => _identifiableTypesByIdAndVersion
45+
.Values
46+
.SelectMany(versions => versions.Values);
47+
4748
public void Register(ITypeRegistry registry) {
4849
RegisterTypes(registry);
4950
PromoteInheritedIdentifiableTypes(registry);
@@ -120,9 +121,15 @@ _types[type] is not IIntrospectiveTypeMetadata metadata
120121
.Select(type => _types[type])
121122
.OfType<IIntrospectiveTypeMetadata>()
122123
.SelectMany((metadata) => metadata.Metatype.Attributes)
124+
.GroupBy(
125+
kvp => kvp.Key,
126+
kvp => kvp.Value
127+
)
123128
.ToDictionary(
124-
keySelector: (typeToAttr) => typeToAttr.Key,
125-
elementSelector: (typeToAttr) => typeToAttr.Value
129+
group => group.Key,
130+
elementSelector: group => group
131+
.SelectMany(attributes => attributes)
132+
.ToArray()
126133
);
127134
}
128135
return _attributes[type];

Chickensoft.Introspection/src/types/ITypeGraph.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ namespace Chickensoft.Introspection;
1111
/// registered the types are represented in the graph, including nested types.
1212
/// </summary>
1313
public interface ITypeGraph {
14+
/// <summary>
15+
/// Enumerable of all registered (or promoted) identifiable types.
16+
/// </summary>
17+
IEnumerable<Type> IdentifiableTypes { get; }
18+
1419
/// <summary>
1520
/// Computes and caches secondary type lookup tables based on the types
1621
/// registered in the provided type registry.

0 commit comments

Comments
 (0)