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: inline methods consisting of a single return statement or local variable declaration expression #1422

Merged
merged 2 commits into from
Aug 3, 2024

Conversation

hartmair
Copy link
Contributor

@hartmair hartmair commented Jul 28, 2024

feat: inline methods consisting of a single return statement or local variable declaration expression

Description

  • feat: inline methods consisting of a single return statement
  • feat: inline methods consisting of a single local variable declaration

Fixes # (issue)

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)

@hartmair hartmair changed the title feat: inline methods consisting of a single return statement feat: inline methods consisting of a single return statement or local variable declaration Jul 28, 2024
@hartmair hartmair force-pushed the feat/inline-single-return-statement branch from dbd1e5a to 5fb7526 Compare July 28, 2024 08:15
@hartmair hartmair marked this pull request as ready for review July 28, 2024 08:18
@hartmair hartmair force-pushed the feat/inline-single-return-statement branch from 5fb7526 to 14977e2 Compare July 28, 2024 08:18
@trejjam
Copy link
Contributor

trejjam commented Jul 31, 2024

You can do this with current features:

Source:

[Mapper]
public static partial class Mapper
{
    private static partial System.Linq.IQueryable<B> Map(this System.Linq.IQueryable<A> source);

    [MapProperty(nameof(C.Value), nameof(D.Value), Use = nameof(GetComputedValue))]
    private static partial D MapToD(C v);

    [UserMapping(Default = false)]
    private static string GetComputedValue(string value) => value + "-mapped";
}

class A { public string StringValue { get; set; } public C NestedValue { get; set; } }
class B { public string StringValue { get; set; } public D NestedValue { get; set; } }
class C { public string Value { get; set; } }
class D { public string Value { get; set; } }

Generated

// <auto-generated />
#nullable enable


    public static partial class Mapper
    {
        [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "0.0.1.0")]
        private static partial global::System.Linq.IQueryable<B> Map(this global::System.Linq.IQueryable<A> source)
        {
#nullable disable
            return System.Linq.Queryable.Select(source, x => new B()
            {
                StringValue = x.StringValue,
                NestedValue = new D()
                {
                    Value = x.NestedValue.Value + "-mapped",
                },
            });
#nullable enable
        }

        [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "0.0.1.0")]
        private static partial D MapToD(C v)
        {
            var target = new D();
            target.Value = GetComputedValue(v.Value);
            return target;
        }
    }

@hartmair
Copy link
Contributor Author

@trejjam I know that you can do this if you use an expression body. That's the point of this pull request. That you don't have to think about the code style. It just works regardlessly how you write your user mapping.

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 this contribution!
As @trejjam pointed out this works already by using method expressions. I can understand that there are use-cases where one doesn't want to use expressions and I think it's nice that the Mapperly inlining just works out of the box 😊
I added my feedback.

@latonz
Copy link
Contributor

latonz commented Aug 3, 2024

Relates #1406

@latonz latonz added the enhancement New feature or request label Aug 3, 2024
@hartmair hartmair changed the title feat: inline methods consisting of a single return statement or local variable declaration feat: inline methods consisting of a single return statement or local variable declaration expression Aug 3, 2024
@hartmair hartmair force-pushed the feat/inline-single-return-statement branch from 14977e2 to da50bf9 Compare August 3, 2024 06:19
@hartmair hartmair requested a review from latonz August 3, 2024 06:22
@latonz latonz force-pushed the feat/inline-single-return-statement branch from da50bf9 to 0e680f5 Compare August 3, 2024 14:43
@latonz latonz enabled auto-merge (squash) August 3, 2024 14:43
@latonz latonz merged commit a870238 into riok:main Aug 3, 2024
17 checks passed
@hartmair hartmair deleted the feat/inline-single-return-statement branch August 3, 2024 16:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants