Skip to content

Commit

Permalink
- with fluent assertions generator: fix problem when assembly name of…
Browse files Browse the repository at this point in the history
… referenced assembly was different from root namespace
  • Loading branch information
ax0l0tl committed Jul 26, 2024
1 parent d6caf88 commit f49891c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ public static IEnumerable<ResultTypeSchema> GetResultTypes(
Action<Diagnostic> reportDiagnostic,
CancellationToken cancellationToken)
{
return assembly.Modules
.SelectMany(m => GetTypesWithAttribute(m.GlobalNamespace, ResultTypeAttribute))
var allTypesInAssembly = GetAllTypes(assembly);

return GetTypesWithAttribute(allTypesInAssembly, ResultTypeAttribute)
.Where(tuple => tuple.Type.DeclaredAccessibility == Accessibility.Public || (tuple.Type.DeclaredAccessibility == Accessibility.Internal && generateForInternalTypes))
.Select(tuple =>
{
Expand All @@ -27,23 +28,29 @@ public static IEnumerable<UnionTypeSchema> GetUnionTypes(
Action<Diagnostic> reportDiagnostic,
CancellationToken cancellationToken)
{
var unionTypes = assembly.Modules
.SelectMany(m => GetTypesWithAttribute(m.GlobalNamespace, UnionTypeAttribute))
var allTypesInAssembly = GetAllTypes(assembly);

var unionTypes = GetTypesWithAttribute(allTypesInAssembly, UnionTypeAttribute)
.Where(tuple => tuple.Type.DeclaredAccessibility == Accessibility.Public || (tuple.Type.DeclaredAccessibility == Accessibility.Internal && generateForInternalTypes));

foreach (var (unionType, _) in unionTypes)
{
var main = assembly.Identity.Name.Split('.').Aggregate(assembly.GlobalNamespace,
(symbol, part) => symbol.GetNamespaceMembers().Single(m => m.Name.Equals(part)));

var derivedTypes = main.GetAllTypes().Where(t => t.InheritsFrom(unionType)).ToList();
var derivedTypes = allTypesInAssembly.Where(t => t.InheritsFrom(unionType)).ToList();
yield return new UnionTypeSchema(unionType, derivedTypes);
}
}

static List<INamedTypeSymbol> GetAllTypes(IAssemblySymbol assembly)
{
var allTypesInAssembly = assembly.Modules
.SelectMany(m => m.GlobalNamespace.GetAllTypes())
.ToList();
return allTypesInAssembly;
}

private static IEnumerable<(INamedTypeSymbol Type, AttributeData Attribute)> GetTypesWithAttribute(
INamespaceSymbol @namespace, string name)
=> @namespace.GetAllTypes()
IReadOnlyCollection<INamedTypeSymbol> types, string name)
=> types
.Select(t => (Type: t,
Attribute: t.GetAttributes().FirstOrDefault(a => a.AttributeClass?.ToDisplayString() == name)))
.Where(tuple => tuple.Attribute is not null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

<!--#region adapt versions here-->
<MajorVersion>1</MajorVersion>
<MinorAndPatchVersion>2.1</MinorAndPatchVersion>
<MinorAndPatchVersion>2.2</MinorAndPatchVersion>
<!--#endregion-->

<AssemblyVersion>$(MajorVersion).0.0</AssemblyVersion>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//Generator runs: 1
#nullable enable
#nullable enable
using global::System.Linq;
using FunicularSwitch;
using System;
Expand Down

0 comments on commit f49891c

Please sign in to comment.