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

Attribute to instruct Mapperly to generate a default value for a target member. #749

Closed
Phoenix359 opened this issue Sep 13, 2023 · 1 comment

Comments

@Phoenix359
Copy link

Phoenix359 commented Sep 13, 2023

Is your feature request related to a problem? Please describe.

I would like the option to set a default value on the target member when the source cannot provide it.
We now have to constantly make a second mapping function in a separate class just to set a boolean to true or an int to 1 or ....

[Mapper]
public partial class MyMapperlyMapper
{
    [MapperIgnoreTarget(nameof(YourTarget.MonitorProgress))]
    private static partial YourTarget ConvertToYourTarget(Event1 eventMessage);

    [MapperIgnoreTarget(nameof(YourTarget.MonitorProgress))]
    private static partial YourTarget ConvertToYourTarget(Event2 eventMessage);

    [MapperIgnoreTarget(nameof(YourTarget.MonitorProgress))]
    private static partial YourTarget ConvertToYourTarget(Event3 eventMessage);
}
public static partial class MyMapper
{
    public static YourTarget ToYourTarget(this Event1 eventMessage)
    {
        var yourTarget = ConvertToYourTarget(eventMessage);
        yourTarget.MonitorProgress = true;
        return yourTarget;
    }

    public static YourTarget ToYourTarget(this Event2 eventMessage)
    {
        var yourTarget = ConvertToYourTarget(eventMessage);
        yourTarget.MonitorProgress = false;
        return yourTarget;
    }

    public static YourTarget ToYourTarget(this Event3 eventMessage)
    {
        var yourTarget = ConvertToYourTarget(eventMessage);
        yourTarget.MonitorProgress = true;
        return yourTarget;
    }
}

Describe the solution you'd like

I would like a new Attribute to instruct Mapperly to generate a default value for a target member.
I would also like to get an compiler warning when this would override a mapping for a source member.

[Mapper]
public partial class MyMapperlyMapper
{
    [MapperTargetValue(nameof(YourTarget.MonitorProgress, true))]
    private static partial YourTarget ConvertToYourTarget(Event1 eventMessage);

    [MapperTargetValue(nameof(YourTarget.MonitorProgress, false))]
    private static partial YourTarget ConvertToYourTarget(Event2 eventMessage);

    [MapperTargetValue(nameof(YourTarget.MonitorProgress, true))]
    private static partial YourTarget ConvertToYourTarget(Event3 eventMessage);
}

Describe alternatives you've considered

I can see use cases were overriding member mappings is desired. That could be implemented with a different attribute or an extra parameter on this one. In that case i expect no compiler warning, because this explicitly instructs mapperly to override the source.

[MapperOverrideSourceValue(nameof(YourTarget.MonitorProgress, true))]

[MapperTargetValue(nameof(YourTarget.MonitorProgress, true, overrideSource: true))]

Additional context

@latonz
Copy link
Contributor

latonz commented Sep 13, 2023

Thank you for opening this issue. This seems to be a duplicate of #631, isn't it?

@latonz latonz closed this as completed Sep 13, 2023
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

No branches or pull requests

2 participants