mapstruct / mapstruct/mapstruct-examples
Add example for SolrDocument + Mapstruct
まだ誰も着手していません。
- 主要言語
- Java
- スター
- 1.4k
- フォーク
- 504
- PR マージ指標
- 30日以内にマージされた PR はありません
説明
It would be nice to have an example that shows how to use SolrDocument with Mapstruct.
I was trying to use MapStruct with SolrDocument and couldn't find a comprehensive example that could guide me . I was facing below issue during compiling:
**Can't generate mapping method from iterable type to non-iterable type.**
Reason is [SolrDocument](https://github.com/apache/lucene-solr/blob/master/solr/solrj/src/java/org/apache/solr/common/SolrDocument.java) implements Iterable interface and Target DTOs doesn't .
So i came up with below solution to deal with this.
**Source Class**
[SolrDocument](https://github.com/apache/lucene-solr/blob/master/solr/solrj/src/java/org/apache/solr/common/SolrDocument.java)
Here SolrDocument internally has Map _fields;
**Destination Class**:
`public class ItemDTO {
private String id;
private String displayName;
private String availability;
private String price;
//getter
//setter
}
}`
**Error : Can't generate mapping method from iterable type to non-iterable type.**
**Solution**:
Created SolrResponseWrapper around SolrDocument like below:
`public class SolrResponseWrapper {
private SolrDocument document;
public SolrResponseWrapper(SolrDocument document) {
this.document = document;
}
public String get(String key) {
return this.document.get(key).toString();
}
}`
**Now Mapper is defined below for SolResponseWrapper to ItemDTO.**
`@Mapper
public interface ItemMapper {
ItemMapper INSTANCE = Mappers.getMapper(ItemMapper);
@Mappings({
@Mapping(expression = "java(record.get(\"product_id\"))", target = "id"),
@Mapping(expression = "java(record.get(\"display_name\"))", target = "displayName"),
@Mapping(expression = "java(record.get(\"availability\"))", target = "availability"),
@Mapping(expression = "java(record.get(\"price\"))", target = "price")
})
ItemDTO convertToItemDTO(SolrResponseWrapper record);
}`
If you think this is a good addition, please let me know. I will raise PR to have it included as an example.
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、リポジトリ内の既存の Java サンプルレイアウトを確認し、次に issue にある SolrDocument、SolrResponseWrapper、ItemDTO、ItemMapper の例を出発点として使用します。wrapper ベースのマッピングを示す、焦点を絞った例を追加し、その例が報告された iterable-to-non-iterable の問題とその解決策を実証していることを確認します。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- java
- 領域
- documentation
- issue の種類
- ドキュメント
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100