-
-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: datetime to dateonly and timeonly mapping (#253)
- Loading branch information
Showing
23 changed files
with
236 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/Riok.Mapperly/Descriptors/MappingBuilders/DateTimeToDateOnlyMappingBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Riok.Mapperly.Abstractions; | ||
using Riok.Mapperly.Descriptors.Mappings; | ||
|
||
namespace Riok.Mapperly.Descriptors.MappingBuilders; | ||
|
||
public static class DateTimeToDateOnlyMappingBuilder | ||
{ | ||
private const string FromDateTimeMethodName = "FromDateTime"; | ||
|
||
public static StaticMethodMapping? TryBuildMapping(MappingBuilderContext ctx) | ||
{ | ||
if (!ctx.IsConversionEnabled(MappingConversionType.DateTimeToDateOnly) || ctx.Types.DateOnly == null) | ||
return null; | ||
|
||
if (ctx.Source.SpecialType != SpecialType.System_DateTime) | ||
return null; | ||
|
||
if (ctx.Target is not INamedTypeSymbol namedSymbol || !SymbolEqualityComparer.Default.Equals(namedSymbol, ctx.Types.DateOnly)) | ||
return null; | ||
|
||
var fromDateTimeMethod = ResolveFromDateTimeMethod(ctx); | ||
if (fromDateTimeMethod is null) | ||
return null; | ||
|
||
return new StaticMethodMapping(fromDateTimeMethod); | ||
} | ||
|
||
private static IMethodSymbol? ResolveFromDateTimeMethod(MappingBuilderContext ctx) | ||
{ | ||
return ctx.Types.DateOnly? | ||
.GetMembers(FromDateTimeMethodName) | ||
.OfType<IMethodSymbol>() | ||
.FirstOrDefault(m => m.IsStatic); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/Riok.Mapperly/Descriptors/MappingBuilders/DateTimeToTimeOnlyMappingBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Riok.Mapperly.Abstractions; | ||
using Riok.Mapperly.Descriptors.Mappings; | ||
|
||
namespace Riok.Mapperly.Descriptors.MappingBuilders; | ||
|
||
public static class DateTimeToTimeOnlyMappingBuilder | ||
{ | ||
private const string FromDateTimeMethodName = "FromDateTime"; | ||
|
||
public static StaticMethodMapping? TryBuildMapping(MappingBuilderContext ctx) | ||
{ | ||
if (!ctx.IsConversionEnabled(MappingConversionType.DateTimeToTimeOnly) || ctx.Types.TimeOnly == null) | ||
return null; | ||
|
||
if (ctx.Source.SpecialType != SpecialType.System_DateTime) | ||
return null; | ||
|
||
if (ctx.Target is not INamedTypeSymbol namedSymbol || !SymbolEqualityComparer.Default.Equals(namedSymbol, ctx.Types.TimeOnly)) | ||
return null; | ||
|
||
var fromDateTimeMethod = ResolveFromDateTimeMethod(ctx); | ||
if (fromDateTimeMethod is null) | ||
return null; | ||
|
||
return new StaticMethodMapping(fromDateTimeMethod); | ||
} | ||
|
||
private static IMethodSymbol? ResolveFromDateTimeMethod(MappingBuilderContext ctx) | ||
{ | ||
return ctx.Types.TimeOnly? | ||
.GetMembers(FromDateTimeMethodName) | ||
.OfType<IMethodSymbol>() | ||
.FirstOrDefault(m => m.IsStatic); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
test/Riok.Mapperly.IntegrationTests/Helpers/PortableDateOnlyConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#if !NET6_0_OR_GREATER | ||
using System; | ||
using System.Globalization; | ||
using VerifyTests; | ||
|
||
namespace Riok.Mapperly.IntegrationTests.Helpers | ||
{ | ||
/// <summary> | ||
/// Portable <see cref="DateOnly"/> converter for VerifyTests to ensure consistent json output across different net versions. | ||
/// Is necessary to handle net framework tests which include <see cref="DateOnly"/> per nuget package. | ||
/// </summary> | ||
public class PortableDateOnlyConverter : WriteOnlyJsonConverter<DateOnly> | ||
{ | ||
public override void Write(VerifyJsonWriter writer, DateOnly value) => | ||
// copied format from https://github.com/VerifyTests/Verify/blob/19.7.1/src/Verify/Serialization/VerifierSettings.cs#L58 | ||
writer.WriteRawValue(value.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)); | ||
} | ||
} | ||
#endif |
20 changes: 20 additions & 0 deletions
20
test/Riok.Mapperly.IntegrationTests/Helpers/PortableTimeOnlyConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#if !NET6_0_OR_GREATER | ||
using System; | ||
using System.Globalization; | ||
using VerifyTests; | ||
|
||
|
||
namespace Riok.Mapperly.IntegrationTests.Helpers | ||
{ | ||
/// <summary> | ||
/// Portable <see cref="TimeOnly"/> converter for VerifyTests to ensure consistent json output across different net versions. | ||
/// Is necessary to handle net framework tests which include <see cref="TimeOnly"/> per nuget package. | ||
/// </summary> | ||
public class PortableTimeOnlyConverter : WriteOnlyJsonConverter<TimeOnly> | ||
{ | ||
public override void Write(VerifyJsonWriter writer, TimeOnly value) => | ||
// copied format from https://github.com/VerifyTests/Verify/blob/19.7.1/src/Verify/Serialization/VerifierSettings.cs#L65 | ||
writer.WriteRawValue(value.ToString("h:mm tt", CultureInfo.InvariantCulture)); | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.