Skip to content

Commit

Permalink
1.5.2 released
Browse files Browse the repository at this point in the history
  • Loading branch information
vipwan committed Nov 8, 2024
1 parent 1b4a14b commit 4fdc051
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
36 changes: 31 additions & 5 deletions Biwen.AutoClassGen.Gen/AutoDtoSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

using System.Threading.Tasks.Sources;
using Desc = Biwen.AutoClassGen.DiagnosticDescriptors;

namespace Biwen.AutoClassGen;
Expand Down Expand Up @@ -81,8 +81,19 @@ private static void HandleAnnotatedNodesDto(Compilation compilation, ImmutableAr
envStringBuilder.AppendLine("using System.Threading.Tasks;");
envStringBuilder.AppendLine("#pragma warning disable");

foreach (ClassDeclarationSyntax node in nodes.AsEnumerable().Cast<ClassDeclarationSyntax>())
foreach (var syntaxNode in nodes.AsEnumerable())
{
//Cast<ClassDeclarationSyntax>()
//Cast<RecordDeclarationSyntax>()

if (syntaxNode is not TypeDeclarationSyntax node)
{
continue;
}

//如果是Record类
var isRecord = syntaxNode is RecordDeclarationSyntax;

//如果不含partial关键字,则不生成
if (!node.Modifiers.Any(x => x.IsKind(SyntaxKind.PartialKeyword)))
{
Expand Down Expand Up @@ -135,7 +146,10 @@ private static void HandleAnnotatedNodesDto(Compilation compilation, ImmutableAr
sb.AppendLine("$classes");
sb.AppendLine("}");
// sb.AppendLine("#pragma warning restore");
string classTemp = $"partial class $className {{ $body }}";
string classTemp = $"partial $isRecord $className {{ $body }}";

classTemp = classTemp.Replace("$isRecord", isRecord ? "record class" : "class");


{
// 排除的属性
Expand Down Expand Up @@ -280,8 +294,19 @@ private static void HandleGenericAnnotatedNodesDto(Compilation compilation, Immu
envStringBuilder.AppendLine("using System.Threading.Tasks;");
envStringBuilder.AppendLine("#pragma warning disable");

foreach (ClassDeclarationSyntax node in nodes.AsEnumerable().Cast<ClassDeclarationSyntax>())
foreach (var nodeSyntax in nodes.AsEnumerable())
{
//Cast<ClassDeclarationSyntax>()
//Cast<RecordDeclarationSyntax>()
if (nodeSyntax is not TypeDeclarationSyntax node)
{
continue;
}

//如果是Record类
var isRecord = nodeSyntax is RecordDeclarationSyntax;


//如果不含partial关键字,则不生成
if (!node.Modifiers.Any(x => x.IsKind(SyntaxKind.PartialKeyword)))
{
Expand Down Expand Up @@ -327,7 +352,8 @@ private static void HandleGenericAnnotatedNodesDto(Compilation compilation, Immu
sb.AppendLine("$classes");
sb.AppendLine("}");
// sb.AppendLine("#pragma warning restore");
string classTemp = $"partial class $className {{ $body }}";
string classTemp = $"partial $isRecord $className {{ $body }}";
classTemp = classTemp.Replace("$isRecord", isRecord ? "record class" : "class");

{
// 排除的属性
Expand Down
2 changes: 1 addition & 1 deletion Biwen.AutoClassGen.Gen/Biwen.AutoClassGen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<AnalysisLevel>6.0-all</AnalysisLevel>
<Authors>万雅虎</Authors>
<PackageVersion>1.5.1.3</PackageVersion>
<PackageVersion>1.5.2</PackageVersion>
<Version>$(PackageVersion)</Version>
<FileVersion>$(PackageVersion)</FileVersion>
</PropertyGroup>
Expand Down
18 changes: 18 additions & 0 deletions Biwen.AutoClassGen.TestConsole/Dtos/UserDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@ public partial class User3Dto
}


[AutoDto(typeof(User), nameof(User.Email))]
public partial record class User4Dto
{
public string? Wooo { get; set; }
}

[AutoDto<User>(nameof(User.Id))]
public partial record class User5Dto(int Id)
{
/// <summary>
/// 如果存在主构造函数,必须有对应的参数,否则Mapper ToDto方法会报错
/// </summary>
public User5Dto() : this(1) { }

public string? Wooo { get; set; }
}


[AutoDto<VenueImage>]
public partial class VenueImageDto
{
Expand Down
4 changes: 4 additions & 0 deletions Biwen.AutoClassGen.TestConsole/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
### Notice

当前项目是一个测试项目,用于测试自动生成的代码,因此有意的抛出异常和警告等信息。
如果需要调试代码,请调试编译`Biwen.AutoClassGen`项目,注意查看输出信息。

0 comments on commit 4fdc051

Please sign in to comment.