1

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?

Maelig
  • 2,046
  • 4
  • 24
  • 49
barnacle.m
  • 2,070
  • 3
  • 38
  • 82
  • I am not sure that I follow. What do you have so far? You should be able to apply formatting on the relationships if you define a mapper for `Contact` (it should work the same as for the `ClientMapper`. I also suppose you need to define some custom mappings for `HasMany` to `RealmList` as they are not known to MapStruct. Do you have something like that already? – Filip Oct 29 '17 at 10:37
  • I believe I'll have to create custom mappings for those relationships. – barnacle.m Oct 30 '17 at 23:19
  • 2
    You could use a Decorator on your Mapper and manually parse those Dates : http://mapstruct.org/documentation/stable/reference/html/#customizing-mappers-using-decorators – Maelig Aug 02 '18 at 12:54
  • @Maelig I landed up doing pretty much exactly that :) – barnacle.m Aug 02 '18 at 13:02
  • You should post your solution and accept it as the right answer to help people with same kind of problem ;) – Maelig Aug 03 '18 at 09:02
  • The problem is the answer doesn't really match the question, i.e. it doesn't actually have much if anything to do with Realm io. – barnacle.m Aug 23 '18 at 14:20

0 Answers0