mapperly icon indicating copy to clipboard operation
mapperly copied to clipboard

Expression mapper

Open ranma42 opened this issue 2 years ago • 2 comments

Expression mappers

Description

Implement the Expression<Func<A, B>> map generation.

This is a WIP attempt at extending the source generator. The code is most likely NOT ready for a review (yet 😇 ).

Fixes #733

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)

ranma42 avatar Nov 25 '23 22:11 ranma42

I managed to get https://github.com/riok/mapperly/issues/733 mostly working but I need some directions on how to approach a couple of issues.

Specifically, I am hacking around the fact that the expr methods are not supposed to have a source in https://github.com/ranma42/mapperly/commit/53e62b10955cf337841f2c7d81c580fd5a59ba22#diff-39772ddec5eba09b32838264b5f579c437ce1452f4c4a8f5f3d3f2ad1779bbcfR280-R281 and again in https://github.com/ranma42/mapperly/commit/53e62b10955cf337841f2c7d81c580fd5a59ba22#diff-6771c2c8998836aea4afc8eec9c5d28109aa1544052ef52756889fcc6e7cba99R59

What is the right approach here? I can see several ways forward, but I am not sure what is the right approach. Some proposals:

  • should have a fake source parameter of type void (injected when extracting, dropped when emitting)
  • should extend the code to allow null as source parameter Right now my commit follows the first approach, as it results in a smaller impact in the codebase.

A related issue I am unsure about is: for expression mappers, do we expect the types to be:

  • source: void, dest: expr<func<A,B>>, or
  • source: A, dest: B ? I was afraid that the second approach would risk conflicts when looking up existing mappers and make it hard/impossible for users to provide their own implementations, so I went with the first one.

Sorry if I opened this as a (draft) PR even though it is not yet ready for review (and most checklist requirements are not met), but I thought it would be easier to ask for directions on these with the code on the side

ranma42 avatar Nov 25 '23 22:11 ranma42

@ranma42 thank you for your efforts! I haven't done a detailed review of the code yet, but your decisions sound reasonable. I'm not sure about the void source parameter though 🤔 At first glance it looks a bit "hacky"... Especially parts like source ?? LiteralExpression(SyntaxKind.NullLiteralExpression); in the TypeMappingBuildContext or SourceParameter = sourceParameter.Ordinal >= 0 ? sourceParameter : null in the MethodMapping. IMO the "more correct" way would be to set the source parameter nullable. I haven't tried it out, not sure what implications it has on the rest of the source code.

latonz avatar Dec 02 '23 15:12 latonz

Closing for now as no updates were provided for a longer time period. I would be happy to review a new PR with updates, feel free to open a new PR if you plan to continue your work on this.

latonz avatar Mar 11 '24 08:03 latonz