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

Emit more readable constructor mapping code #1322

Open
aradalvand opened this issue Jun 7, 2024 · 2 comments · May be fixed by #1337
Open

Emit more readable constructor mapping code #1322

aradalvand opened this issue Jun 7, 2024 · 2 comments · May be fixed by #1337
Labels
enhancement New feature or request

Comments

@aradalvand
Copy link

aradalvand commented Jun 7, 2024

Currently, mapping to records emits code that passes the source values to the constructor of the destination record type, but it does so all in a single line, like so:

// <auto-generated />
#nullable enable
namespace PaymentManagement.Infrastructure.Mappers
{
    public static partial class GatewayMappers
    {
        [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "3.5.1.0")]
        public static partial global::System.Linq.IQueryable<global::PaymentManagement.Application.UseCases.GatewayDto> ToGatewayDtos(this global::System.Linq.IQueryable<global::PaymentManagement.Domain.Entities.Gateway> query)
        {
#nullable disable
            return System.Linq.Queryable.Select(query, x => new global::PaymentManagement.Application.UseCases.GatewayDto(x.Id, global::PaymentManagement.Domain.Enums.PaymentServiceProviderExtensions.ToHumanReadableName(x.Psp), x.SubsidiaryTitle, new global::PaymentManagement.Application.UseCases.GatewayBankAccountDto(x.BankAccount.Id, x.BankAccount.Number, x.BankAccount.Iban, x.BankAccount.BankTitle)));
#nullable enable
        }

        [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "3.5.1.0")]
        private static partial global::PaymentManagement.Application.UseCases.GatewayDto ToGatewayDto(this global::PaymentManagement.Domain.Entities.Gateway gateway)
        {
            var target = new global::PaymentManagement.Application.UseCases.GatewayDto(gateway.Id, MapPspTitle(gateway.Psp), gateway.SubsidiaryTitle, MapToGatewayBankAccountDto(gateway.BankAccount));
            return target;
        }

        [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "3.5.1.0")]
        private static global::PaymentManagement.Application.UseCases.GatewayBankAccountDto MapToGatewayBankAccountDto(global::PaymentManagement.Domain.Entities.GatewayBankAccount source)
        {
            var target = new global::PaymentManagement.Application.UseCases.GatewayBankAccountDto(source.Id, source.Number, source.Iban, source.BankTitle);
            return target;
        }
    }
}

Given that one of Mapperly's main claims to fame is generating readable code, it would go a long way in terms of improving readability if the constructor arguments in this case were each on a separate line.

@aradalvand aradalvand added the enhancement New feature or request label Jun 7, 2024
@aradalvand aradalvand changed the title Emit more readable record mapping code Jun 7, 2024
@latonz
Copy link
Contributor

latonz commented Jun 10, 2024

Thanks for reporting, Mapperly doesn't differentiate between records and other constructors. This should target all constructors. I adjusted the title.

@latonz latonz changed the title Emit more readable record (constructor) mapping code Jun 10, 2024
@latonz
Copy link
Contributor

latonz commented Jun 10, 2024

Would be happy to accept a PR implementing this. A good starting point to contribute is the contributors documentation. Let us know if you plan to work on this 😊
Some hints / affected code:

  • SyntaxFactoryHelper.CreateObject in src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.New.cs
@aradalvand aradalvand linked a pull request Jun 14, 2024 that will close this issue
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
2 participants