question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support generic user implemented mappings

See original GitHub issue

Add support for user implemented generic mapping methods:

class Optional<T> where T : notnull { public T? Value; }
class User { string Id; }
class UserDto { string Id; }
class Document { Optional<User> ModifiedBy; }
class DocumentDto { Optional<UserDto> ModifiedBy; }

[Mapper]
public partial class Mapper
{
    public partial DocumentDto MapDocument(Document source);
    
    private Optional<T> MapOptional<TTarget, TSource>(Optional<TSource> source)
    {
        return new Optional<TTarget> { Value = Map<TTarget>(source.Value) };
    }

    private partial T Map(object source);

    private partial UserDto MapUser(User source);
}

Mapperly should use the user implemented MapOptional method to map Document.ModifiedBy to DocumentDto.ModifiedBy. Depends on https://github.com/riok/mapperly/issues/357. Discussed in #444.

Issue Analytics

  • State:open
  • Created 4 months ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
latonzcommented, Jun 22, 2023

https://github.com/riok/mapperly/issues/421 occurred when Mapperly didn’t have any support for generic methods, but still tried to call them. If a generic method was implemented by a user, Mapperly handled the generic type as any other type. Therefore it did type checks with typeof(TGeneric) which won’t work outside the method itself, also methods with a generic target type were called like any other method, without explicit type arguments. But if the return type is a type argument, the c# compiler cannot infer the type. The fix to #421 was to just ignore them for now and open this issue to implement support. Example of how Mapperly would currently invoke generic user implemented methods if it has a generic return type:

// user implemented mapping method
TTarget Map<TSource, TTarget>(TSource source)
    where TSource : IMyInterface
    where TTarget : new(), IMyOtherInterface
  => new TTarget { MyValue = source.MyValue };

// would currently be called by Mapperly
var target = Map(source);

// instead of
var target = Map<MySource, MyTarget>(soruce);

To support this, as you figured out, Mapperly needs to implement generic mapping support during the mapping resolution in MappingCollection and UserMethodMappingExtractor.BuildUserImplementedMapping. Additionally the syntax generation needs to be aware of generics and whether they need to be specified explicitly. Probably a new GenericUserImplementedMethodMapping is needed or the existing UserImplementedMethodMapping could be adjusted. In the Build method of the generic mapping, the Invocation needs to be a GenericInvocation if type arguments need to be specified explicitly.

1reaction
latonzcommented, Jun 21, 2023

@Fube I think this works for methods where the source is the only generic type argument (would definitely need some cleanup 😄). If the target is (also) generic, not only the lookup side, but also the syntax generation side needs to be adjusted as explicit specification of the generic type arguments is required.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Customize Azure Active Directory attribute mappings in ...
Learn about attribute mappings for Software as a Service (SaaS) apps in Azure Active Directory Application Provisioning.
Read more >
Use Service Mappings
Use service mappings to map data from a source to a destination and modify the data. Used with analgorithm service mappings are a...
Read more >
Generic and Explicit Object Mappings
Generic mapping, which allows you to store arbitrary objects without special configuration or administration. Explicit mapping, which maps specific objects and ...
Read more >
KB5014754—Certificate-based authentication changes on ...
Certificate mappings. Domain administrators can manually map certificates to a user in Active Directory using the altSecurityIdentities attribute of the users ...
Read more >
Role Mapping Providers
Role Mapping Providers. Role mapping is the process whereby principals (users or groups) are dynamically mapped to security roles at runtime.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found