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

Generate static methods in nonstatic classes #681

Conversation

trejjam
Copy link
Contributor

@trejjam trejjam commented Aug 24, 2023

Generate static methods in nonstatic classes

Description

This PR adds the Mapper configuration option GenerateStaticMethods which is a switch that generates static methods inside a non-static class.

Fixes #673

Checklist

  • The existing code style is followed
  • The commit message follows our guidelines
  • Performed a self-review of my code
  • Hard-to-understand areas of my code are commented
  • The documentation is updated (as applicable)
  • Unit tests are added/updated
  • Integration tests are added/updated (as applicable, especially if feature/bug depends on roslyn or framework version in use)

@codecov
Copy link

codecov bot commented Aug 24, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (c5f0c54) 91.40% compared to head (ff6fac9) 91.48%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #681      +/-   ##
==========================================
+ Coverage   91.40%   91.48%   +0.08%     
==========================================
  Files         214      214              
  Lines        7189     7212      +23     
  Branches      869      872       +3     
==========================================
+ Hits         6571     6598      +27     
+ Misses        411      408       -3     
+ Partials      207      206       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@trejjam trejjam force-pushed the feature/generated-static-methods-in-non-static-classes branch 7 times, most recently from 02175cf to 14c709a Compare August 29, 2023 08:30
@trejjam
Copy link
Contributor Author

trejjam commented Aug 29, 2023

Hi, @latonz, @TimothyMakkison,

can I ask you for your opinion about this functionality? And if so, can I ask for feedback on implementation?

Thank you,
Jan

@trejjam
Copy link
Contributor Author

trejjam commented Aug 29, 2023

The goal is to support this code:

public interface IMyInterface
{
    static abstract CarDto ToDto(Car car);
}

[Mapper(GenerateStaticMethods = true)]
public partial class CarMapper : IMyInterface
{
    public static partial CarDto ToDto(Car car);
}

@latonz
Copy link
Contributor

latonz commented Aug 29, 2023

Sorry for my late feedback, I had the pleasure to enjoy some vacation days 🏝️
Thank you for opening the corresponding issue and submitting this PR, I understand your use case.
On a first glance the current implementation has several not yet addressed problems (which probably aren't that easy to fix):

  • Generated nested mapping methods are not static
    (to reproduce simply add public static partial Riok.Mapperly.IntegrationTests.Dto.TestObjectDto Map(Riok.Mapperly.IntegrationTests.Models.TestObject value); to the TestMapperWithStaticMethods in your integration tests)
  • Non-Static user defined mappings are called from generated static mapping methods
  • Non-Static user implemented mappings are called from generated static mapping methods
  • Non-Static object factories are called from generated static mapping methods
  • Non-Static external mappers are called from generated static mapping methods (probably due to rebasing, only landed recently in main)

I will comment on the issue #673 with the concerns that I have for this feature.

@trejjam
Copy link
Contributor Author

trejjam commented Aug 29, 2023

Thanks, I will take a look at the missing parts

@latonz
Copy link
Contributor

latonz commented Aug 29, 2023

@trejjam thank you for your efforts! But before you are going to continue working on this PR we should probably agree on the design. I added my bits here.

@trejjam
Copy link
Contributor Author

trejjam commented Aug 29, 2023

Yes, for sure :)

@latonz latonz marked this pull request as draft September 1, 2023 07:38
@trejjam trejjam force-pushed the feature/generated-static-methods-in-non-static-classes branch from 14c709a to c089017 Compare November 3, 2023 12:42
@trejjam trejjam marked this pull request as ready for review November 3, 2023 12:42
@trejjam trejjam force-pushed the feature/generated-static-methods-in-non-static-classes branch 2 times, most recently from 6909d3f to 6b72263 Compare November 4, 2023 15:45
@trejjam
Copy link
Contributor Author

trejjam commented Nov 4, 2023

Hi @latonz,
I finally found some time to finish this PR. I will be happy if you find some time for a review.

