Skip to content

Commit

Permalink
Merge pull request #670 from SteveDunn/throwhelper
Browse files Browse the repository at this point in the history
Throwhelper
  • Loading branch information
SteveDunn authored Sep 24, 2024
2 parents dedb1f9 + 183f60f commit e27adc6
Show file tree
Hide file tree
Showing 30,588 changed files with 1,479,341 additions and 434,139 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
2,823 changes: 2,823 additions & 0 deletions assets/cavey.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/Vogen/GenerateCodeForTryFrom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private static string GenerateNullCheckAndReturnErrorIfNeeded(SyntaxToken classN
return $$"""
if (value is null)
{
return new ValueObjectOrError<{{className}}>(Validation.Invalid("The value provided was null"));
return new ValueObjectOrError<{{className}}>(Vogen.Validation.Invalid("The value provided was null"));
}
""";
}
Expand Down
3 changes: 2 additions & 1 deletion src/Vogen/GenerateComparableCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ public int CompareTo(object{{item.Nullable.QuestionMarkForOtherReferences}} othe
if(other is {{wrapper}} x)
return CompareTo(x);

throw new global::System.ArgumentException("Cannot compare to object as it is not of type {{wrapper}}", nameof(other));
ThrowHelper.ThrowArgumentException("Cannot compare to object as it is not of type {{wrapper}}", nameof(other));
return 0;
}
""";
}
Expand Down
42 changes: 14 additions & 28 deletions src/Vogen/Generators/ClassGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ string Generate() => $@"
{Util.GenerateCommentForValueProperty(item)}
public {itemUnderlyingType} Value
{{
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
[global::System.Diagnostics.DebuggerStepThroughAttribute]
get
{{
Expand Down Expand Up @@ -77,6 +78,7 @@ string Generate() => $@"
/// </summary>
/// <param name=""value"">The underlying type.</param>
/// <returns>An instance of this type.</returns>
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public static {className} From({itemUnderlyingType} value)
{{
{GenerateNullCheckAndThrowIfNeeded(item)}
Expand All @@ -85,9 +87,7 @@ string Generate() => $@"
{Util.GenerateCallToValidationAndThrowIfRequired(item)}
{className} instance = new {className}(value);
return instance;
return new {className}(value);
}}
{GenerateCodeForTryFrom.GenerateForAClass(item, className, itemUnderlyingType)}
Expand Down Expand Up @@ -121,24 +121,7 @@ string Generate() => $@"
{GenerateHashCodes.GenerateGetHashCodeForAClass(item)}
#if NETCOREAPP3_0_OR_GREATER
[global::System.Diagnostics.CodeAnalysis.MemberNotNullAttribute(nameof(_value))]
[global::System.Diagnostics.CodeAnalysis.MemberNotNullAttribute(nameof(Value))]
#endif
private void EnsureInitialized()
{{
if (!IsInitialized())
{{
#if DEBUG
{DebugGeneration.GenerateMessageForUninitializedValueObject(item)}
#else
global::System.String message = ""Use of uninitialized Value Object."";
#endif
throw new {item.ValidationExceptionFullName}(message);
}}
}}
{Util.GenerateEnsureInitializedMethod(item, readOnly: false)}
{InstanceGeneration.GenerateAnyInstances(tds, item)}
Expand All @@ -147,6 +130,8 @@ private void EnsureInitialized()
{Util.GenerateAnyConversionBodies(tds, item)}
{Util.GenerateDebuggerProxyForClasses(tds, item)}
{Util.GenerateThrowHelper(item)}
}}
{GenerateEfCoreExtensions.GenerateInnerIfNeeded(item)}
{Util.WriteCloseNamespace(item.FullNamespace)}
Expand All @@ -155,12 +140,13 @@ private void EnsureInitialized()

private static string GenerateNullCheckAndThrowIfNeeded(VoWorkItem voWorkItem) =>
voWorkItem.IsTheUnderlyingAValueType ? string.Empty
: $$"""
if (value is null)
{
throw new {{voWorkItem.ValidationExceptionFullName}}("Cannot create a value object with null.");
}

