Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/dotnet-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v5
with:
dotnet-version: 9.0
dotnet-version: 10.0

# Install dependencies
- name: Restore dependencies
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release-please.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
if: ${{ steps.release.outputs.release_created }}
uses: actions/setup-dotnet@v5
with:
dotnet-version: '8.0.x'
dotnet-version: '10.0.x'

- name: Restore dependencies
if: ${{ steps.release.outputs.release_created }}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
7 changes: 5 additions & 2 deletions examples/Example.Core/Example.Core/Example.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand All @@ -12,7 +12,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Dango" Version="0.1.0" />
<ProjectReference Include="..\..\..\src\Dango\Dango.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false"/>
<ProjectReference Include="..\..\..\src\Dango.Abstractions\Dango.Abstractions.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions src/Dango.Abstractions/IDangoEnumMappingConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@ public interface IDangoEnumMappingConfiguration<TSource, TDestination>
/// </summary>
/// <returns>The configuration instance for method chaining.</returns>
IDangoEnumMappingConfiguration<TSource, TDestination> MapByValue();

/// <summary>
/// Configures the mapping to match enum values by their names (default behavior).
/// For example, SourceEnum.Active will map to DestinationEnum.Active if both exist.
/// </summary>
/// <returns>The configuration instance for method chaining.</returns>
IDangoEnumMappingConfiguration<TSource, TDestination> MapByName();

/// <summary>
/// Specifies a default destination value to use when a source enum value has no matching destination value.
/// This prevents compilation errors when the destination enum has fewer values than the source.
/// </summary>
/// <param name="defaultValue">The default destination enum value to use for unmapped source values.</param>
/// <returns>The configuration instance for method chaining.</returns>
IDangoEnumMappingConfiguration<TSource, TDestination> WithDefault(TDestination defaultValue);

