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

feat: support derived type mappings #366

Merged
merged 1 commit into from
Apr 28, 2023
Merged

feat: support derived type mappings #366

merged 1 commit into from
Apr 28, 2023

Conversation

latonz
Copy link
Contributor

@latonz latonz commented Apr 20, 2023

Closes #330

  • implements support to map derived types

@latonz latonz self-assigned this Apr 20, 2023
@TimothyMakkison
Copy link
Collaborator

This is a really cool addition, I love how elegant the generated code is.

I wasn't sure where to put this but after reading the original issue I got thinking about adding generic attributes without breaking anything. Because generic attributes are a C#11 feature it mighr be possible to support them for compatible programs by doing as follows:

  • Set the language version of Riok.Mapperly.Abstractions to 11
  • Create a copy of MapDerivedTypeAttribute with 2 generic parameters and an additional empty constructor.
  • Inside DerivedTypeMappingBuilder concat configs to a call to below.
  • Programs using C# 10 or lower can use the typeof attribute constructor, getting an error if they try to use the generic attribute. C#11 programs can use either constructors.
// Check is MapDerivedTypeAttribute...

var attrDatas = symbol.GetAttributes().Where(x => x.AttributeClass.TypeArguments.Count() == 2);

foreach (var attrData in attrDatas)
{
    var namedSymbol = attrData.AttributeClass;
    yield return new MapDerivedType(namedSymbol.TypeArguments[0], namedSymbol.TypeArguments[1]);
}

I've hacked together a working version here, it appears to work with ,NET Framework and Core. I've probably overlooked something, perhaps it only works on modern .NET Sdk's or recent versions of Visual Studio. Hopefully this helps 🤞

@latonz latonz requested a review from ni507 April 23, 2023 15:22
@latonz latonz marked this pull request as ready for review April 23, 2023 15:22
@latonz latonz removed the request for review from ni507 April 23, 2023 15:22
@latonz latonz marked this pull request as draft April 23, 2023 15:30
@latonz latonz marked this pull request as ready for review April 27, 2023 08:16
@latonz latonz requested review from CommonGuy and ni507 April 27, 2023 08:16
@latonz latonz enabled auto-merge (squash) April 27, 2023 09:42
@latonz latonz requested a review from CommonGuy April 27, 2023 09:42
@latonz latonz disabled auto-merge April 27, 2023 09:43
@latonz latonz enabled auto-merge (squash) April 28, 2023 06:27
@latonz latonz requested a review from CommonGuy April 28, 2023 06:27
@latonz latonz merged commit 11995f0 into riok:main Apr 28, 2023
@latonz latonz deleted the 330-support-mapping-derived-types branch April 28, 2023 06:30
@latonz latonz added this to the v2.9.0 milestone Jun 14, 2023
@github-actions
Copy link

github-actions bot commented Aug 7, 2023

🎉 This PR is included in version 3.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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.

Support mapping derived types
3 participants