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 eb9be6d commit 5fa1666
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 52 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
101 changes: 100 additions & 1 deletion src/Generators.Shared/Builder/MethodBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public MethodBuilder()
public bool IsLambdaBody { get; set; }
string Async => IsAsync ? " async " : " ";
public string? ReturnType { get; set; } = "void";

public override string ToString()
{
if (IsLambdaBody)
Expand Down Expand Up @@ -92,3 +92,102 @@ private string AttachSemicolon()

}
}

internal class SwitchStatement : Statement
{
public SwitchStatement(string switchValue) : base("")
{
SwitchValue = switchValue;
}
public SwitchStatement() : base("")
{

}
public static SwitchStatement Default => new SwitchStatement();

public string? SwitchValue { get; set; }
public List<SwitchCaseStatement> SwitchCases { get; set; } = [];
public DefaultCaseStatement? DefaultCase { get; set; }
public override string ToString()
{
return
$$"""
{{Indent}}switch ({{SwitchValue}})
{{Indent}}{
{{string.Join("\n", SwitchCases)}}
{{DefaultCase}}
{{Indent}}}
""";
}
}

internal class SwitchCaseStatement : Statement
{
public SwitchCaseStatement() : base("")
{

}
public string? Condition { get; set; }
public string? Action { get; set; }
public bool IsBreak { get; set; }
public override string ToString()
{
if (IsBreak)
{
return
$"""
{Indent} case {Condition}:
{Indent} {Action};
{Indent} break;
""";
}
else
{
return
$"""
{Indent} case {Condition}:
{Indent} {Action};
""";
}
}
}

internal class DefaultCaseStatement : Statement
{
public DefaultCaseStatement() : base("")
{

}
public string? Condition { get; set; }
public string? Action { get; set; }
public override string ToString()
{
return
$"""
{Indent} default:
{Indent} {Action};
""";
}
public static implicit operator DefaultCaseStatement(string action) => new DefaultCaseStatement { Action = action };
}