/// <summary>
/// Provides custom mappings for specific source enum values, overriding the default mapping strategy.
/// This is useful when certain values need special handling that doesn't follow the name or value pattern.
Expand Down
4 changes: 2 additions & 2 deletions src/Dango/Analysis/MappingAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Dango.Analysis;
internal static class MappingAnalyzer
{
public static Dictionary<INamedTypeSymbol, Dictionary<EnumPair, EnumMapping>> AnalyzeMappings(
SourceProductionContext context,
SourceProductionContext context,
IMethodSymbol registerMethod,
SemanticModel model)
{
Expand Down Expand Up @@ -210,7 +210,7 @@ private static void ProcessChainedInvocation(
}

private static void AddMapping(
Dictionary<INamedTypeSymbol, Dictionary<EnumPair, EnumMapping>> enumMappingsBySourceEnum,
Dictionary<INamedTypeSymbol, Dictionary<EnumPair, EnumMapping>> enumMappingsBySourceEnum,
EnumPair enumPair,
EnumMapping mapping)
{
Expand Down
10 changes: 5 additions & 5 deletions src/Dango/Analysis/MappingResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Dango.Analysis;
internal static class MappingResolver
{
public static IEnumerable<(string SourceValue, string DestinationValue)> ResolveValueMappings(
SourceProductionContext context,
SourceProductionContext context,
EnumPair enumPair,
EnumMapping mapping)
{
Expand All @@ -21,7 +21,7 @@ internal static class MappingResolver
}

private static IEnumerable<(string SourceValue, string DestinationValue)> ResolveValueMappingsByName(
SourceProductionContext context,
SourceProductionContext context,
EnumPair enumPair,
EnumMapping mapping)
{
Expand All @@ -30,13 +30,13 @@ internal static class MappingResolver
.Where(f => f.IsConst && f.IsStatic)
.Select(f => f.Name)
.ToList();

var destinationEnumValues = enumPair.DestinationEnum.GetMembers()
.OfType<IFieldSymbol>()
.Where(f => f.IsConst && f.IsStatic)
.Select(f => f.Name)
.ToImmutableHashSet();

foreach (var enumValue in sourceEnumValues)
{
if (mapping.Overrides is not null && mapping.Overrides.TryGetValue(enumValue, out var destNameOverride))
Expand All @@ -60,7 +60,7 @@ internal static class MappingResolver
}

private static IEnumerable<(string SourceValue, string DestinationValue)> ResolveValueMappingsByValue(
SourceProductionContext context,
SourceProductionContext context,
EnumPair enumPair,
EnumMapping mapping)
{
Expand Down
10 changes: 5 additions & 5 deletions src/Dango/CodeGeneration/CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public static void GenerateSources(
}

private static CompilationUnitSyntax GenerateCompilationUnit(
string className,
string className,
string assemblyName,
IEnumerable<KeyValuePair<EnumPair, EnumMapping>> mappingsForEnumPairs,
IEnumerable<KeyValuePair<EnumPair, EnumMapping>> mappingsForEnumPairs,
SourceProductionContext context)
{
var methods = new List<MemberDeclarationSyntax>();
Expand All @@ -60,7 +60,7 @@ private static CompilationUnitSyntax GenerateCompilationUnit(
.AddMembers(namespaceDeclaration)
.WithLeadingTrivia(
Comment("// <auto-generated>"), EndOfLine("\n"),
Comment("// This code was generated by Dango tool. Do not change manually as changes will be overwritten."), EndOfLine("\n"),
Comment("// This code was generated by Dango tool. Do not change manually as changes will be overwritten."), EndOfLine("\n"),
Comment("// </auto-generated>"), EndOfLine("\n"));
}

Expand All @@ -86,9 +86,9 @@ private static MethodDeclarationSyntax GenerateNonNullableExtensionMethod(
.AddArgumentListArguments(Argument(IdentifierName("value")))),
Argument(IdentifierName("value")),
Argument(LiteralExpression(SyntaxKind.NullLiteralExpression)))));

var switchArms = mappings.Select(m => GenerateSwitchArm(enumPair, m)).Append(defaultArm).ToArray();

var switchExpression = SwitchExpression(IdentifierName("value")).AddArms(switchArms);

return MethodDeclaration(destinationTypeName, mappingMethodName)
Expand Down
2 changes: 1 addition & 1 deletion src/Dango/Dango.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.14.0" PrivateAssets="all"/>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="5.0.0" PrivateAssets="all" />
<PackageReference Update="NETStandard.Library" Version="2.0.3" PrivateAssets="all"/>
</ItemGroup>

Expand Down
7 changes: 3 additions & 4 deletions src/Dango/DangoGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Dango.Abstractions;
using Dango.Analysis;
using Dango.CodeGeneration;
using Dango.ErrorHandling;
Expand All @@ -27,7 +26,7 @@ private static void Execute(SourceProductionContext context,
(Compilation compilation, ImmutableArray<ClassDeclarationSyntax> candidates) input)
{
var (compilation, candidates) = input;
var registrarInterface = compilation.GetTypeByMetadataName(typeof(IDangoMapperRegistrar).FullName!)!;
var registrarInterface = compilation.GetTypeByMetadataName(WellKnownTypes.IDangoMapperRegistrarFullName)!;

var enumMappingsBySourceEnum = new Dictionary<INamedTypeSymbol, Dictionary<EnumPair, EnumMapping>>(SymbolEqualityComparer.Default);
var registrarInterfaceImplementationFound = false;
Expand All @@ -47,7 +46,7 @@ private static void Execute(SourceProductionContext context,

registrarInterfaceImplementationFound = true;

var registerMethod = symbol.GetMembers(nameof(IDangoMapperRegistrar.Register))
var registerMethod = symbol.GetMembers(WellKnownTypes.RegisterMethodName)
.OfType<IMethodSymbol>()
.FirstOrDefault(m => m.Parameters.Length == 1);

Expand Down Expand Up @@ -90,7 +89,7 @@ private static void AppendMappings(
private static void AddToResolvedMappings(
SourceProductionContext context,
Location location,
Dictionary<INamedTypeSymbol, Dictionary<EnumPair, EnumMapping>> enumMappingsBySourceEnum,
Dictionary<INamedTypeSymbol, Dictionary<EnumPair, EnumMapping>> enumMappingsBySourceEnum,
EnumPair enumPair,
EnumMapping mapping)
{
Expand Down
13 changes: 13 additions & 0 deletions src/Dango/WellKnownTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Dango;

/// <summary>
/// Constants for well-known type metadata names.
/// These use nameof() to get compile-time type safety in the generator project,
/// but are used as strings at runtime (no runtime type loading required).
/// </summary>
internal static class WellKnownTypes
{
// Compile-time constants that give us type safety
public const string IDangoMapperRegistrarFullName = nameof(Dango) + "." + nameof(Abstractions) + "." + nameof(Abstractions.IDangoMapperRegistrar);
public const string RegisterMethodName = nameof(Abstractions.IDangoMapperRegistrar.Register);
}
8 changes: 4 additions & 4 deletions test/Dango.Tests/Dango.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
Expand All @@ -11,11 +11,11 @@

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.4"/>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.14.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.0"/>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="5.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
<PackageReference Include="NUnit" Version="4.4.0"/>
<PackageReference Include="NUnit.Analyzers" Version="4.11.2"/>
<PackageReference Include="NUnit3TestAdapter" Version="5.2.0"/>
<PackageReference Include="NUnit3TestAdapter" Version="6.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down