Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support Custom mapping from Mapping To Primitive #650

Open
wants to merge 8 commits into
base: development
Choose a base branch
from

Conversation

DocSvartz
Copy link

@DocSvartz DocSvartz commented Oct 28, 2023

before:
When you mapping To primitive and string always using converter logic

now You can use Custom Conversion logic
supported:
.MapWith()
.MapToTargetWith()

Supporting primitive type from source code

typeof(bool)
typeof(short)
typeof(int)
typeof(long)
typeof(float)
typeof(double)
typeof(decimal)
typeof(ushort)
typeof(uint)
typeof(ulong)
typeof(byte)
typeof(sbyte)
typeof(DateTime)
+
typeof(string)

Example:

[TestMethod]
public void CustomMappingDateTimeToPrimitive()
{
    TypeAdapterConfig<DateTime, long>
       .NewConfig()
       .MapWith(src => new DateTimeOffset(src).ToUnixTimeSeconds());

    TypeAdapterConfig<DateTime, string>
       .NewConfig()
       .MapWith(src => src.ToShortDateString());

    var _source = new DateTime(2023, 10, 27, 0, 0, 0, DateTimeKind.Utc);

    var _resultToLong = _source.Adapt<long>();
    var _resultToString = _source.Adapt<string>();

    _resultToLong.ShouldBe(new DateTimeOffset(new DateTime(2023, 10, 27, 0, 0, 0, DateTimeKind.Utc)).ToUnixTimeSeconds());
    _resultToString.ShouldNotBe(_source.ToString());
    _resultToString.ShouldBe(_source.ToShortDateString());
}

/// <summary>
/// https://github.com/MapsterMapper/Mapster/issues/407
/// </summary>
[TestMethod]
public void MappingDatetimeToLongWithCustomMapping()
{
    TypeAdapterConfig<DateTime, long>
       .NewConfig()
       .MapWith(src => new DateTimeOffset(src).ToUnixTimeSeconds());

    TypeAdapterConfig<long, DateTime>
      .NewConfig()
      .MapWith(src => new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(src).Date);

    var emptySource = new Source407() { Time = DateTime.UtcNow.Date };
    var fromC1 = new DateTime(2023, 10, 27,0,0,0,DateTimeKind.Utc);
    var fromC2 = new DateTimeOffset(new DateTime(2025, 11, 23, 0, 0, 0, DateTimeKind.Utc)).ToUnixTimeSeconds();
    var c1 = new Source407 { Time = fromC1 };
    var c2 = new Destination407 { Time = fromC2 };

    var _result = c1.Adapt<Destination407>(); // Work 
    var _resultLongtoDateTime = c2.Adapt<Source407>();

    _result.Time.ShouldBe(new DateTimeOffset(new DateTime(2023, 10, 27, 0, 0, 0, DateTimeKind.Utc)).ToUnixTimeSeconds());               
    _resultLongtoDateTime.Time.ShouldBe(new DateTime(2025, 11, 23).Date);
}

@DocSvartz DocSvartz marked this pull request as draft October 28, 2023 02:16
@DocSvartz DocSvartz marked this pull request as ready for review October 28, 2023 03:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant