Skip to content

Commit

Permalink
增加Builder功能
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvelTiter committed Aug 24, 2024
1 parent 0eea22d commit 889b830
Show file tree
Hide file tree
Showing 17 changed files with 810 additions and 570 deletions.
44 changes: 24 additions & 20 deletions src/AutoGenMapper.Roslyn/BuildAutoMapClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,20 @@ private static bool GetPropertyValue(GenMapperContext context, IPropertySymbol p
{
if (prop.Type.HasInterfaceAll("System.Collections.IEnumerable") && prop.Type.SpecialType == SpecialType.None)
{
var et = prop.Type.GetGenericTypes().First();
ITypeSymbol? et = null;
var fin = "";
if (prop.Type is IArrayTypeSymbol at)
{
et = at.ElementType;
fin = "ToArray()";
}
else
{
et = prop.Type.GetGenericTypes().First();
fin = "ToList()";
}
if (et.HasInterface(AutoMapperGenerator.GenMapableInterface))
{
var fin = "";
if (prop.Type.Name.Contains("Array"))
{
fin = "ToArray()";
}
else
{
fin = "ToList()";
}
value = ($"""this.{customTrans.From}.Select(i => i.MapTo<{et.ToDisplayString()}>("{et.MetadataName}")).{fin}""");
}
else
Expand All @@ -147,18 +149,20 @@ private static bool GetPropertyValue(GenMapperContext context, IPropertySymbol p
{
if (prop.Type.HasInterfaceAll("System.Collections.IEnumerable") && prop.Type.SpecialType == SpecialType.None)
{
var et = prop.Type.GetGenericTypes().First();
ITypeSymbol? et = null;
var fin = "";
if (prop.Type is IArrayTypeSymbol at)
{
et = at.ElementType;
fin = "ToArray()";
}
else
{
et = prop.Type.GetGenericTypes().First();
fin = "ToList()";
}
if (et.HasInterface(AutoMapperGenerator.GenMapableInterface))
{
var fin = "";
if (prop.Type.Name.Contains("Array"))
{
fin = "ToArray()";
}
else
{
fin = "ToList()";
}
value = ($"""this.{p.Name}.Select(i => i.MapTo<{et.ToDisplayString()}>("{et.MetadataName}")).{fin}""");
}
else
Expand Down
115 changes: 56 additions & 59 deletions src/Generators.Shared/Builder/ClassBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,85 +1,82 @@
using Microsoft.CodeAnalysis;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Generators.Shared.Builder
namespace Generators.Shared.Builder;

internal enum NodeType
{
Unit,
NameSpace,
Class,
Interface,
Record,
Constructor,
Field,
Property,
Method,
}
internal enum MemberType
{
internal enum NodeType
Field,
Property,
Method,
Constructor,
}
internal class ClassBuilder : MemberBuilder<ClassBuilder>
{
public ClassBuilder()
{
Unit,
NameSpace,
Class,
Interface,
Record,
Constructor,
Field,
Property,
Method,
Modifiers = "public partial";
}
internal enum MemberType
public override NodeType Type => NodeType.Class;
public override string Indent => " ";
public string? BaseType { get; set; }
public string ClassType { get; set; } = "class";
public IList<string> Interfaces { get; } = [];
private string BaseTypeList
{
Field,
Property,
Method,
Constructor,
get
{
if (BaseType == null && !Interfaces.Any())
{
return "";
}
string?[] all = [BaseType, .. Interfaces];
return $": {string.Join(", ", all.Where(b => !string.IsNullOrEmpty(b)))}";
}
}
internal class ClassBuilder : MemberBuilder<ClassBuilder>

private IEnumerable<string> RenderMembers()
{
public ClassBuilder()
foreach (var m in Members.Where(m => m.Type == NodeType.Field))
{
Modifiers = "public partial";
yield return $"{m}";
}
public override NodeType Type => NodeType.Class;
public override string Indent => " ";
public string? BaseType { get; set; }
public IList<string> Interfaces { get; } = [];
private string BaseTypeList
foreach (var m in Members.Where(m => m.Type == NodeType.Constructor))
{
get
{
if (BaseType == null && !Interfaces.Any())
{
return "";
}
string?[] all = [BaseType, .. Interfaces];
return $": {string.Join(", ", all.Where(b => !string.IsNullOrEmpty(b)))}";
}
yield return $"{m}";
}

private IEnumerable<string> RenderMembers()
foreach (var m in Members.Where(m => m.Type == NodeType.Property))
{
foreach (var m in Members.Where(m => m.Type == NodeType.Field))
{
yield return $"{m}";
}
foreach (var m in Members.Where(m => m.Type == NodeType.Constructor))
{
yield return $"{m}";
}
foreach (var m in Members.Where(m => m.Type == NodeType.Property))
{
yield return $"{m}";
}
foreach (var m in Members.Where(m => m.Type == NodeType.Method))
{
yield return $"{m}";
}
yield return $"{m}";
}

public override string ToString()
foreach (var m in Members.Where(m => m.Type == NodeType.Method))
{
return
yield return $"{m}";
}
}

public override string ToString()
{
return
$$"""
{{AttributeList}}
{{Indent}}/// <inheritdoc/>
{{Indent}}{{Modifiers}} class {{Name}} {{BaseTypeList}}
{{Indent}}{{Modifiers}} {{ClassType}} {{Name}} {{BaseTypeList}}
{{Indent}}{
{{string.Join("\n\n", RenderMembers())}}
{{Indent}}}
""";
}
}
}
37 changes: 18 additions & 19 deletions src/Generators.Shared/Builder/CodeFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,32 @@
using System.Collections.Generic;
using System.Linq;

namespace Generators.Shared.Builder
namespace Generators.Shared.Builder;

internal class CodeFile : TypeBuilder
{
internal class CodeFile : TypeBuilder
{
private CodeFile(string filename) { FileName = filename; }
public static CodeFile New(string filename) => new(filename);
public override NodeType Type => NodeType.Unit;
public string FileName { get; set; }
public IList<string> Usings { get; } = [];
private CodeFile(string filename) { FileName = filename; }
public static CodeFile New(string filename) => new(filename);
public override NodeType Type => NodeType.Unit;
public string FileName { get; set; }
public IList<string> Usings { get; } = [];

public CodeFile AddUsings(params string[] usings)
public CodeFile AddUsings(params string[] usings)
{
foreach (var us in usings)
{
foreach (var us in usings)
{
Usings.Add(us);
}
return this;
Usings.Add(us);
}
return this;
}

public override string ToString()
{
return
public override string ToString()
{
return
$"""
{string.Join("\n", Usings)}
{string.Join("\n\n", Members.Select(m => m.ToString()))}
"""
;
}
;
}
}
56 changes: 35 additions & 21 deletions src/Generators.Shared/Builder/FieldBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,50 @@
using System.Collections.Generic;
using System.Text;

namespace Generators.Shared.Builder
namespace Generators.Shared.Builder;

internal class FieldBuilder : MemberBuilder<FieldBuilder>
{
internal class FieldBuilder : MemberBuilder<FieldBuilder>
public FieldBuilder()
{
public FieldBuilder()
{
Modifiers = "private readonly";
}
public override NodeType Type => NodeType.Field;
public override string Indent => " ";
public override string ToString()
{
return $"""
{Indent}{Modifiers} {MemberType} {Name};
""";
}
Modifiers = "private readonly";
}
public override NodeType Type => NodeType.Field;
public override string Indent => " ";
public override string ToString()
{
return $"""
{Indent}{Modifiers} {MemberType} {Name};
""";
}
}

internal class PropertyBuilder : MemberBuilder<PropertyBuilder>
internal class PropertyBuilder : MemberBuilder<PropertyBuilder>
{
public PropertyBuilder()
{
public PropertyBuilder()
Modifiers = "public";
}
public bool CanRead { get; set; } = true;
public bool CanWrite { get; set; } = true;
public override NodeType Type => NodeType.Property;
public override string Indent => " ";
string Getter => CanRead ? " get;" : "";
string Setter => CanWrite ? " set;" : "";
public bool IsLambdaBody { get; set; }
string InitStatement => string.IsNullOrEmpty(Initialization) ? "" : $" = {Initialization};";
public override string ToString()
{
if (IsLambdaBody)
{
Modifiers = "public";
return $$"""
{{Indent}}{{Modifiers}} {{MemberType}} {{Name}} => {{Initialization}};
""";
}
public override NodeType Type => NodeType.Property;
public override string Indent => " ";
public override string ToString()
else
{
return $$"""
{{Indent}}{{Modifiers}} {{MemberType}} {{Name}} { get; set; }
{{Indent}}{{Modifiers}} {{MemberType}} {{Name}} {{{Getter}}{{Setter}} }{{InitStatement}}
""";
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/Generators.Shared/Builder/Member.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace Generators.Shared.Builder

namespace Generators.Shared.Builder;

internal abstract class Node
{
internal abstract class Node
{
public abstract NodeType Type { get; }
public virtual string Indent => "";
//public abstract new string ToString();
}
public abstract NodeType Type { get; }
public virtual string Indent => "";
//public abstract new string ToString();
}
Loading

0 comments on commit 889b830

Please sign in to comment.