Skip to content

Commit dc89444

Browse files
authored
Merge pull request #28 from altasoft/bugfix/dateonlyXmlSerialization
Add DateOnly support and XML serialization enhancements
2 parents c9978a0 + 1e09715 commit dc89444

7 files changed

Lines changed: 427 additions & 5 deletions

File tree

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<Product>Domain Primitives</Product>
1010
<Company>ALTA Software llc.</Company>
1111
<Copyright>Copyright © 2024 ALTA Software llc.</Copyright>
12-
<Version>6.0.3</Version>
12+
<Version>6.0.4</Version>
1313
</PropertyGroup>
1414

1515
<PropertyGroup>

src/AltaSoft.DomainPrimitives.Generator/Helpers/MethodGeneratorHelper.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,7 @@ public static void GenerateIXmlSerializableMethods(GeneratorData data, SourceCod
992992
{
993993
"string" => "ReadElementContentAsString",
994994
"bool" => "ReadElementContentAsBoolean",
995+
"DateOnly" => "ReadElementContentAsDateOnly",
995996
_ => $"ReadElementContentAs<{data.PrimitiveTypeFriendlyName}>"
996997
};
997998

src/AltaSoft.DomainPrimitives/XmlReaderExt.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,33 @@ namespace AltaSoft.DomainPrimitives;
1313
public static class XmlReaderExt
1414
{
1515
/// <summary>
16-
/// Reads the content of the current element as a <see cref="byte" /> object.
16+
/// Reads the content of the current XML element as a <typeparamref name="T"/> value.
1717
/// </summary>
18-
/// <param name="reader">The XmlReader instance.</param>
19-
/// <returns>A byte object representing the value read from the element.</returns>
18+
/// <typeparam name="T">The type of value to parse, which must implement <see cref="IParsable{TSelf}"/>.</typeparam>
19+
/// <param name="reader">The <see cref="XmlReader"/> instance.</param>
20+
/// <returns>
21+
/// A <typeparamref name="T"/> value parsed from the current element's content.
22+
/// </returns>
2023
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2124
public static T ReadElementContentAs<T>(this XmlReader reader) where T : IParsable<T>
2225
{
2326
return T.Parse(reader.ReadElementContentAsString(), CultureInfo.InvariantCulture);
2427
}
28+
29+
/// <summary>
30+
/// Reads the content of the current XML element as a <see cref="DateOnly"/> value.
31+
/// </summary>
32+
/// <param name="reader">The <see cref="XmlReader"/> instance.</param>
33+
/// <returns>
34+
/// A <see cref="DateOnly"/> value parsed from the current element's content.
35+
/// </returns>
36+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
37+
public static DateOnly ReadElementContentAsDateOnly(this XmlReader reader)
38+
{
39+
var str = reader.ReadElementContentAsString();
40+
if (DateOnly.TryParse(str, CultureInfo.InvariantCulture, out var result))
41+
return result;
42+
43+
return DateOnly.FromDateTime(DateTime.Parse(str, CultureInfo.InvariantCulture));
44+
}
2545
}

tests/AltaSoft.DomainPrimitives.Generator.Tests/DomainPrimitiveGeneratorTest.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,44 @@ public static PrimitiveValidationResult Validate(uint value)
829829
GenerateTypeConverters = false
830830
});
831831
}
832+
833+
[Fact]
834+
public Task DateOnlyValue_GeneratesAllInterfacesAndConvertersWithXml()
835+
{
836+
const string source = """
837+
using System;
838+
using System.Collections.Generic;
839+
using System.Linq;
840+
using System.Text;
841+
using System.Threading.Tasks;
842+
using AltaSoft.DomainPrimitives;
843+
844+
namespace AltaSoft.DomainPrimitives;
845+
846+
public readonly partial struct DateOnlyXmlValue : IDomainValue<DateOnly>
847+
{
848+
/// <inheritdoc/>
849+
public static PrimitiveValidationResult Validate(DateOnly value)
850+
{
851+
if (value == default)
852+
return "Invalid Value";
853+
854+
return PrimitiveValidationResult.Ok;
855+
}
856+
}
857+
858+
""";
859+
860+
return TestHelper.Verify(source, (_, x, _) => Assert.Equal(1, x.Count), new DomainPrimitiveGlobalOptions()
861+
{
862+
GenerateEntityFrameworkCoreValueConverters = false,
863+
GenerateJsonConverters = false,
864+
GenerateSwaggerConverters = false,
865+
GenerateTypeConverters = false,
866+
GenerateXmlSerialization = true
867+
});
868+
}
869+
832870
public static class TestHelper
833871
{
834872
internal static Task Verify(string source, Action<ImmutableArray<Diagnostic>, List<string>, GeneratorDriver>? additionalChecks = null, DomainPrimitiveGlobalOptions? options = null)

tests/AltaSoft.DomainPrimitives.Generator.Tests/ModuleInitializer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Runtime.CompilerServices;
2+
using DiffEngine;
23

34
namespace AltaSoft.DomainPrimitives.Generator.Tests;
45

@@ -8,6 +9,6 @@ public static class ModuleInitializer
89
public static void Init()
910
{
1011
VerifySourceGenerators.Initialize();
11-
12+
DiffTools.UseOrder(DiffTool.VisualStudioCode);
1213
}
1314
}

0 commit comments

Comments
 (0)