""";
: """
if (value is null)
{
ThrowHelper.ThrowWhenCreatedWithNull();
return default!;
}

""";
}

31 changes: 8 additions & 23 deletions src/Vogen/Generators/RecordClassGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ string Generate() => $@"
{Util.GenerateCommentForValueProperty(item)}
public {itemUnderlyingType} Value
{{
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
[global::System.Diagnostics.DebuggerStepThroughAttribute]
get
{{
Expand Down Expand Up @@ -84,6 +85,7 @@ string Generate() => $@"
/// </summary>
/// <param name=""value"">The underlying type.</param>
/// <returns>An instance of this type.</returns>
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public static {wrapperName} From({itemUnderlyingType} value)
{{
{GenerateNullCheckAndThrowIfNeeded(item)}
Expand All @@ -92,9 +94,7 @@ string Generate() => $@"
{Util.GenerateCallToValidationAndThrowIfRequired(item)}
{wrapperName} instance = new {wrapperName}(value);
return instance;
return new {wrapperName}(value);
}}
{GenerateCodeForTryFrom.GenerateForAStruct(item, wrapperName, itemUnderlyingType)}
Expand Down Expand Up @@ -122,26 +122,9 @@ string Generate() => $@"
{GenerateCodeForTryParse.GenerateAnyHoistedTryParseMethods(item)}{GenerateCodeForParse.GenerateAnyHoistedParseMethods(item)}
{GenerateHashCodes.GenerateGetHashCodeForAClass(item)}
#if NETCOREAPP3_0_OR_GREATER
[global::System.Diagnostics.CodeAnalysis.MemberNotNullAttribute(nameof(_value))]
[global::System.Diagnostics.CodeAnalysis.MemberNotNullAttribute(nameof(Value))]
#endif
private void EnsureInitialized()
{{
if (!IsInitialized())
{{
#if DEBUG
{DebugGeneration.GenerateMessageForUninitializedValueObject(item)}
#else
global::System.String message = ""Use of uninitialized Value Object."";
#endif
throw new {item.ValidationExceptionFullName}(message);
}}
}}
{GenerateHashCodes.GenerateGetHashCodeForAClass(item)}
{Util.GenerateEnsureInitializedMethod(item, readOnly: false)}
{InstanceGeneration.GenerateAnyInstances(tds, item)}
Expand All @@ -151,7 +134,9 @@ private void EnsureInitialized()
{Util.GenerateAnyConversionBodies(tds, item)}
{Util.GenerateDebuggerProxyForClasses(tds, item)}
}}
{Util.GenerateThrowHelper(item)}
}}
{GenerateEfCoreExtensions.GenerateInnerIfNeeded(item)}
{Util.WriteCloseNamespace(item.FullNamespace)}";
}
Expand Down
28 changes: 7 additions & 21 deletions src/Vogen/Generators/RecordStructGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ string Generate() => $@"
{Util.GenerateCommentForValueProperty(item)}
public readonly {itemUnderlyingType} Value
{{
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
[global::System.Diagnostics.DebuggerStepThroughAttribute]
get
{{
Expand Down Expand Up @@ -87,15 +88,14 @@ public readonly {itemUnderlyingType} Value
/// </summary>
/// <param name=""value"">The underlying type.</param>
/// <returns>An instance of this type.</returns>
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public static {wrapperName} From({itemUnderlyingType} value)
{{
{Util.GenerateCallToNormalizeMethodIfNeeded(item)}
{Util.GenerateCallToValidationAndThrowIfRequired(item)}
{wrapperName} instance = new {wrapperName}(value);
return instance;
return new {wrapperName}(value);
}}
{GenerateCodeForTryFrom.GenerateForAStruct(item, wrapperName, itemUnderlyingType)}
Expand Down Expand Up @@ -123,23 +123,7 @@ public readonly {itemUnderlyingType} Value
{GenerateHashCodes.GenerateForAStruct(item)}
#if NETCOREAPP3_0_OR_GREATER
[global::System.Diagnostics.CodeAnalysis.MemberNotNullAttribute(nameof(_value))]
[global::System.Diagnostics.CodeAnalysis.MemberNotNullAttribute(nameof(Value))]
#endif
private readonly void EnsureInitialized()
{{
if (!IsInitialized())
{{
#if DEBUG
{DebugGeneration.GenerateMessageForUninitializedValueObject(item)}
#else
global::System.String message = ""Use of uninitialized Value Object."";
#endif
throw new {item.ValidationExceptionFullName}(message);
}}
}}
{Util.GenerateEnsureInitializedMethod(item, readOnly: true)}
// record enumerates fields - we just want our Value and to throw if it's not initialized.
{Util.GenerateToStringReadOnly(item)}
Expand All @@ -150,6 +134,7 @@ private readonly void EnsureInitialized()
{Util.GenerateDebuggerProxyForStructs(item)}
{Util.GenerateThrowHelper(item)}
}}
{GenerateEfCoreExtensions.GenerateInnerIfNeeded(item)}
{Util.WriteCloseNamespace(item.FullNamespace)}";
Expand All @@ -160,7 +145,8 @@ private static string GenerateNullCheckIfNeeded(VoWorkItem voWorkItem) =>
: $$"""
if (value is null)
{
throw new {{voWorkItem.ValidationExceptionFullName}}("Cannot create a value object with null.");
ThrowHelper.ThrowWhenCreatedWithNull();
return;
}

