Skip to content

Commit a8b2133

Browse files
authored
feat: Suppress collection methods (#41)
* fix(ExampleProject): suppress function not used warning * feat: suppress withItem and withZeroItems method generation * test: CollectionMemberClassWithSuppression * chore: increase nuget versions to 1.10.0 * docs(readme): update * docs(readme): fix typo
1 parent 84dac02 commit a8b2133

File tree

11 files changed

+256
-22
lines changed

11 files changed

+256
-22
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ PM> Install-Package M31.FluentApi
3737
A package reference will be added to your `csproj` file. Moreover, since this library provides code via source code generation, consumers of your project don't need the reference to `M31.FluentApi`. Therefore, it is recommended to use the `PrivateAssets` metadata tag:
3838

3939
```xml
40-
<PackageReference Include="M31.FluentApi" Version="1.9.1" PrivateAssets="all"/>
40+
<PackageReference Include="M31.FluentApi" Version="1.10.0" PrivateAssets="all"/>
4141
```
4242

4343
If you would like to examine the generated code, you may emit it by adding the following lines to your `csproj` file:
@@ -222,11 +222,11 @@ FluentCollection(
222222
int builderStep,
223223
string singularName,
224224
string withItems = "With{Name}",
225-
string withItem = "With{SingularName}",
226-
string withZeroItems = "WithZero{Name}")
225+
string? withItem = "With{SingularName}",
226+
string? withZeroItems = "WithZero{Name}")
227227
```
228228

229-
Can be used instead of the `FluentMember` attribute if the decorated member is a collection. This attribute generates methods for setting multiple items, one item and zero items. The supported collection types can be seen in the source file [CollectionInference.cs](src/M31.FluentApi.Generator/SourceGenerators/Collections/CollectionInference.cs).
229+
Can be used instead of the `FluentMember` attribute if the decorated member is a collection. This attribute generates methods for setting multiple items, one item and zero items. The supported collection types can be seen in the source file [CollectionInference.cs](src/M31.FluentApi.Generator/SourceGenerators/Collections/CollectionInference.cs). If `withItem` or `withZeroItems` is set to `null`, the corresponding method will not be generated.
230230

231231
```cs
232232
[FluentCollection(5, "Friend", "WhoseFriendsAre", "WhoseFriendIs", "WhoHasNoFriends")]

src/ExampleProject/Program.cs

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System.Text.Json;
22
using ExampleProject;
33

4+
#pragma warning disable CS8321 // Local function is declared but never used
5+
46
// Student
57
//
68

src/M31.FluentApi.Generator/CodeGeneration/CodeBoardActors/MethodCreation/Collections/CollectionMethodCreator.cs

+13-3
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,13 @@ internal BuilderMethod CreateWithItemsParamsMethod(MethodCreator methodCreator)
8989
computeValueCode);
9090
}
9191

