Skip to content

Commit

Permalink
Add support for string identifiers.
Browse files Browse the repository at this point in the history
  • Loading branch information
jscarle committed Oct 25, 2024
1 parent 39c272c commit 1bdc3cf
Show file tree
Hide file tree
Showing 13 changed files with 1,748 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ private static bool Filter(SyntaxNode syntaxNode, CancellationToken cancellation
declaredValueType = "long";
fullValueType = "Int64";
break;
case SpecialType.System_String:
declaredValueType = "string";
fullValueType = "String";
break;
default:
if (typeArgument is not { Name: "Guid", ContainingNamespace: { Name: "System", ContainingNamespace.IsGlobalNamespace: true } })
return null;
Expand Down Expand Up @@ -153,7 +157,16 @@ namespace {structNamespace};
[JsonConverter(typeof({{structName}}JsonConverter))]
readonly partial struct {{structName}} :
ICreatableValueObject<{{declaredValueType}}, {{structName}}>,
IParsableValueObject<{{structName}}>,
"""
);

if (declaredValueType != "string")
source.AppendLine($$"""
IParsableValueObject<{{structName}}>,
"""
);

source.AppendLine($$"""
IValueObject<{{declaredValueType}}, {{structName}}>,
IComparable<{{structName}}>,
IComparable
Expand Down Expand Up @@ -216,52 +229,55 @@ namespace {structNamespace};
"""
);

source.AppendLine($$"""
/// <inheritdoc />
public static {{structName}} Parse(string s)
{
var result = TryParse(s);
if (result.IsSuccess(out var identifier, out var error))
return identifier;
throw new ValueObjectException(error.Message);
}
if (declaredValueType != "string")
{
source.AppendLine($$"""
/// <inheritdoc />
public static {{structName}} Parse(string s)
{
var result = TryParse(s);
if (result.IsSuccess(out var identifier, out var error))
return identifier;
throw new ValueObjectException(error.Message);
}
"""
);
"""
);

source.AppendLine($$"""
/// <inheritdoc />
public static Result<{{structName}}> TryParse(string s)
{
if ({{declaredValueType}}.TryParse(s, out var value))
return TryCreate(value);
return Result.Fail<{{structName}}>("The string is not a valid identifier.");
}
source.AppendLine($$"""
/// <inheritdoc />
public static Result<{{structName}}> TryParse(string s)
{
if ({{declaredValueType}}.TryParse(s, out var value))
return TryCreate(value);
return Result.Fail<{{structName}}>("The string is not a valid identifier.");
}
"""
);
"""
);

source.AppendLine($$"""
/// <inheritdoc />
public static bool TryParse(string s, out {{structName}} identifier)
{
return TryParse(s).IsSuccess(out identifier);
}
source.AppendLine($$"""
/// <inheritdoc />
public static bool TryParse(string s, out {{structName}} identifier)
{
return TryParse(s).IsSuccess(out identifier);
}
"""
);
"""
);

source.AppendLine($$"""
/// <inheritdoc />
public static bool TryParse(string s, IFormatProvider provider, out {{structName}} identifier)
{
return TryParse(s).IsSuccess(out identifier);
}
source.AppendLine($$"""
/// <inheritdoc />
public static bool TryParse(string s, IFormatProvider provider, out {{structName}} identifier)
{
return TryParse(s).IsSuccess(out identifier);
}
"""
);
"""
);
}

source.AppendLine($$"""
/// <inheritdoc />
Expand All @@ -283,7 +299,7 @@ public override bool Equals(object? obj)
"""
);

if (declaredValueType == "Guid")
if (declaredValueType is "long" or "Guid" or "string")
source.AppendLine("""
/// <inheritdoc />
public override int GetHashCode()
Expand Down Expand Up @@ -403,16 +419,17 @@ public int CompareTo(object? obj)
"""
);

source.AppendLine($$"""
/// <summary>Gets the underlying value of the <see cref="{{structName}}" />.</summary>
/// <returns>The underlying value of the <see cref="{{structName}}" />.</returns>
public {{declaredValueType}} To{{fullValueType}}()
{
return _value;
}
if (declaredValueType != "string")
source.AppendLine($$"""
/// <summary>Gets the underlying value of the <see cref="{{structName}}" />.</summary>
/// <returns>The underlying value of the <see cref="{{structName}}" />.</returns>
public {{declaredValueType}} To{{fullValueType}}()
{
return _value;
}
"""
);
"""
);

if (declaredValueType == "Guid")
source.AppendLine("""
Expand All @@ -422,6 +439,16 @@ public override string ToString()
return _value.ToString();
}
"""
);
else if (declaredValueType == "string")
source.AppendLine("""
/// <inheritdoc />
public override string ToString()
{
return _value;
}
"""
);
else
Expand All @@ -439,6 +466,17 @@ public override string ToString()
source.AppendLine($$"""
private static Result Validate({{declaredValueType}} value)
{
return Result.Ok();
}
"""
);
else if (declaredValueType == "string")
source.AppendLine($$"""
private static Result Validate({{declaredValueType}} value)
{
if (string.IsNullOrWhiteSpace(value))
return Result.Fail("The value must not be empty.");
return Result.Ok();
}
"""
Expand Down Expand Up @@ -491,15 +529,27 @@ public override void Write(Utf8JsonWriter writer, {{structName}} identifier, Jso

if (declaredValueType == "Guid")
source.AppendLine(""" writer.WriteStringValue(value.ToString());""");
else if (declaredValueType == "string")
source.AppendLine(""" writer.WriteStringValue(value);""");
else
source.AppendLine(""" writer.WriteNumberValue(value);""");

source.AppendLine($$"""
}
public override {{structName}} Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var value = reader.Get{{fullValueType}}();
"""
);
if (declaredValueType == "string")
source.AppendLine("""
if (string.IsNullOrWhiteSpace(value))
throw new InvalidOperationException("The value must not be empty.");
"""
);
source.Append($$"""
}
public override {{structName}} Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var value = reader.Get{{fullValueType}}();
return {{structName}}.Create(value);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<!-- Output -->
<PropertyGroup>
<AssemblyName>LightResults.Extensions.GeneratedIdentifier</AssemblyName>
<Version>9.0.0-preview.3</Version>
<Version>9.0.0-preview.4</Version>
<AssemblyVersion>9.0.0.0</AssemblyVersion>
<FileVersion>9.0.0.0</FileVersion>
<NeutralLanguage>en-US</NeutralLanguage>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace LightResults.Extensions.GeneratedIdentifier.Fixtures.Identifiers;

[GeneratedIdentifier<long>]
public partial struct TestLongId;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace LightResults.Extensions.GeneratedIdentifier.Fixtures.Identifiers;

[GeneratedIdentifier<string>]
public partial struct TestStringId;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand All @@ -10,6 +10,7 @@
<AnalysisLevel>latest-Default</AnalysisLevel>
<IsPackable>false</IsPackable>
<IsTestProject>false</IsTestProject>
<OutputType>Library</OutputType>
</PropertyGroup>

<ItemGroup>
Expand Down

This file was deleted.

Loading

0 comments on commit 1bdc3cf

Please sign in to comment.