Skip to content

Commit

Permalink
fix: do not rely on dictionary key order since it is unstable
Browse files Browse the repository at this point in the history
  • Loading branch information
jolexxa committed Jun 4, 2024
1 parent d1b6721 commit 80facb5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void Equality() {
var registry = new DeclaredTypeRegistry(
globalUsings: ImmutableArray<UsingDirective>.Empty,
scopeTree: new ScopeTree(
new Dictionary<string, DeclaredType>()
[], new Dictionary<string, DeclaredType>()
),
allTypes: ImmutableDictionary<string, DeclaredType>.Empty,
visibleTypes: ImmutableHashSet<DeclaredType>.Empty
Expand All @@ -26,7 +26,7 @@ public void Equality() {
new DeclaredTypeRegistry(
globalUsings: ImmutableArray<UsingDirective>.Empty,
scopeTree: new ScopeTree(
new Dictionary<string, DeclaredType>()
[], new Dictionary<string, DeclaredType>()
),
allTypes: ImmutableDictionary<string, DeclaredType>.Empty,
visibleTypes: ImmutableHashSet<DeclaredType>.Empty
Expand All @@ -36,15 +36,15 @@ public void Equality() {
new DeclaredTypeRegistry(
globalUsings: ImmutableArray<UsingDirective>.Empty,
scopeTree: new ScopeTree(
new Dictionary<string, DeclaredType>()
[], new Dictionary<string, DeclaredType>()
),
allTypes: ImmutableDictionary<string, DeclaredType>.Empty,
visibleTypes: ImmutableHashSet<DeclaredType>.Empty
).ShouldNotBe(
new DeclaredTypeRegistry(
globalUsings: ImmutableArray<UsingDirective>.Empty,
scopeTree: new ScopeTree(
new Dictionary<string, DeclaredType>()
[], new Dictionary<string, DeclaredType>()
),
allTypes: ImmutableDictionary<string, DeclaredType>.Empty,
visibleTypes: new DeclaredType[] {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace Chickensoft.Introspection.Generator.Tests.Models;

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using Chickensoft.Collections;
using Chickensoft.Introspection.Generator.Models;
using Shouldly;
using Xunit;
Expand Down Expand Up @@ -146,7 +146,12 @@ public class ScopeTreeTest {
public void AddsDeclaredTypes() {
// Use a map to guarantee types are added in the order shown below.
var tree = new ScopeTree(
new Map<string, DeclaredType>() {
[
_inner,
_outer,
_genericOuter
],
new Dictionary<string, DeclaredType>() {
[_inner.FullNameOpen] = _inner,
[_outer.FullNameOpen] = _outer,
[_genericOuter.FullNameOpen] = _genericOuter
Expand All @@ -173,7 +178,8 @@ public void AddsDeclaredTypes() {
public void ThrowsIfContainingTypeIsNotAbleToBeFound() =>
Should.Throw<InvalidOperationException>(
() => new ScopeTree(
new Map<string, DeclaredType>() {
[_inner, _outer],
new Dictionary<string, DeclaredType>() {
[_inner.FullNameOpen] = _inner,
[_outer.FullNameOpen] = _outer
}
Expand All @@ -183,7 +189,8 @@ public void ThrowsIfContainingTypeIsNotAbleToBeFound() =>
[Fact]
public void FindsTypesInScope() {
var tree = new ScopeTree(
new Map<string, DeclaredType>() {
[_typeInOtherNs, _typeExtendingTypeInOtherNs],
new Dictionary<string, DeclaredType>() {
[_typeInOtherNs.FullNameOpen] = _typeInOtherNs,
[_typeExtendingTypeInOtherNs.FullNameOpen] = _typeExtendingTypeInOtherNs,
}
Expand Down
15 changes: 8 additions & 7 deletions Chickensoft.Introspection.Generator/src/TypeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,22 @@ public void Initialize(IncrementalGeneratorInitializationContext context) {
var typesByFullName = declaredTypes
.GroupBy((type) => type.FullNameOpen);

var uniqueTypes = typesByFullName
var uniqueTypeList = typesByFullName
.Select(
// Combine non-unique type entries together.
group => group.Aggregate(
(DeclaredType typeA, DeclaredType typeB) =>
typeA.MergePartialDefinition(typeB)
)
)
.OrderBy(type => type.FullNameOpen) // Sort for deterministic output
.ToDictionary(
g => g.FullNameOpen,
g => g
);
.OrderBy(type => type.FullNameOpen);

var uniqueTypes = uniqueTypeList.ToDictionary(
type => type.FullNameOpen,
type => type
);

var tree = new ScopeTree(uniqueTypes);
var tree = new ScopeTree(uniqueTypeList, uniqueTypes);

var visibleTypeFullNames = tree.GetTypes().Select(t => t.FullNameOpen);

Expand Down
11 changes: 7 additions & 4 deletions Chickensoft.Introspection.Generator/src/models/ScopeTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ public class ScopeTree {
/// <summary>Map of types by their full, open generic names.</summary>
public IDictionary<string, DeclaredType> TypesByFullNameOpen { get; }

public ScopeTree(IDictionary<string, DeclaredType> typesByFullNameOpen) {
public ScopeTree(
IEnumerable<DeclaredType> types,
IDictionary<string, DeclaredType> typesByFullNameOpen
) {
TypesByFullNameOpen = typesByFullNameOpen;
InitializeTree();
InitializeTree(types);
}

private void InitializeTree() {
foreach (var declaredType in TypesByFullNameOpen.Values) {
private void InitializeTree(IEnumerable<DeclaredType> typesByFullNameOpen) {
foreach (var declaredType in typesByFullNameOpen) {
AddType(declaredType);
}
}
Expand Down

0 comments on commit 80facb5

Please sign in to comment.