Skip to content

Commit e978a8a

Browse files
committed
feat: suppress withItem and withZeroItems method generation
1 parent 4397281 commit e978a8a

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

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

Lines changed: 13 additions & 3 deletions
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/SourceGenerators/AttributeInfo/FluentCollectionAttributeInfo.cs

Lines changed: 16 additions & 8 deletions
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);

src/M31.FluentApi/Attributes/FluentCollectionAttribute.cs

Lines changed: 5 additions & 5 deletions
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
}

0 commit comments

Comments
 (0)