Skip to content

Commit 02cd494

Browse files
committed
add option '--generate-records' to improve generating of record types
1 parent c295235 commit 02cd494

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

src/Smdn.Reflection.ReverseGenerating.ListApi/Smdn.Reflection.ReverseGenerating.ListApi.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ SPDX-License-Identifier: MIT
3838
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" Condition="$(TargetFramework.StartsWith('net6.')) Or $(TargetFramework.StartsWith('netcoreapp'))" />
3939
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" Condition="$(TargetFramework.StartsWith('net8.'))" />
4040

41-
<ProjectOrPackageReference ReferencePackageVersion="[1.3.0,2.0.0)" Include="..\Smdn.Reflection.ReverseGenerating.ListApi.Core\Smdn.Reflection.ReverseGenerating.ListApi.Core.csproj" />
41+
<ProjectOrPackageReference ReferencePackageVersion="[1.4.0,2.0.0)" Include="..\Smdn.Reflection.ReverseGenerating.ListApi.Core\Smdn.Reflection.ReverseGenerating.ListApi.Core.csproj" />
4242
</ItemGroup>
4343

4444
<ItemGroup Condition=" '$(EnableFeature_BuildProjectFile)' == 'True' ">

src/Smdn.Reflection.ReverseGenerating.ListApi/Smdn.Reflection.ReverseGenerating.ListApi/RootCommandImplementation.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ public sealed class RootCommandImplementation : ICommandHandler {
104104
description: "Generates declarations with nullable annotations.",
105105
getDefaultValue: static () => true
106106
);
107+
private static readonly Option<bool> OptionGenerateRecords = new(
108+
aliases: new[] { "--generate-records" },
109+
description: "Generates record type declarations and hides compiler-generated mebers.",
110+
getDefaultValue: static () => true
111+
);
107112
// cSpell:enable
108113

109114
private readonly Microsoft.Extensions.Logging.ILogger? logger;
@@ -130,6 +135,7 @@ internal Command CreateCommand()
130135
OptionGenerateMethodBody,
131136
OptionGenerateStaticMembersFirst,
132137
OptionGenerateNullableAnnotations,
138+
OptionGenerateRecords,
133139
};
134140

135141
rootCommand.Handler = this;
@@ -161,6 +167,10 @@ private static ApiListWriterOptions GetApiListWriterOptions(ParseResult parseRes
161167
options.Writer.OrderStaticMembersFirst = parseResult.GetValueForOption(OptionGenerateStaticMembersFirst);
162168
options.Writer.WriteNullableAnnotationDirective = parseResult.GetValueForOption(OptionGenerateNullableAnnotations);
163169

170+
options.TypeDeclaration.EnableRecordTypes =
171+
options.TypeDeclaration.OmitRecordImplicitInterface =
172+
options.Writer.OmitCompilerGeneratedRecordEqualityMethods = parseResult.GetValueForOption(OptionGenerateRecords);
173+
164174
options.AttributeDeclaration.TypeFilter = AttributeFilter.Default;
165175

166176
options.ValueDeclaration.UseDefaultLiteral = true;

tests/Smdn.Reflection.ReverseGenerating.ListApi/Smdn.Reflection.ReverseGenerating.ListApi/RootCommandImplementation.GetApiListWriterOptions.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// SPDX-FileCopyrightText: 2021 smdn <smdn@smdn.jp>
22
// SPDX-License-Identifier: MIT
33
using System;
4+
45
using NUnit.Framework;
5-
using Smdn.Reflection.ReverseGenerating;
66

77
namespace Smdn.Reflection.ReverseGenerating.ListApi;
88

@@ -77,4 +77,19 @@ public void GetApiListWriterOptions_GenerateNullableAnnotations(string args, boo
7777

7878
Assert.That(options.Writer.WriteNullableAnnotationDirective, Is.EqualTo(expected), $"args='{args}'");
7979
}
80+
81+
// cSpell:disable
82+
[TestCase("--generate-records", true)]
83+
[TestCase("--generate-records=true", true)]
84+
[TestCase("--generate-records=false", false)]
85+
[TestCase("", true)]
86+
// cSpell:enable
87+
public void GetApiListWriterOptions_GenerateRecords(string args, bool expected)
88+
{
89+
var options = GetApiListWriterOptions(args);
90+
91+
Assert.That(options.TypeDeclaration.EnableRecordTypes, Is.EqualTo(expected), $"args='{args}'");
92+
Assert.That(options.TypeDeclaration.OmitRecordImplicitInterface, Is.EqualTo(expected), $"args='{args}'");
93+
Assert.That(options.Writer.OmitCompilerGeneratedRecordEqualityMethods, Is.EqualTo(expected), $"args='{args}'");
94+
}
8095
}

0 commit comments

Comments
 (0)