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
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<Product>Domain Primitives</Product>
<Company>ALTA Software llc.</Company>
<Copyright>Copyright © 2024 ALTA Software llc.</Copyright>
<Version>6.0.3</Version>
<Version>6.0.4</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,7 @@ public static void GenerateIXmlSerializableMethods(GeneratorData data, SourceCod
{
"string" => "ReadElementContentAsString",
"bool" => "ReadElementContentAsBoolean",
"DateOnly" => "ReadElementContentAsDateOnly",
_ => $"ReadElementContentAs<{data.PrimitiveTypeFriendlyName}>"
};

Expand Down
26 changes: 23 additions & 3 deletions src/AltaSoft.DomainPrimitives/XmlReaderExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,33 @@ namespace AltaSoft.DomainPrimitives;
public static class XmlReaderExt
{
/// <summary>
/// Reads the content of the current element as a <see cref="byte" /> object.
/// Reads the content of the current XML element as a <typeparamref name="T"/> value.
/// </summary>
/// <param name="reader">The XmlReader instance.</param>
/// <returns>A byte object representing the value read from the element.</returns>
/// <typeparam name="T">The type of value to parse, which must implement <see cref="IParsable{TSelf}"/>.</typeparam>
/// <param name="reader">The <see cref="XmlReader"/> instance.</param>
/// <returns>
/// A <typeparamref name="T"/> value parsed from the current element's content.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T ReadElementContentAs<T>(this XmlReader reader) where T : IParsable<T>
{
return T.Parse(reader.ReadElementContentAsString(), CultureInfo.InvariantCulture);
}

/// <summary>
/// Reads the content of the current XML element as a <see cref="DateOnly"/> value.
/// </summary>
/// <param name="reader">The <see cref="XmlReader"/> instance.</param>
/// <returns>
/// A <see cref="DateOnly"/> value parsed from the current element's content.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static DateOnly ReadElementContentAsDateOnly(this XmlReader reader)
{
var str = reader.ReadElementContentAsString();
if (DateOnly.TryParse(str, CultureInfo.InvariantCulture, out var result))
return result;

return DateOnly.FromDateTime(DateTime.Parse(str, CultureInfo.InvariantCulture));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,44 @@
GenerateTypeConverters = false
});
}

[Fact]
public Task DateOnlyValue_GeneratesAllInterfacesAndConvertersWithXml()
{
const string source = """
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AltaSoft.DomainPrimitives;

namespace AltaSoft.DomainPrimitives;

public readonly partial struct DateOnlyXmlValue : IDomainValue<DateOnly>
{
/// <inheritdoc/>
public static PrimitiveValidationResult Validate(DateOnly value)
{
if (value == default)
return "Invalid Value";

return PrimitiveValidationResult.Ok;
}
}

""";

return TestHelper.Verify(source, (_, x, _) => Assert.Equal(1, x.Count), new DomainPrimitiveGlobalOptions()

Check warning on line 860 in tests/AltaSoft.DomainPrimitives.Generator.Tests/DomainPrimitiveGeneratorTest.cs

View workflow job for this annotation

GitHub Actions / build

Do not use Assert.Equal() to check for collection size. Use Assert.Single instead. (https://xunit.net/xunit.analyzers/rules/xUnit2013)

Check warning on line 860 in tests/AltaSoft.DomainPrimitives.Generator.Tests/DomainPrimitiveGeneratorTest.cs

View workflow job for this annotation

GitHub Actions / build

Do not use Assert.Equal() to check for collection size. Use Assert.Single instead. (https://xunit.net/xunit.analyzers/rules/xUnit2013)
{
GenerateEntityFrameworkCoreValueConverters = false,
GenerateJsonConverters = false,
GenerateSwaggerConverters = false,
GenerateTypeConverters = false,
GenerateXmlSerialization = true
});
}

public static class TestHelper
{
internal static Task Verify(string source, Action<ImmutableArray<Diagnostic>, List<string>, GeneratorDriver>? additionalChecks = null, DomainPrimitiveGlobalOptions? options = null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Runtime.CompilerServices;
using DiffEngine;

namespace AltaSoft.DomainPrimitives.Generator.Tests;

Expand All @@ -8,6 +9,6 @@ public static class ModuleInitializer
public static void Init()
{
VerifySourceGenerators.Initialize();

DiffTools.UseOrder(DiffTool.VisualStudioCode);
}
}
Loading
Loading