Skip to content

Commit de42384

Browse files
committed
Added combination out of AllowNull and NotNull to not-nullable reference parameters of ValidateFactoryArguments
* Ensures that the value is validated/handled by the developer, if not then a compiler warning is emitted.
1 parent 7485b19 commit de42384

File tree

14 files changed

+45
-2
lines changed

14 files changed

+45
-2
lines changed

samples/Thinktecture.Runtime.Extensions.Benchmarking/Database/Description.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ static partial void ValidateFactoryArguments(ref ValidationResult? validationRes
1212
{
1313
if (String.IsNullOrWhiteSpace(value))
1414
{
15+
value = null!;
1516
validationResult = new ValidationResult("Description cannot be empty.");
1617
return;
1718
}

samples/Thinktecture.Runtime.Extensions.Benchmarking/Database/DescriptionStruct.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ static partial void ValidateFactoryArguments(ref ValidationResult? validationRes
1212
{
1313
if (String.IsNullOrWhiteSpace(value))
1414
{
15+
value = null!;
1516
validationResult = new ValidationResult("Description cannot be empty.");
1617
return;
1718
}

samples/Thinktecture.Runtime.Extensions.Benchmarking/Database/Name.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ static partial void ValidateFactoryArguments(ref ValidationResult? validationRes
1212
{
1313
if (String.IsNullOrWhiteSpace(value))
1414
{
15+
value = null!;
1516
validationResult = new ValidationResult("Name cannot be empty.");
1617
return;
1718
}

samples/Thinktecture.Runtime.Extensions.Benchmarking/Database/NameStruct.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ static partial void ValidateFactoryArguments(ref ValidationResult? validationRes
1212
{
1313
if (String.IsNullOrWhiteSpace(value))
1414
{
15+
value = null!;
1516
validationResult = new ValidationResult("Name cannot be empty.");
1617
return;
1718
}

samples/Thinktecture.Runtime.Extensions.MessagePack.Samples/ProductNameWithMessagePackFormatter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ static partial void ValidateFactoryArguments(ref ValidationResult? validationRes
1313
{
1414
if (String.IsNullOrWhiteSpace(value))
1515
{
16+
value = null!;
1617
validationResult = new ValidationResult("Product name cannot be empty.");
1718
return;
1819
}

samples/Thinktecture.Runtime.Extensions.Samples/ValueObjects/OtherProductName.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ static partial void ValidateFactoryArguments(ref ValidationResult? validationRes
1313
{
1414
if (String.IsNullOrWhiteSpace(value))
1515
{
16+
value = null!;
1617
validationResult = new ValidationResult("Product name cannot be empty.");
1718
return;
1819
}

samples/Thinktecture.Runtime.Extensions.Samples/ValueObjects/ProductName.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ static partial void ValidateFactoryArguments(ref ValidationResult? validationRes
1414
{
1515
if (String.IsNullOrWhiteSpace(value))
1616
{
17+
value = null!;
1718
validationResult = new ValidationResult("Product name cannot be empty.");
1819
return;
1920
}

samples/Thinktecture.Runtime.Extensions.Samples/ValueObjects/ProductNameStruct.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ static partial void ValidateFactoryArguments(ref ValidationResult? validationRes
1414
{
1515
if (String.IsNullOrWhiteSpace(value))
1616
{
17+
value = null!;
1718
validationResult = new ValidationResult("Product name cannot be empty.");
1819
return;
1920
}

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/ValueObjects/ValueObjectCodeGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ private void GenerateValidateFactoryArguments()
555555

556556
_sb.Append("static partial ").Append(_state.FactoryValidationReturnType ?? "void").Append(" ").Append(Constants.Methods.VALIDATE_FACTORY_ARGUMENTS).Append("(ref global::System.ComponentModel.DataAnnotations.ValidationResult? validationResult");
557557

558-
_sb.RenderArgumentsWithType(fieldsAndProperties, "ref ", leadingComma: true);
558+
_sb.RenderArgumentsWithType(fieldsAndProperties, "ref ", leadingComma: true, addAllowNullNotNullCombi: true);
559559

560560
_sb.Append(");");
561561
}

src/Thinktecture.Runtime.Extensions.SourceGenerator/Extensions/StringBuilderExtensions.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Text;
2+
using Microsoft.CodeAnalysis;
23
using Thinktecture.CodeAnalysis;
34
using Thinktecture.CodeAnalysis.SmartEnums;
45

@@ -38,14 +39,19 @@ public static void RenderArgumentsWithType(
3839
string comma = ", ",
3940
bool leadingComma = false,
4041
bool trailingComma = false,
41-
bool useNullableTypes = false)
42+
bool useNullableTypes = false,
43+
bool addAllowNullNotNullCombi = false)
4244
{
4345
for (var i = 0; i < members.Count; i++)
4446
{
4547
if (leadingComma || i > 0)
4648
sb.Append(comma);
4749

4850
var member = members[i];
51+
52+
if (addAllowNullNotNullCombi && member.IsReferenceType && member.NullableAnnotation != NullableAnnotation.Annotated)
53+
sb.Append("[global::System.Diagnostics.CodeAnalysis.AllowNullAttribute, global::System.Diagnostics.CodeAnalysis.NotNullAttribute] ");
54+
4955
sb.Append(prefix).Append(member.TypeFullyQualifiedWithNullability);
5056

5157
if (useNullableTypes && !member.IsNullableStruct)

0 commit comments

Comments
 (0)