You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Instead of generating a new method for each mapping we might use something similar as in #1046 and use update mapping internally. For example for the mapping:
publicclassChartEntryToArtistImplextendsChartEntryToArtist {
@OverridepublicChartEntrymap(Songsong) {
if ( song == null ) {
returnnull;
}
ChartEntrychartEntry = newChartEntry();
chartEntry.setSongTitle( song.getTitle() );
Stringcity = songArtistLabelStudioCity1( song );
if ( city != null ) {
chartEntry.setCity( city );
}
Stringname = songArtistLabelStudioName1( song );
if ( name != null ) {
chartEntry.setRecordedAt( name );
}
Stringname1 = songArtistName1( song );
if ( name1 != null ) {
chartEntry.setArtistName( name1 );
}
returnchartEntry;
}
privateStringsongArtistLabelStudioCity1(Songsong) {
if ( song == null ) {
returnnull;
}
Artistartist = song.getArtist();
if ( artist == null ) {
returnnull;
}
Labellabel = artist.getLabel();
if ( label == null ) {
returnnull;
}
Studiostudio = label.getStudio();
if ( studio == null ) {
returnnull;
}
Stringcity = studio.getCity();
if ( city == null ) {
returnnull;
}
returncity;
}
privateStringsongArtistLabelStudioName1(Songsong) {
if ( song == null ) {
returnnull;
}
Artistartist = song.getArtist();
if ( artist == null ) {
returnnull;
}
Labellabel = artist.getLabel();
if ( label == null ) {
returnnull;
}
Studiostudio = label.getStudio();
if ( studio == null ) {
returnnull;
}
Stringname = studio.getName();
if ( name == null ) {
returnnull;
}
returnname;
}
privateStringsongArtistName1(Songsong) {
if ( song == null ) {
returnnull;
}
Artistartist = song.getArtist();
if ( artist == null ) {
returnnull;
}
Stringname = artist.getName();
if ( name == null ) {
returnnull;
}
returnname;
}
}
The methods songArtistLabelStudioCity1(Song song), songArtistLabelStudioName1(Song song), songArtistName1(Song song) have a lot of duplicated code.
What if we generate this instead:
publicclassChartEntryToArtistImplextendsChartEntryToArtist {
@OverridepublicChartEntrymap(Songsong) {
if ( song == null ) {
returnnull;
}
ChartEntrychartEntry = newChartEntry();
chartEntry.setSongTitle( song.getTitle() );
updateChartEntryFromArtist( chartEntry, song.getArtist() );
returnchartEntry;
}
privatevoidupdateChartEntryFromArtist(ChartEntrychartEntry, Artistartist) {
if ( artist == null ) {
return;
}
if ( artist.getName() != null ) {
chartEntry.setArtistName( artist.getName() );
}
Labellabel = artist.getLabel();
if ( label == null ) {
return;
}
Studiostudio = label.getStudio();
if ( studio == null ) {
return;
}
if ( studio.getName() != null ) {
chartEntry.setRecordedAt( studio.getName() );
}
if ( studio.getCity() ) {
chartEntry.setCity( studio.getCity() );
}
}
}
I think that with this the readability is much improved, we gain something small in performance (we don't need to repeat the same conditions multiple times) and we generate less code.
Do you think that this is worth pursuing?
The text was updated successfully, but these errors were encountered:
@filiphr, First of all l thank you so much for building Mapstruct.
It really helped to minimize manual effort.
Waiting this enhancement to be implemented
Instead of generating a new method for each mapping we might use something similar as in #1046 and use update mapping internally. For example for the mapping:
We generate:
The methods
songArtistLabelStudioCity1(Song song)
,songArtistLabelStudioName1(Song song)
,songArtistName1(Song song)
have a lot of duplicated code.What if we generate this instead:
I think that with this the readability is much improved, we gain something small in performance (we don't need to repeat the same conditions multiple times) and we generate less code.
Do you think that this is worth pursuing?
The text was updated successfully, but these errors were encountered: