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

Explicit mappings #1179

Open
xnoreq opened this issue Mar 15, 2024 · 5 comments
Open

Explicit mappings #1179

xnoreq opened this issue Mar 15, 2024 · 5 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@xnoreq
Copy link

xnoreq commented Mar 15, 2024

Is your feature request related to a problem? Please describe.
I see no way to prevent Mapperly from automatically generating mapping methods for nested properties and instead producing an error for a missing mapping.

Describe the solution you'd like
I'd like to see an ExplicitMappingRequired = true option in MapperAttribute which prevents this.

Example:

[Mapper]
public static partial class Mapper
{
    public static partial ATarget AToTarget(A a);
}

public class A
{
    public B B { get; set; }

    public A(B b)
    {
        B = b;
    }
}

public class B
{
}

public class ATarget
{
    public BTarget? B { get; set; }
}

public class BTarget
{
}

Currently generated code:

public static partial class Mapper
{
    [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "3.4.0.0")]
    public static partial global::ConsoleApp1.ATarget AToTarget(global::ConsoleApp1.A a)
    {
        var target = new global::ConsoleApp1.ATarget();
        target.B = MapToBTarget(a.B);
        return target;
    }

    [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "3.4.0.0")]
    private static global::ConsoleApp1.BTarget MapToBTarget(global::ConsoleApp1.B source)
    {
        var target = new global::ConsoleApp1.BTarget();
        return target;
    }
}

Suggested change:

[Mapper(RequireExplicitMapping = true)]
public static partial class Mapper
{
    public static partial ATarget AToTarget(A a);
}

-> Mapperly fails with an error during compilation because B cannot be mapped to BTarget.

Describe alternatives you've considered
Other mappers like Mapster or AutoMapper which support this feature.

@xnoreq xnoreq added the enhancement New feature or request label Mar 15, 2024
@latonz

This comment was marked as resolved.

@xnoreq

This comment was marked as resolved.

@latonz

This comment was marked as resolved.

@xnoreq

This comment was marked as resolved.

@latonz
Copy link
Contributor

latonz commented Mar 19, 2024

Ah my bad, I understand you now, I also just learned we don't provide a conversion for new instance mappings.
What you are trying to achieve is not supported by Mapperly yet...

Idea: Never build a conversion except there is a user-method requesting exactly that mapping.

I'd be happy to accept a PR 😊
Hints (see also contributing docs):

  • add a new configuration to the MapperAttribute
  • If this is enabled: Returnnull when trying to build a mapping without a UserSymbol (e.g. in MappingBuilderContext.BuildMapping)

@latonz latonz added the good first issue Good for newcomers label Mar 19, 2024
@latonz latonz changed the title Support explicit mapping Explicit mappings Mar 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants