Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 33 additions & 14 deletions src/LightObjects.Generated/GeneratedIdentifierSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,23 @@ private static bool Filter(SyntaxNode syntaxNode, CancellationToken cancellation

string? declaredValueType;
string? fullValueType;
var supportsProviderBasedTryParse = false;
switch (typeArgument.SpecialType)
{
case SpecialType.System_Int16:
declaredValueType = "short";
fullValueType = "Int16";
supportsProviderBasedTryParse = true;
break;
case SpecialType.System_Int32:
declaredValueType = "int";
fullValueType = "Int32";
supportsProviderBasedTryParse = true;
break;
case SpecialType.System_Int64:
declaredValueType = "long";
fullValueType = "Int64";
supportsProviderBasedTryParse = true;
break;
case SpecialType.System_String:
declaredValueType = "string";
Expand All @@ -115,7 +119,15 @@ private static bool Filter(SyntaxNode syntaxNode, CancellationToken cancellation
break;
}

var symbol = new Identifier(containingDeclarations, symbolName, isStruct, isPublic, declaredValueType, fullValueType);
var symbol = new Identifier(
containingDeclarations,
symbolName,
isStruct,
isPublic,
declaredValueType,
fullValueType,
supportsProviderBasedTryParse
);

return symbol;
}
Expand All @@ -131,6 +143,8 @@ private static void GenerateIdentifier(SourceProductionContext context, Immutabl
var declaredValueType = symbol.DeclaredValueType;
var fullValueType = symbol.FullValueType;

var supportsProviderBasedTryParse = symbol.SupportsProviderBasedTryParse;

var source = new StringBuilder();

source.AppendLine($"""
Expand Down Expand Up @@ -277,19 +291,22 @@ public static bool TryParse(string s, out {{symbolName}} identifier)
"""
);

source.AppendLine($$"""
/// <inheritdoc />
public static bool TryParse(string s, IFormatProvider provider, out {{symbolName}} identifier)
{
if ({{declaredValueType}}.TryParse(s, provider, out var value))
return TryCreate(value).IsSuccess(out identifier);

identifier = default;
return false;
}
if (supportsProviderBasedTryParse)
{
source.AppendLine($$"""
/// <inheritdoc />
public static bool TryParse(string s, IFormatProvider provider, out {{symbolName}} identifier)
{
if ({{declaredValueType}}.TryParse(s, provider, out var value))
return TryCreate(value).IsSuccess(out identifier);

"""
);
identifier = default;
return false;
}

"""
);
}
}

if (isStruct)
Expand Down Expand Up @@ -743,7 +760,8 @@ private readonly record struct Identifier(
bool IsStruct,
bool IsPublic,
string DeclaredValueType,
string FullValueType
string FullValueType,
bool SupportsProviderBasedTryParse
)
{
public EquatableImmutableArray<Declaration> ContainingDeclarations { get; } = ContainingDeclarations;
Expand All @@ -752,5 +770,6 @@ string FullValueType
public bool IsPublic { get; } = IsPublic;
public string DeclaredValueType { get; } = DeclaredValueType;
public string FullValueType { get; } = FullValueType;
public bool SupportsProviderBasedTryParse { get; } = SupportsProviderBasedTryParse;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,6 @@ public readonly partial struct TestGuidId :
return false;
}

/// <inheritdoc />
public static bool TryParse(string s, IFormatProvider provider, out TestGuidId identifier)
{
if (Guid.TryParse(s, provider, out var value))
return TryCreate(value).IsSuccess(out identifier);

identifier = default;
return false;
}

/// <inheritdoc />
public bool Equals(TestGuidId other)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,6 @@ public readonly partial struct TestGuidId :
return false;
}

/// <inheritdoc />
public static bool TryParse(string s, IFormatProvider provider, out TestGuidId identifier)
{
if (Guid.TryParse(s, provider, out var value))
return TryCreate(value).IsSuccess(out identifier);

identifier = default;
return false;
}

/// <inheritdoc />
public bool Equals(TestGuidId other)
{
Expand Down
Loading