I want to map the following classes
class Schedule {
ZoneId timezoneId;
List<AvailabilityRule> rules;
}
class AvailabilityRule {
long startEpoch;
long endEpoch;
}
to these classes.
class ScheduleDTO {
String timezone;
List<AvailabilityRuleDTO> rules;
}
class AvailabilityRuleDTO {
ZonedDateTime startTime;
ZonedDateTime endTime;
}
Both timezoneId and startEpoch are needed for calculating startTime.
Instant instant = Instant.ofEpochMilli(startEpoch);
ZonedDateTime zonedDateTime = instant.atZone(timezoneId);
How can I achieve this using mapstruct?
Pseudo code of what I want
@Mapping(source = {"startEpoch", "timezoneId"}, target = "startTime", qualifiedByName = "epochToString")
AvailabilityRuleDTO toAvailabilityRuleDTO(AvailabilityRule
availabilityRule, Schedule schedule);
This can be done in several ways. Below you see 2 options. They do the same thing only one uses
qualifiedByNamewhile the other usesexpression. Depending on your need one might fit better then the other.Using a custom method found by mapstruct
qualifiedByNamerequired because otherwise mapstruct does not know which method to use.Using a custom method configured by an expression
@Namedused here to prevent mapstruct from accidentally using this method for other mapping actions. Without it it will most likely still work.Complete mapper including schedule