-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use internal Map methods if possible, else use Mapster (#88)
* Map * x * tstst * ok? * cleanup * x * x
- Loading branch information
Showing
26 changed files
with
1,530 additions
and
990 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 72 additions & 38 deletions
110
src/ProxyInterfaceSourceGenerator/FileGenerators/ProxyClassesGenerator.Mapster.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,73 @@ | ||
using System.Text; | ||
using ProxyInterfaceSourceGenerator.Extensions; | ||
|
||
namespace ProxyInterfaceSourceGenerator.FileGenerators; | ||
|
||
internal partial class ProxyClassesGenerator | ||
{ | ||
private string GenerateMapperConfigurationForMapster(string className) | ||
{ | ||
if (Context.ReplacedTypes.Count == 0) | ||
{ | ||
return string.Empty; | ||
} | ||
|
||
var str = new StringBuilder(); | ||
|
||
str.AppendLine($" static {className}()"); | ||
str.AppendLine(@" {"); | ||
|
||
foreach (var replacedType in Context.ReplacedTypes) | ||
{ | ||
TryFindProxyDataByTypeName(replacedType.Key, out var fullTypeName); | ||
|
||
var classNameProxy = $"global::{fullTypeName!.NamespaceDot}{fullTypeName!.ShortMetadataName}Proxy"; | ||
|
||
var instance = $"instance{(replacedType.Key + replacedType.Value).GetDeterministicHashCodeAsString()}"; | ||
var proxy = $"proxy{(replacedType.Value + replacedType.Key).GetDeterministicHashCodeAsString()}"; | ||
|
||
str.AppendLine($" Mapster.TypeAdapterConfig<{replacedType.Key}, {replacedType.Value}>.NewConfig().ConstructUsing({instance} => new {classNameProxy}({instance}));"); | ||
str.AppendLine($" Mapster.TypeAdapterConfig<{replacedType.Value}, {replacedType.Key}>.NewConfig().MapWith({proxy} => (({classNameProxy}) {proxy})._Instance);"); | ||
str.AppendLine(); | ||
} | ||
|
||
str.AppendLine(@" }"); | ||
|
||
|
||
return str.ToString(); | ||
} | ||
using System.Text; | ||
using ProxyInterfaceSourceGenerator.Extensions; | ||
|
||
namespace ProxyInterfaceSourceGenerator.FileGenerators; | ||
|
||
internal partial class ProxyClassesGenerator | ||
{ | ||
private string GenerateMapperConfigurationForMapster(string className) | ||
{ | ||
if (Context.ReplacedTypes.Count == 0) | ||
{ | ||
return string.Empty; | ||
} | ||
|
||
var str = new StringBuilder(); | ||
|
||
var indirectReplacedTypes = Context.ReplacedTypes.Where(r => !r.Direct).ToArray(); | ||
var directReplacedTypes = Context.ReplacedTypes.Where(r => r.Direct).ToArray(); | ||
|
||
if (indirectReplacedTypes.Length > 0) | ||
{ | ||
str.AppendLine($" static {className}()"); | ||
str.AppendLine(@" {"); | ||
|
||
foreach (var direct in directReplacedTypes) | ||
{ | ||
var hash = (direct.ClassType + direct.InterfaceType).GetDeterministicHashCodeAsString(); | ||
var instance = $"instance{hash}"; | ||
var proxy = $"proxy{hash}"; | ||
|
||
str.AppendLine($" Mapster.TypeAdapterConfig<{direct.ClassType}, {direct.InterfaceType}>.NewConfig().ConstructUsing({instance} => new {direct.Proxy}({instance}));"); | ||
str.AppendLine($" Mapster.TypeAdapterConfig<{direct.InterfaceType}, {direct.ClassType}>.NewConfig().MapWith({proxy} => {proxy}._Instance);"); | ||
str.AppendLine(); | ||
} | ||
|
||
str.AppendLine(@" }"); | ||
} | ||
|
||
str.AppendLine(); | ||
|
||
foreach (var indirect in indirectReplacedTypes) | ||
{ | ||
str.AppendLine($" private static {indirect.InterfaceType} MapToInterface({indirect.ClassType} value)"); | ||
str.AppendLine(@" {"); | ||
str.AppendLine($" return Mapster.TypeAdapter.Adapt<{indirect.InterfaceType}>(value);"); | ||
str.AppendLine(@" }"); | ||
str.AppendLine(); | ||
|
||
str.AppendLine($" private static {indirect.ClassType} MapToInstance({indirect.InterfaceType} value)"); | ||
str.AppendLine(@" {"); | ||
str.AppendLine($" return Mapster.TypeAdapter.Adapt<{indirect.ClassType}>(value);"); | ||
str.AppendLine(@" }"); | ||
str.AppendLine(); | ||
} | ||
|
||
foreach (var direct in directReplacedTypes) | ||
{ | ||
str.AppendLine($" private static {direct.InterfaceType} MapToInterface({direct.ClassType} value)"); | ||
str.AppendLine(@" {"); | ||
str.AppendLine($" return new {direct.Proxy}(value);"); | ||
str.AppendLine(@" }"); | ||
str.AppendLine(); | ||
|
||
str.AppendLine($" private static {direct.ClassType} MapToInstance({direct.InterfaceType} value)"); | ||
str.AppendLine(@" {"); | ||
str.AppendLine($" return value._Instance;"); | ||
str.AppendLine(@" }"); | ||
str.AppendLine(); | ||
} | ||
|
||
return str.ToString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace ProxyInterfaceSourceGenerator.Models; | ||
|
||
internal record Context | ||
{ | ||
public GeneratorExecutionContext GeneratorExecutionContext { get; init; } | ||
|
||
public IDictionary<InterfaceDeclarationSyntax, ProxyData> Candidates { get; init; } = default!; | ||
|
||
public Dictionary<string, string> ReplacedTypes { get; } = new(); | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace ProxyInterfaceSourceGenerator.Models; | ||
|
||
internal record Context | ||
{ | ||
public GeneratorExecutionContext GeneratorExecutionContext { get; init; } | ||
|
||
public IDictionary<InterfaceDeclarationSyntax, ProxyData> Candidates { get; init; } = default!; | ||
|
||
public List<ReplacedTypeInfo> ReplacedTypes { get; } = new(); | ||
} |
11 changes: 11 additions & 0 deletions
11
src/ProxyInterfaceSourceGenerator/Models/ReplacedTypeInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace ProxyInterfaceSourceGenerator.Models; | ||
|
||
internal record ReplacedTypeInfo | ||
( | ||
string ClassType, | ||
string InterfaceType, | ||
string ElementType, | ||
string ElementInterfaceType, | ||
string Proxy, | ||
bool Direct | ||
); |
Oops, something went wrong.