mapstruct / mapstruct/mapstruct
QOL improvement with @PrimaryMappingSource instead of explicit source declaration
まだ誰も着手していません。
- 主要言語
- Java
- スター
- 7.7k
- フォーク
- 1.1k
- PR マージ指標
- 30日以内にマージされた PR はありません
説明
Use case
Sometimes, a mapping needs to borrow a field from another table.
@Mapping(target = "borrowed", source = "tblB.borrowed")
EntityA toEntity(TblA tblA, TblB tblB)
But if those tables have many similar fields, then we'll get lots of errors
error: Several possible source properties for target property "id"
error: Several possible source properties for target property "name"
error: Several possible source properties for target property "description"
error: Several possible source properties for target property "createdAtUtc"
error: Several possible source properties for target property "updatedAtUtc"
which can be resolved by manually specifying the source of every conflicting property
@Mapping(target = "id", source = "tblA.id")
@Mapping(target = "id", source = "tblA.name")
@Mapping(target = "id", source = "tblA.description")
@Mapping(target = "id", source = "tblA.createdAtUtc")
@Mapping(target = "id", source = "tblA.updatedAtUtc")
@Mapping(target = "borrowed", source = "tblB.borrowed")
EntityA toEntity(TblA tblA, TblB tblB)
My proposal is a new annotation called something like @PrimaryMappingSource which would be used like this
@Mapping(target = "borrowed", source = "tblB.borrowed")
EntityA toEntity(
@PrimaryMappingSource TblA tblA,
TblB tblB
)
Which would have the effect of defaulting to tblA when there is any ambiguity in the source for a target property. This is similar to the @org.springframework.context.annotation.Primary annotation, which they explain like this:
Indicates that a bean should be given preference when multiple candidates are qualified to autowire a single-valued dependency. If exactly one 'primary' bean exists among the candidates, it will be the autowired value.
Generated Code
The generated code would be the same as manually specifying the source of all those ambiguous properties
Possible workarounds
No response
MapStruct Version
No response
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
この issue には、ソースファイル、テスト、エントリーポイントが記載されていません。まず、MapStruct の曖昧な source-property の解決と annotation-processing のテストを見つけ、次に既存のパラメーター annotation の規約と比較してください。完了条件には、primary-source annotation のサポート、曖昧なプロパティのカバレッジ、明示的な source 宣言と同等の生成 mappings を含めてください。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- java
- 領域
- developer-experience, tooling
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 25/100