@trejjam trejjam force-pushed the feature/generated-static-methods-in-non-static-classes branch from 6b72263 to 57008f3 Compare November 4, 2023 15:49
Copy link
Contributor

@latonz latonz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the updates, I added my feedback 😊

docs/docs/configuration/mapper.mdx Outdated Show resolved Hide resolved
docs/docs/configuration/mapper.mdx Outdated Show resolved Hide resolved
docs/docs/configuration/mapper.mdx Outdated Show resolved Hide resolved
docs/docs/configuration/mapper.mdx Outdated Show resolved Hide resolved
src/Riok.Mapperly/Descriptors/DescriptorBuilder.cs Outdated Show resolved Hide resolved
src/Riok.Mapperly/Descriptors/Mappings/MethodMapping.cs Outdated Show resolved Hide resolved
@trejjam trejjam force-pushed the feature/generated-static-methods-in-non-static-classes branch 2 times, most recently from 8686122 to 227cf5b Compare November 8, 2023 12:26
@trejjam trejjam requested a review from latonz November 8, 2023 12:27
@trejjam
Copy link
Contributor Author

trejjam commented Nov 8, 2023

Hi, thank you for the review! I processed all recommendations.

I left changes as separate commits, so it's easier to navigate through changes. Please let me know for the final rebase/squash

@trejjam trejjam force-pushed the feature/generated-static-methods-in-non-static-classes branch from 227cf5b to 3e1433b Compare November 8, 2023 12:31
@trejjam trejjam force-pushed the feature/generated-static-methods-in-non-static-classes branch from f6d46cb to 45db532 Compare November 9, 2023 18:58
Copy link
Contributor

@latonz latonz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the updates, only a few comments left and this can get merged 👍

src/Riok.Mapperly/Emit/SourceEmitter.cs Outdated Show resolved Hide resolved
src/Riok.Mapperly/Descriptors/DescriptorBuilder.cs Outdated Show resolved Hide resolved
@latonz
Copy link
Contributor

latonz commented Nov 13, 2023

I just checked my earlier comment #681 (comment) and noticed that there is still a problem with object factories and external mappers. In pseudo-static mappers, instance object factories and external mappers are still beeing used without a diagnostic. These should be ignored and a diagnostic should be emitted. ExtractObjectFactories can probably be moved after ExtractUserMappings. Then _mapperDescriptor.Static can be used in ExtractUserMappings and ExtractObjectFactories.

@trejjam trejjam force-pushed the feature/generated-static-methods-in-non-static-classes branch from 99dd4b5 to 2b6db69 Compare November 13, 2023 15:10
@trejjam
Copy link
Contributor Author

trejjam commented Nov 13, 2023

I will take a look at the factories and external mappers

@trejjam
Copy link
Contributor Author

trejjam commented Nov 13, 2023

I dropped one test which I would say did not test what it was saying so: 2b6db69

@trejjam
Copy link
Contributor Author

trejjam commented Nov 13, 2023

I added tests for external mappers and object factories

@trejjam trejjam requested a review from latonz November 13, 2023 19:36
@trejjam trejjam force-pushed the feature/generated-static-methods-in-non-static-classes branch 5 times, most recently from a94254d to 25059f3 Compare November 17, 2023 13:00
@trejjam trejjam requested a review from latonz November 17, 2023 13:01
@trejjam trejjam force-pushed the feature/generated-static-methods-in-non-static-classes branch from 25059f3 to 25d2e30 Compare November 19, 2023 13:25
@trejjam trejjam requested a review from latonz November 19, 2023 13:33
@trejjam trejjam force-pushed the feature/generated-static-methods-in-non-static-classes branch from d931e58 to ff6fac9 Compare November 19, 2023 13:34
@latonz latonz merged commit 1724124 into riok:main Nov 19, 2023
18 of 19 checks passed
@trejjam trejjam deleted the feature/generated-static-methods-in-non-static-classes branch November 19, 2023 14:38
Copy link

🎉 This PR is included in version 3.3.0-next.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

Copy link

🎉 This PR is included in version 3.3.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.

Use interface with a static abstract member on class managed by Mapperly
2 participants