-
-
Notifications
You must be signed in to change notification settings - Fork 218
feat: add [MapperNoInlining] attribute and MapperAttribute.NoInlining property #2240
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
base: main
Are you sure you want to change the base?
Changes from all commits
c3a0223
b3b7794
c9c22b0
aede6a0
942c0cd
f431dcf
adf6ddb
258f70f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -142,4 +142,13 @@ public class MapperAttribute : Attribute | |
| /// partial methods are discovered. | ||
| /// </summary> | ||
| public bool AutoUserMappings { get; set; } = true; | ||
|
|
||
| /// <summary> | ||
| /// Whether to prevent mapping methods of this mapper from being inlined | ||
| /// into expression trees for queryable projection mappings. | ||
| /// When <c>true</c>, methods from this mapper referenced via <see cref="UseStaticMapperAttribute"/> | ||
| /// will not be inlined or rebuilt in expression context. | ||
| /// Defaults to <c>false</c>. | ||
| /// </summary> | ||
| public bool NoInlining { get; set; } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO we should include the fact that this only applies to expressions / queryable projection mappings in the name. The same for the separate attribute. |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| using System.Diagnostics; | ||
|
|
||
| namespace Riok.Mapperly.Abstractions; | ||
|
|
||
| /// <summary> | ||
| /// Prevents a mapping method from being inlined into expression trees for queryable projection mappings. | ||
| /// When applied, the method call is preserved as-is instead of being rebuilt in expression context. | ||
| /// This is useful when inlining causes issues such as false enum mapping diagnostics | ||
| /// due to expression tree limitations. | ||
| /// </summary> | ||
| [AttributeUsage(AttributeTargets.Method)] | ||
| [Conditional("MAPPERLY_ABSTRACTIONS_SCOPE_RUNTIME")] | ||
| public sealed class MapperNoInliningAttribute : Attribute; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -201,6 +201,9 @@ private INewInstanceMapping TryInlineMapping(INewInstanceMapping mapping) | |
| { | ||
| return mapping switch | ||
| { | ||
| // check if NoInline is requested | ||
| IUserMapping userMapping when ShouldSkipInlining(userMapping.Method) => mapping, | ||
|
|
||
| // inline existing mapping | ||
| UserImplementedMethodMapping implementedMapping => InlineOrRebuild(implementedMapping) ?? implementedMapping, | ||
|
|
||
|
|
@@ -216,6 +219,15 @@ private INewInstanceMapping TryInlineMapping(INewInstanceMapping mapping) | |
| }; | ||
| } | ||
|
|
||
| private bool ShouldSkipInlining(IMethodSymbol method) | ||
| { | ||
| if (SymbolAccessor.HasAttribute<MapperNoInliningAttribute>(method)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mapperly usually only reads configuration through |
||
| return true; | ||
|
|
||
| var mapperAttribute = AttributeAccessor.AccessFirstOrDefault<MapperAttribute>(method.ContainingType); | ||
| return mapperAttribute?.NoInlining == true; | ||
| } | ||
|
|
||
| private INewInstanceMapping? InlineOrRebuild(UserImplementedMethodMapping mapping) | ||
| { | ||
| // Try inline first | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd move this into the queryable projection documentation page.