92-
internal BuilderMethod CreateWithItemMethod(MethodCreator methodCreator)
92+
internal BuilderMethod? CreateWithItemMethod(MethodCreator methodCreator)
9393
{
94+
if (collectionAttributeInfo.WithItem == null)
95+
{
96+
return null;
97+
}
98+
9499
Parameter parameter = new Parameter(genericTypeArgument, collectionAttributeInfo.SingularNameInCamelCase);
95100
return methodCreator.CreateMethodWithComputedValue(
96101
symbolInfo,
@@ -101,7 +106,7 @@ internal BuilderMethod CreateWithItemMethod(MethodCreator methodCreator)
101106

102107
internal BuilderMethod? CreateWithItemLambdaMethod(MethodCreator methodCreator)
103108
{
104-
if (collectionAttributeInfo.LambdaBuilderInfo == null)
109+
if (collectionAttributeInfo.WithItem == null || collectionAttributeInfo.LambdaBuilderInfo == null)
105110
{
106111
return null;
107112
}
@@ -123,8 +128,13 @@ internal BuilderMethod CreateWithItemMethod(MethodCreator methodCreator)
123128
computeValueCode);
124129
}
125130

126-
internal BuilderMethod CreateWithZeroItemsMethod(MethodCreator methodCreator)
131+
internal BuilderMethod? CreateWithZeroItemsMethod(MethodCreator methodCreator)
127132
{
133+
if (collectionAttributeInfo.WithZeroItems == null)
134+
{
135+
return null;
136+
}
137+
128138
string collectionWithZeroItemsCode = CreateCollectionWithZeroItems(genericTypeArgument);
129139
return methodCreator.CreateMethodWithFixedValue(
130140
symbolInfo,

src/M31.FluentApi.Generator/M31.FluentApi.Generator.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1212
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1313
<SuppressDependenciesWhenPacking>true</SuppressDependenciesWhenPacking>
14-
<PackageVersion>1.9.1</PackageVersion>
14+
<PackageVersion>1.10.0</PackageVersion>
1515
<Authors>Kevin Schaal</Authors>
1616
<Description>The generator package for M31.FluentAPI. Don't install this package explicitly, install M31.FluentAPI instead.</Description>
1717
<PackageTags>fluentapi fluentbuilder fluentinterface fluentdesign fluent codegeneration</PackageTags>

src/M31.FluentApi.Generator/SourceGenerators/AttributeInfo/FluentCollectionAttributeInfo.cs

+16-8
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ private FluentCollectionAttributeInfo(
1010
int builderStep,
1111
string singularName,
1212
string withItems,
13-
string withItem,
14-
string withZeroItems,
13+
string? withItem,
14+
string? withZeroItems,
1515
LambdaBuilderInfo? lambdaBuilderInfo)
1616
: base(builderStep)
1717
{
@@ -26,8 +26,8 @@ private FluentCollectionAttributeInfo(
2626
internal string SingularName { get; }
2727
internal string SingularNameInCamelCase { get; }
2828
internal string WithItems { get; }
29-
internal string WithItem { get; }
30-
internal string WithZeroItems { get; }
29+
internal string? WithItem { get; }
30+
internal string? WithZeroItems { get; }
3131
internal LambdaBuilderInfo? LambdaBuilderInfo { get; }
3232
internal override string FluentMethodName => WithItems;
3333

@@ -36,12 +36,20 @@ internal static FluentCollectionAttributeInfo Create(
3636
string memberName,
3737
LambdaBuilderInfo? lambdaBuilderInfo)
3838
{
39-
(int builderStep, string singularName, string withItems, string withItem, string withZeroItems) =
40-
attributeData.GetConstructorArguments<int, string, string, string, string>();
39+
(int builderStep, string singularName, string withItems, string? withItem, string? withZeroItems) =
40+
attributeData.GetConstructorArguments<int, string, string, string?, string?>();
4141

4242
withItems = NameCreator.CreateName(withItems, memberName, singularName);
43-
withItem = NameCreator.CreateName(withItem, memberName, singularName);
44-
withZeroItems = NameCreator.CreateName(withZeroItems, memberName, singularName);
43+
44+
if (withItem != null)
45+
{
46+
withItem = NameCreator.CreateName(withItem, memberName, singularName);
47+
}
48+
49+
if (withZeroItems != null)
50+
{
51+
withZeroItems = NameCreator.CreateName(withZeroItems, memberName, singularName);
52+
}
4553

4654
return new FluentCollectionAttributeInfo(
4755
builderStep, singularName, withItems, withItem, withZeroItems, lambdaBuilderInfo);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// <auto-generated/>
2+
// This code was generated by the library M31.FluentAPI.
3+
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
4+
5+
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
6+
#nullable enable
7+
8+
using System.Collections.Generic;
9+
10+
namespace M31.FluentApi.Tests.CodeGeneration.TestClasses.Abstract.CollectionMemberClassWithSuppression;
11+
12+
public class CreateStudent :
13+
CreateStudent.ICreateStudent,
14+
CreateStudent.IWhoseFriendsAre,
15+
CreateStudent.IWithPets,
16+
CreateStudent.IWithBackpackContent
17+
{
18+
private readonly Student student;
19+
20+
private CreateStudent()
21+
{
22+
student = new Student();
23+
}
24+
25+
public static ICreateStudent InitialStep()
26+
{
27+
return new CreateStudent();
28+
}
29+
30+
public static IWithPets WhoseFriendsAre(System.Collections.Generic.List<string> friends)
31+
{
32+
CreateStudent createStudent = new CreateStudent();
33+
createStudent.student.Friends = friends;
34+
return createStudent;
35+
}
36+
37+
public static IWithPets WhoseFriendsAre(params string[] friends)
38+
{
39+
CreateStudent createStudent = new CreateStudent();
40+
createStudent.student.Friends = new List<string>(friends);
41+
return createStudent;
42+
}
43+
44+
IWithPets IWhoseFriendsAre.WhoseFriendsAre(System.Collections.Generic.List<string> friends)
45+
{
46+
student.Friends = friends;
47+
return this;
48+
}
49+
50+
IWithPets IWhoseFriendsAre.WhoseFriendsAre(params string[] friends)
51+
{
52+
student.Friends = new List<string>(friends);
53+
return this;
54+
}
55+
56+
IWithBackpackContent IWithPets.WithPets(params string[] pets)
57+
{
58+
student.Pets = pets;
59+
return this;
60+
}
61+
62+
Student IWithBackpackContent.WithBackpackContent(System.Collections.Generic.HashSet<string> backpackContent)
63+
{
64+
student.BackpackContent = backpackContent;
65+
return student;
66+
}
67+
68+
Student IWithBackpackContent.WithBackpackContent(params string[] backpackContent)
69+
{
70+
student.BackpackContent = new HashSet<string>(backpackContent);
71+
return student;
72+
}
73+
74+
public interface ICreateStudent : IWhoseFriendsAre
75+
{
76+
}
77+
78+
public interface IWhoseFriendsAre
79+
{
80+
IWithPets WhoseFriendsAre(System.Collections.Generic.List<string> friends);
81+
82+
IWithPets WhoseFriendsAre(params string[] friends);
83+
}
84+
85+
public interface IWithPets
86+
{
87+
IWithBackpackContent WithPets(params string[] pets);
88+
}
89+
90+
public interface IWithBackpackContent
91+
{
92+
Student WithBackpackContent(System.Collections.Generic.HashSet<string> backpackContent);
93+
94+
Student WithBackpackContent(params string[] backpackContent);
95+
}
96+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// <auto-generated/>
2+
// This code was generated by the library M31.FluentAPI.
3+
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
4+
5+
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
6+
#nullable enable
7+
8+
using System.Collections.Generic;
9+
10+
namespace M31.FluentApi.Tests.CodeGeneration.TestClasses.Abstract.CollectionMemberClassWithSuppression;
11+
12+
public class CreateStudent :
13+
CreateStudent.ICreateStudent,
14+
CreateStudent.IWhoseFriendsAre,
15+
CreateStudent.IWithPets,
16+
CreateStudent.IWithBackpackContent
17+
{
18+
private readonly Student student;
19+
20+
private CreateStudent()
21+
{
22+
student = new Student();
23+
}
24+
25+
public static ICreateStudent InitialStep()
26+
{
27+
return new CreateStudent();
28+
}
29+
30+
public static IWithPets WhoseFriendsAre(System.Collections.Generic.List<string> friends)
31+
{
32+
CreateStudent createStudent = new CreateStudent();
33+
createStudent.student.Friends = friends;
34+
return createStudent;
35+
}
36+
37+
public static IWithPets WhoseFriendsAre(params string[] friends)
38+
{
39+
CreateStudent createStudent = new CreateStudent();
40+
createStudent.student.Friends = new List<string>(friends);
41+
return createStudent;
42+
}
43+
44+
IWithPets IWhoseFriendsAre.WhoseFriendsAre(System.Collections.Generic.List<string> friends)
45+
{
46+
student.Friends = friends;
47+
return this;
48+
}
49+
50+
IWithPets IWhoseFriendsAre.WhoseFriendsAre(params string[] friends)
51+
{
52+
student.Friends = new List<string>(friends);
53+
return this;
54+
}
55+
56+
IWithBackpackContent IWithPets.WithPets(params string[] pets)
57+
{
58+
student.Pets = pets;
59+
return this;
60+
}
61+
62+
Student IWithBackpackContent.WithBackpackContent(System.Collections.Generic.HashSet<string> backpackContent)
63+
{
64+
student.BackpackContent = backpackContent;
65+
return student;
66+
}
67+
68+
Student IWithBackpackContent.WithBackpackContent(params string[] backpackContent)
69+
{
70+
student.BackpackContent = new HashSet<string>(backpackContent);
71+
return student;
72+
}
73+
74+
public interface ICreateStudent : IWhoseFriendsAre
75+
{
76+
}
77+
78+
public interface IWhoseFriendsAre
79+
{
80+
IWithPets WhoseFriendsAre(System.Collections.Generic.List<string> friends);
81+
82+
IWithPets WhoseFriendsAre(params string[] friends);
83+
}
84+
85+
public interface IWithPets
86+
{
87+
IWithBackpackContent WithPets(params string[] pets);
88+
}
89+
90+
public interface IWithBackpackContent
91+
{
92+
Student WithBackpackContent(System.Collections.Generic.HashSet<string> backpackContent);
93+
94+
Student WithBackpackContent(params string[] backpackContent);
95+
}
96+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Non-nullable member is uninitialized
2+
#pragma warning disable CS8618
3+
// ReSharper disable All
4+
5+
using System.Collections.Generic;
6+
using M31.FluentApi.Attributes;
7+
8+
namespace M31.FluentApi.Tests.CodeGeneration.TestClasses.Abstract.CollectionMemberClassWithSuppression;
9+
10+
[FluentApi]
11+
public class Student
12+
{
13+
[FluentCollection(0, "Friend", "WhoseFriendsAre", null, null)]
14+
public List<string> Friends { get; set; }
15+
16+
[FluentCollection(1, "Pet", withItem: null, withZeroItems: null)]
17+
public string[] Pets { get; set; }
18+
19+
[FluentCollection(2, "BackpackContent", "WithBackpackContent", null, null)]
20+
public HashSet<string> BackpackContent { get; set; }
21+
}

src/M31.FluentApi.Tests/CodeGeneration/TestDataProvider.cs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ internal class TestDataProvider : IEnumerable<object[]>
1313
new object[] { "Abstract", "AliasNamespaceClass", "Student" },
1414
new object[] { "Abstract", "CollectionInterfaceMemberClass", "Student" },
1515
new object[] { "Abstract", "CollectionMemberClass", "Student" },
16+
new object[] { "Abstract", "CollectionMemberClassWithSuppression", "Student" },
1617
new object[] { "Abstract", "CollectionNullableArrayClass", "Student" },
1718
new object[] { "Abstract", "ContinueWithAfterCompoundClass", "Student" },
1819
new object[] { "Abstract", "ContinueWithInForkClass", "Student" },

src/M31.FluentApi/Attributes/FluentCollectionAttribute.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ public class FluentCollectionAttribute : Attribute
1414
/// <param name="builderStep">The builder step in which the collection can be set.</param>
1515
/// <param name="singularName">The singular of the collection name.</param>
1616
/// <param name="withItems">The name of the builder method that sets multiple items.</param>
17-
/// <param name="withItem">The name of the builder method that sets a single item.</param>
17+
/// <param name="withItem">The name of the builder method that sets a single item.
18+
/// If set to null, the builder method will not be generated.</param>
1819
/// <param name="withZeroItems">The name of the builder method that sets zero items.
19-
/// </param>
20+
/// If set to null, the builder method will not be generated.</param>
2021
public FluentCollectionAttribute(
2122
int builderStep,
2223
string singularName,
2324
string withItems = "With{Name}",
24-
string withItem = "With{SingularName}",
25-
string withZeroItems = "WithZero{Name}")
25+
string? withItem = "With{SingularName}",
26+
string? withZeroItems = "WithZero{Name}")
2627
{
27-
2828
}
2929
}

src/M31.FluentApi/M31.FluentApi.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<ImplicitUsings>enable</ImplicitUsings>
88
<GenerateDocumentationFile>true</GenerateDocumentationFile>
99
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
10-
<PackageVersion>1.9.1</PackageVersion>
10+
<PackageVersion>1.10.0</PackageVersion>
1111
<Authors>Kevin Schaal</Authors>
1212
<Description>Generate fluent builders in C#.</Description>
1313
<PackageTags>fluentapi fluentbuilder fluentinterface fluentdesign fluent codegeneration</PackageTags>

0 commit comments

Comments
 (0)