internal class IfStatement : Statement
{
public IfStatement() : base("")
{

}
internal static IfStatement Default => new IfStatement();
public string? Condition { get; set; }
public List<Statement> IfContents { get; set; } = [];
public override string ToString()
{
return
$$"""
{{Indent}}if ({{Condition}})
{{Indent}}{
{{string.Join("\n", IfContents.Select(s => $" {s}"))}}
{{Indent}}}
""";
}
}
1 change: 1 addition & 0 deletions src/Generators.Shared/Builder/NamespaceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public override string ToString()
$$"""
// <auto-generated/>
#pragma warning disable
#nullable enable
namespace {{Namespace}}
{
{{string.Join("\t", Members)}}
Expand Down
30 changes: 0 additions & 30 deletions src/Generators.Shared/Extensions/ClassBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,3 @@ public static ClassBuilder Interface(this ClassBuilder builder, params string[]
return builder;
}
}

internal static class PropertyBuilderExtensions
{
public static PropertyBuilder PropertyName(this PropertyBuilder builder, string name)
{
builder.Name = name;
return builder;
}
public static PropertyBuilder Readonly(this PropertyBuilder builder)
{
builder.CanRead = true;
builder.CanWrite = false;
return builder;
}

public static PropertyBuilder Writeonly(this PropertyBuilder builder)
{
builder.CanRead = false;
builder.CanWrite = true;
return builder;
}

public static PropertyBuilder Lambda(this PropertyBuilder builder, string body)
{
builder.IsLambdaBody = true;
builder.Readonly();
builder.InitializeWith(body);
return builder;
}
}
56 changes: 56 additions & 0 deletions src/Generators.Shared/Extensions/MethodBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,60 @@ public static T AddParameter<T>(this T builder, params string[] parameters)
}
return builder;
}
#region switch
public static T AddSwitchStatement<T>(this T builder, string switchValue, Action<SwitchStatement> action) where T : MethodBase
{
var switchStatement = SwitchStatement.Default.Switch(switchValue);

Check failure on line 76 in src/Generators.Shared/Extensions/MethodBuilderExtensions.cs

View workflow job for this annotation

GitHub Actions / build

'SyntaxFactory.SwitchStatement(SyntaxToken, SyntaxToken, ExpressionSyntax, SyntaxToken, SyntaxToken, SyntaxList<SwitchSectionSyntax>, SyntaxToken)' is a method, which is not valid in the given context

Check failure on line 76 in src/Generators.Shared/Extensions/MethodBuilderExtensions.cs

View workflow job for this annotation

GitHub Actions / build

'SyntaxFactory.SwitchStatement(SyntaxToken, SyntaxToken, ExpressionSyntax, SyntaxToken, SyntaxToken, SyntaxList<SwitchSectionSyntax>, SyntaxToken)' is a method, which is not valid in the given context
action.Invoke(switchStatement);
builder.AddBody(switchStatement);
return builder;
}

public static SwitchStatement Switch(this SwitchStatement switchStatement, string switchValue)
{
switchStatement.SwitchValue = switchValue;
return switchStatement;
}

public static SwitchStatement AddReturnCase(this SwitchStatement switchStatement, string condition, string returnItem)
{
switchStatement.SwitchCases.Add(new SwitchCaseStatement { Condition = condition, Action = $"return {returnItem}" });
return switchStatement;
}

public static SwitchStatement AddBreakCase(this SwitchStatement switchStatement, string condition, string action)
{
switchStatement.SwitchCases.Add(new SwitchCaseStatement { Condition = condition, Action = action, IsBreak = true });
return switchStatement;
}
public static SwitchStatement AddDefaultCase(this SwitchStatement switchStatement, string action)
{
switchStatement.DefaultCase = new DefaultCaseStatement { Action = action };
return switchStatement;
}
#endregion

#region if
public static T AddIfStatement<T>(this T builder, string condition, Action<IfStatement> action) where T : MethodBase
{
var ifs = IfStatement.Default.If(condition);

Check failure on line 109 in src/Generators.Shared/Extensions/MethodBuilderExtensions.cs

View workflow job for this annotation

GitHub Actions / build

'SyntaxFactory.IfStatement(ExpressionSyntax, StatementSyntax, ElseClauseSyntax?)' is a method, which is not valid in the given context

Check failure on line 109 in src/Generators.Shared/Extensions/MethodBuilderExtensions.cs

View workflow job for this annotation

GitHub Actions / build

'SyntaxFactory.IfStatement(ExpressionSyntax, StatementSyntax, ElseClauseSyntax?)' is a method, which is not valid in the given context
action.Invoke(ifs);
builder.AddBody(ifs);
return builder;
}
public static IfStatement If(this IfStatement ifStatement, string condition)
{
ifStatement.Condition = condition;
return ifStatement;
}

public static IfStatement AddStatement(this IfStatement ifStatement, params string[] statements)
{
foreach (var statement in statements)
{
ifStatement.IfContents.Add(statement);
}
return ifStatement;
}
#endregion
}
33 changes: 33 additions & 0 deletions src/Generators.Shared/Extensions/PropertyBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Generators.Shared.Builder;

namespace Generators.Shared;

internal static class PropertyBuilderExtensions
{
public static PropertyBuilder PropertyName(this PropertyBuilder builder, string name)
{
builder.Name = name;
return builder;
}
public static PropertyBuilder Readonly(this PropertyBuilder builder)
{
builder.CanRead = true;
builder.CanWrite = false;
return builder;
}

public static PropertyBuilder Writeonly(this PropertyBuilder builder)
{
builder.CanRead = false;
builder.CanWrite = true;
return builder;
}

public static PropertyBuilder Lambda(this PropertyBuilder builder, string body)
{
builder.IsLambdaBody = true;
builder.Readonly();
builder.InitializeWith(body);
return builder;
}
}
13 changes: 12 additions & 1 deletion src/Generators.Shared/RoslynExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,18 @@ public static string FormatFileName(this INamedTypeSymbol interfaceSymbol)
}
}


public static ITypeSymbol GetElementType(this ITypeSymbol typeSymbol)
{
if (typeSymbol.HasInterfaceAll("System.Collections.IEnumerable") && typeSymbol.SpecialType == SpecialType.None)
{
if (typeSymbol is IArrayTypeSymbol a)
{
return a.ElementType;
}
return typeSymbol.GetGenericTypes().First();
}
return typeSymbol;
}

public static IEnumerable<ITypeSymbol> GetGenericTypes(this ITypeSymbol symbol)
{
Expand Down

0 comments on commit 5fa1666

Please sign in to comment.