AutoMapper
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
using Microsoft.CodeAnalysis;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace ProxyInterfaceConsumer
|
||||
{
|
||||
public class Person
|
||||
{
|
||||
|
||||
private int PrivateId { get; }
|
||||
public int Id { get; }
|
||||
|
||||
@@ -14,6 +14,10 @@ namespace ProxyInterfaceConsumer
|
||||
|
||||
public Address Address { get; set; }
|
||||
|
||||
public List<Address> AddressesLIst { get; set; }
|
||||
|
||||
public Dictionary<string, Address> AddressesDict { get; set; }
|
||||
|
||||
public E E { get; set; }
|
||||
|
||||
public int Add(string s)
|
||||
@@ -28,11 +32,9 @@ namespace ProxyInterfaceConsumer
|
||||
|
||||
public Compilation Compilation { get; set; }
|
||||
|
||||
public INamedTypeSymbol MyNamedTypeSymbol { get;set; }
|
||||
public INamedTypeSymbol MyNamedTypeSymbol { get; set; }
|
||||
}
|
||||
|
||||
|
||||
|
||||
public enum E
|
||||
{
|
||||
V1,
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using AutoMapper;
|
||||
|
||||
namespace ProxyInterfaceConsumer
|
||||
{
|
||||
@@ -6,6 +8,25 @@ namespace ProxyInterfaceConsumer
|
||||
{
|
||||
public static void Main()
|
||||
{
|
||||
|
||||
|
||||
|
||||
var c = new Clazz
|
||||
{
|
||||
Name = "n"
|
||||
};
|
||||
var cp = new ClazzProxy(c);
|
||||
|
||||
var t = new Test();
|
||||
t.Cs = new List<Clazz> { c };
|
||||
|
||||
var tp = new TestProxy(t);
|
||||
tp.Cs = new List<IClazz> { cp };
|
||||
|
||||
Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(t));
|
||||
Console.WriteLine(new string('-', 80));
|
||||
|
||||
|
||||
IPerson p = new PersonProxy(new Person());
|
||||
p.Name = "test";
|
||||
p.Address = new AddressProxy(new Address { HouseNumber = 42 });
|
||||
@@ -17,4 +38,108 @@ namespace ProxyInterfaceConsumer
|
||||
Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(p));
|
||||
}
|
||||
}
|
||||
|
||||
public struct Test
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
public Clazz C { get; }
|
||||
|
||||
public IList<Clazz> Cs { get; set; }
|
||||
|
||||
public int Add(string s)
|
||||
{
|
||||
return 600;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class Clazz
|
||||
{
|
||||
public string Name { get; set; }
|
||||
}
|
||||
|
||||
public interface ITest
|
||||
{
|
||||
int Id { get; set; }
|
||||
|
||||
IClazz C { get; }
|
||||
|
||||
IList<IClazz> Cs { get; set; }
|
||||
|
||||
int Add(string s);
|
||||
}
|
||||
|
||||
public interface IClazz
|
||||
{
|
||||
string Name { get; set; }
|
||||
}
|
||||
|
||||
public class TestProxy : ITest
|
||||
{
|
||||
private Test _instance;
|
||||
|
||||
private IClazz _clazz;
|
||||
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public TestProxy(Test instance)
|
||||
{
|
||||
_instance = instance;
|
||||
|
||||
// _clazz = new ClazzProxy(_instance.C);
|
||||
|
||||
_mapper = new MapperConfiguration(cfg =>
|
||||
{
|
||||
//cfg.CreateMap<Clazz, ClazzProxy>();
|
||||
//cfg.CreateMap<ClazzProxy, Clazz>();
|
||||
|
||||
cfg.CreateMap<Clazz, IClazz>();
|
||||
cfg.CreateMap<IClazz, Clazz>();
|
||||
}).CreateMapper();
|
||||
}
|
||||
|
||||
public int Id
|
||||
{
|
||||
get => _instance.Id;
|
||||
set => _instance.Id = value;
|
||||
}
|
||||
|
||||
public IClazz C => _clazz;
|
||||
|
||||
public IList<IClazz> Cs2
|
||||
{
|
||||
get
|
||||
{
|
||||
//return null; // TinyMapper.Map<IList<IClazz>>(_instance.Cs); //(IList<IClazz>)_instance.Cs.Select(x => new ClazzProxy(x));
|
||||
return _mapper.Map<IList<IClazz>>(_instance.Cs); //(IList<IClazz>)_instance.Cs.Select(x => new ClazzProxy(x));
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
_instance.Cs = _mapper.Map<IList<Clazz>>(value);
|
||||
//_instance.Cs = TinyMapper.Map<IList<Clazz>>(value);
|
||||
}
|
||||
}
|
||||
|
||||
public IList<IClazz> Cs
|
||||
{
|
||||
get => _mapper.Map<IList<IClazz>>(_instance.Cs);
|
||||
|
||||
set => _instance.Cs = _mapper.Map<IList<Clazz>>(value);
|
||||
}
|
||||
|
||||
public int Add(string s) => _instance.Add(s);
|
||||
}
|
||||
|
||||
public class ClazzProxy : IClazz
|
||||
{
|
||||
private Clazz _instance;
|
||||
|
||||
public ClazzProxy(Clazz instance)
|
||||
{
|
||||
_instance = instance;
|
||||
}
|
||||
|
||||
public string Name { get => _instance.Name; set => _instance.Name = value; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,12 +6,14 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper" Version="10.1.1" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="3.10.0" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.10.0" />
|
||||
<!--<PackageReference Include="TinyMapper" Version="3.0.3" />-->
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -39,15 +39,23 @@ namespace ProxyInterfaceSourceGenerator.Extensions
|
||||
return $"{property.Type} {property.Name} {{ {get}{set}}}";
|
||||
}
|
||||
|
||||
public static string ToPropertyTextForClass(this IPropertySymbol property, string interfaceName, string className)
|
||||
public static string ToPropertyTextForClass(this IPropertySymbol property, string overrideType)
|
||||
{
|
||||
var classNameProxy = $"{className}Proxy";
|
||||
var get = property.GetMethod != null ? $"get => new {classNameProxy}(_Instance.{property.Name}); " : string.Empty;
|
||||
var set = property.SetMethod != null ? $"set => _Instance.{property.Name} = (({classNameProxy}) value)._Instance; " : string.Empty;
|
||||
var get = property.GetMethod != null ? $"get => _mapper.Map<{overrideType}>(_Instance.{property.Name}); " : string.Empty;
|
||||
var set = property.SetMethod != null ? $"set => _Instance.{property.Name} = _mapper.Map<{property.Type}>(value); " : string.Empty;
|
||||
|
||||
return $"{interfaceName} {property.Name} {{ {get}{set}}}";
|
||||
return $"{overrideType} {property.Name} {{ {get}{set}}}";
|
||||
}
|
||||
|
||||
//public static string ToPropertyTextForClass(this IPropertySymbol property, string overrideType, string className)
|
||||
//{
|
||||
// var classNameProxy = $"{className}Proxy";
|
||||
// var get = property.GetMethod != null ? $"get => new {classNameProxy}(_Instance.{property.Name}); " : string.Empty;
|
||||
// var set = property.SetMethod != null ? $"set => _Instance.{property.Name} = (({classNameProxy}) value)._Instance; " : string.Empty;
|
||||
|
||||
// return $"{overrideType} {property.Name} {{ {get}{set}}}";
|
||||
//}
|
||||
|
||||
public static string ToMethodText(this IMethodSymbol method)
|
||||
{
|
||||
var parameters = new List<string>();
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace ProxyInterfaceSourceGenerator.FileGenerators
|
||||
@@ -12,6 +14,41 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
|
||||
_context = context;
|
||||
}
|
||||
|
||||
protected string GetPropertyType(IPropertySymbol property, out Dictionary<string, string> differs)
|
||||
{
|
||||
differs = new Dictionary<string, string>();
|
||||
|
||||
var existing = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == property.Type.ToString());
|
||||
if (existing is not null)
|
||||
{
|
||||
differs.Add(property.Type.ToString(), existing.InterfaceName);
|
||||
return existing.InterfaceName;
|
||||
}
|
||||
|
||||
if (property.Type is INamedTypeSymbol namedTypedSymbol)
|
||||
{
|
||||
var type = property.Type.ToString();
|
||||
foreach (var typeArgument in namedTypedSymbol.TypeArguments)
|
||||
{
|
||||
var exist = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == typeArgument.ToString());
|
||||
|
||||
if (exist is not null)
|
||||
{
|
||||
if (!differs.ContainsKey(typeArgument.ToString()))
|
||||
{
|
||||
differs.Add(typeArgument.ToString(), exist.InterfaceName);
|
||||
}
|
||||
|
||||
type = type.Replace(typeArgument.ToString(), exist.InterfaceName);
|
||||
}
|
||||
}
|
||||
|
||||
return type;
|
||||
}
|
||||
|
||||
return property.Type.ToString();
|
||||
}
|
||||
|
||||
protected INamedTypeSymbol GetType(string name)
|
||||
{
|
||||
var symbol = _context.GeneratorExecutionContext.Compilation.GetTypeByMetadataName(name);
|
||||
|
||||
@@ -94,16 +94,19 @@ namespace {symbol.ContainingNamespace}
|
||||
//}
|
||||
//else
|
||||
|
||||
var existing = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == property.Type.ToString());
|
||||
if (existing is not null)
|
||||
{
|
||||
str.AppendLine($" {property.ToPropertyText(existing.InterfaceName)}");
|
||||
}
|
||||
else
|
||||
{
|
||||
str.AppendLine($" {property.ToPropertyText()}");
|
||||
}
|
||||
|
||||
var type = GetPropertyType(property, out _);
|
||||
str.AppendLine($" {property.ToPropertyText(type)}");
|
||||
|
||||
//var existing = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == property.Type.ToString());
|
||||
//if (existing is not null)
|
||||
//{
|
||||
// str.AppendLine($" {property.ToPropertyText(existing.InterfaceName)}");
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// str.AppendLine($" {property.ToPropertyText()}");
|
||||
//}
|
||||
|
||||
str.AppendLine();
|
||||
}
|
||||
|
||||
|
||||
@@ -43,16 +43,21 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
|
||||
}
|
||||
|
||||
private string CreateProxyClassCode(INamedTypeSymbol symbol, string interfaceName, string className, bool proxyAll) => $@"using System;
|
||||
using AutoMapper;
|
||||
|
||||
namespace {symbol.ContainingNamespace}
|
||||
{{
|
||||
public class {className}Proxy : {interfaceName}
|
||||
{{
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public {className} _Instance {{ get; }}
|
||||
|
||||
public {className}Proxy({className} instance)
|
||||
{{
|
||||
_Instance = instance;
|
||||
|
||||
{GenerateAutoMapper()}
|
||||
}}
|
||||
|
||||
{GeneratePublicProperties(symbol, proxyAll)}
|
||||
@@ -61,6 +66,22 @@ namespace {symbol.ContainingNamespace}
|
||||
}}
|
||||
}}";
|
||||
|
||||
private string GenerateAutoMapper()
|
||||
{
|
||||
var str = new StringBuilder();
|
||||
|
||||
str.AppendLine(" _mapper = new MapperConfiguration(cfg =>");
|
||||
str.AppendLine(" {");
|
||||
foreach (var x in _context.CandidateInterfaces)
|
||||
{
|
||||
str.AppendLine($" cfg.CreateMap<{x.Value.InterfaceName}, {x.Value.ClassName}>();");
|
||||
str.AppendLine($" cfg.CreateMap<{x.Value.ClassName}, {x.Value.InterfaceName}>();");
|
||||
}
|
||||
str.AppendLine(" }).CreateMapper();");
|
||||
|
||||
return str.ToString();
|
||||
}
|
||||
|
||||
private string GeneratePublicProperties(INamedTypeSymbol symbol, bool proxyAll)
|
||||
{
|
||||
var str = new StringBuilder();
|
||||
@@ -82,15 +103,51 @@ namespace {symbol.ContainingNamespace}
|
||||
// ComplexProperties
|
||||
foreach (var property in MemberHelper.GetPublicProperties(symbol, p => p.GetTypeEnum() == TypeEnum.Complex))
|
||||
{
|
||||
var existing = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == property.Type.ToString());
|
||||
if (existing is not null)
|
||||
{
|
||||
str.AppendLine($" public {property.ToPropertyTextForClass(existing.InterfaceName, existing.ClassName)}");
|
||||
}
|
||||
else
|
||||
var type = GetPropertyType(property, out var differs);
|
||||
if (!differs.Any())
|
||||
{
|
||||
str.AppendLine($" public {property.ToPropertyTextForClass()}");
|
||||
}
|
||||
else
|
||||
{
|
||||
str.AppendLine($" public {property.ToPropertyTextForClass(type)}");
|
||||
// var get = property.GetMethod != null ? $"get => _mapper.Map<{type}>(_Instance.{property.Name}); " : string.Empty;
|
||||
// var set = property.SetMethod != null ? $"set => _Instance.{property.Name} = _mapper.Map<{property.Type}>(value);" : string.Empty;
|
||||
//var p = $"{type} {property.Name} {{ {get}{set}}}";
|
||||
//str.AppendLine($" public {type} {property.Name} {{ {get}{set}}}");
|
||||
}
|
||||
|
||||
/*
|
||||
public IList<IClazz> Cs
|
||||
{
|
||||
get => _mapper.Map<IList<IClazz>>(_instance.Cs);
|
||||
|
||||
set => _instance.Cs = _mapper.Map<IList<Clazz>>(value);
|
||||
}
|
||||
}*/
|
||||
|
||||
|
||||
//public static string ToPropertyTextForClass(this IPropertySymbol property, string overrideType)
|
||||
//{
|
||||
// // var classNameProxy = $"Proxy";
|
||||
// var get = property.GetMethod != null ? $"get => _mapper.Map<{overrideType}>(_Instance.{property.Name}); " : string.Empty;
|
||||
// var set = property.SetMethod != null ? $"set => _mapper.Map<{property.Type}>( = (({classNameProxy}) value)._Instance; " : string.Empty;
|
||||
|
||||
// return $"{overrideType} {property.Name} {{ {get}{set}}}";
|
||||
//}
|
||||
|
||||
|
||||
//str.AppendLine($" public {property.ToPropertyTextForClass(type)}");
|
||||
|
||||
//var existing = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == property.Type.ToString());
|
||||
//if (existing is not null)
|
||||
//{
|
||||
// str.AppendLine($" public {property.ToPropertyTextForClass(existing.InterfaceName, existing.ClassName)}");
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// str.AppendLine($" public {property.ToPropertyTextForClass()}");
|
||||
//}
|
||||
|
||||
str.AppendLine();
|
||||
}
|
||||
|
||||
@@ -23,9 +23,6 @@ namespace ProxyInterfaceSourceGenerator
|
||||
|
||||
public void Execute(GeneratorExecutionContext ctx)
|
||||
{
|
||||
var attributeData = _proxyAttributeGenerator.GenerateFile();
|
||||
ctx.AddSource(attributeData.FileName, SourceText.From(attributeData.Text, Encoding.UTF8));
|
||||
|
||||
if (ctx.SyntaxReceiver is not ProxySyntaxReceiver receiver)
|
||||
{
|
||||
return;
|
||||
@@ -37,6 +34,9 @@ namespace ProxyInterfaceSourceGenerator
|
||||
CandidateInterfaces = receiver.CandidateInterfaces
|
||||
};
|
||||
|
||||
var attributeData = _proxyAttributeGenerator.GenerateFile();
|
||||
context.GeneratorExecutionContext.AddSource(attributeData.FileName, SourceText.From(attributeData.Text, Encoding.UTF8));
|
||||
|
||||
var partialInterfacesGenerator = new PartialInterfacesGenerator(context);
|
||||
foreach (var data in partialInterfacesGenerator.GenerateFiles())
|
||||
{
|
||||
@@ -50,71 +50,4 @@ namespace ProxyInterfaceSourceGenerator
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public struct Test
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
public Clazz C { get; }
|
||||
|
||||
public int Add(string s)
|
||||
{
|
||||
return 600;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class Clazz
|
||||
{
|
||||
public string Name { get; set; }
|
||||
}
|
||||
|
||||
public interface ITest
|
||||
{
|
||||
int Id { get; set; }
|
||||
|
||||
IClazz C { get; }
|
||||
|
||||
int Add(string s);
|
||||
}
|
||||
|
||||
public interface IClazz
|
||||
{
|
||||
string Name { get; set; }
|
||||
}
|
||||
|
||||
public class TestMock : ITest
|
||||
{
|
||||
private Test _instance;
|
||||
|
||||
private IClazz _clazz;
|
||||
|
||||
public TestMock(Test instance)
|
||||
{
|
||||
_instance = instance;
|
||||
|
||||
_clazz = new ClazzMock(_instance.C);
|
||||
}
|
||||
|
||||
public int Id
|
||||
{
|
||||
get => _instance.Id;
|
||||
set => _instance.Id = value;
|
||||
}
|
||||
|
||||
public IClazz C => _clazz;
|
||||
|
||||
public int Add(string s) => _instance.Add(s);
|
||||
}
|
||||
|
||||
public class ClazzMock : IClazz
|
||||
{
|
||||
private Clazz _instance;
|
||||
|
||||
public ClazzMock(Clazz instance)
|
||||
{
|
||||
_instance = instance;
|
||||
}
|
||||
|
||||
public string Name { get => _instance.Name; set => _instance.Name = value; }
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper" Version="10.1.1" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
|
||||
Reference in New Issue
Block a user