-->

MapStruct - mapping RealmList relationship

2019-08-18 10:13发布

问题:

Okay, I'm not very sure how to ask this but I'm going to try. I am using MapStruct to map my incoming network objects to database objects. I use Realm.io as my local datastore. Some of my objects have RealmList<Obj> which store their relationships, for example:

public class Client extends RealmObject {
    @PrimaryKey
    private String id = UUID.randomUUID().toString();
    private Date createdAt = new Date();
    private Date updatedAt = new Date();
    private RealmList<Contact> contacts; // <-- this guy
    // constructors and getters/setters
}

I use moshi-jsonapi as my deserializer. The equivalent dto fields are

private String createdAt = new Date();
private String updatedAt = new Date();
private HasMany<Contact> contacts;

The problem: Getting MapStruct to properly convert the HasMany to RealmList. One of the issues I'm having is correctly parsing the ISO8601 date fields in the relationships. I can do it on an object's attributes, not on its relationships. Here's a functioning Mapper as an example:

@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface ClientMapper {
    ClientMapper INSTANCE = Mappers.getMapper(ClientMapper.class);

    @Mappings({
            @Mapping(target = "createdAt", dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"),
            @Mapping(target = "updatedAt", dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
    })
    Client resourceToRealm(biz.kusasa.saleboat.jsonapi.resources.Client client);

    List<Client> resourcesToRealms(List<biz.kusasa.saleboat.jsonapi.resources.Client> clients);
}

However those date parsing rules don't seem to apply when mapping a relationship. Any mapstruct experts out there?