Skip to content

Conversation

@DocSvartz
Copy link
Contributor

@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
@andrerav andrerav closed this Jan 2, 2025
@andrerav andrerav reopened this Jan 2, 2025
@andrerav
Copy link
Contributor

andrerav commented Jan 2, 2025

Closed and reopened to re-run the workflow.

@andrerav andrerav changed the title Add Support Custom mapping from Mapping To Primitive Add support for primitive types in custom mapping Jan 2, 2025
@andrerav andrerav merged commit 710fc2b into MapsterMapper:development Jan 2, 2025
1 check passed
@andrerav
Copy link
Contributor

andrerav commented Jan 2, 2025

Thank you @DocSvartz!

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.

2 participants