""";
Expand Down
25 changes: 5 additions & 20 deletions src/Vogen/Generators/StructGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ string Generate() => $@"
public readonly {itemUnderlyingType} Value
{{
[global::System.Diagnostics.DebuggerStepThroughAttribute]
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
get
{{
EnsureInitialized();
Expand Down Expand Up @@ -78,15 +79,14 @@ public readonly {itemUnderlyingType} Value
/// </summary>
/// <param name=""value"">The underlying type.</param>
/// <returns>An instance of this type.</returns>
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public static {structName} From({itemUnderlyingType} value)
{{
{Util.GenerateCallToNormalizeMethodIfNeeded(item)}
{Util.GenerateCallToValidationAndThrowIfRequired(item)}
{structName} instance = new {structName}(value);
return instance;
return new {structName}(value);
}}
{GenerateCodeForTryFrom.GenerateForAStruct(item, structName, itemUnderlyingType)}
Expand Down Expand Up @@ -120,30 +120,15 @@ public readonly {itemUnderlyingType} Value
{Util.GenerateToStringReadOnly(item)}
#if NETCOREAPP3_0_OR_GREATER
[global::System.Diagnostics.CodeAnalysis.MemberNotNullAttribute(nameof(_value))]
[global::System.Diagnostics.CodeAnalysis.MemberNotNullAttribute(nameof(Value))]
#endif
private readonly void EnsureInitialized()
{{
if (!IsInitialized())
{{
#if DEBUG
{DebugGeneration.GenerateMessageForUninitializedValueObject(item)}
#else
global::System.String message = ""Use of uninitialized Value Object."";
#endif
throw new {item.ValidationExceptionFullName}(message);
}}
}}
{Util.GenerateEnsureInitializedMethod(item, readOnly: true)}
{InstanceGeneration.GenerateAnyInstances(tds, item)}
{Util.GenerateAnyConversionBodies(tds, item)}
{Util.GenerateDebuggerProxyForStructs(item)}
{Util.GenerateThrowHelper(item)}
}}
{GenerateEfCoreExtensions.GenerateInnerIfNeeded(item)}
{Util.WriteCloseNamespace(item.FullNamespace)}";
Expand Down
Loading

0 comments on commit e27adc6

Please sign in to comment.