Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Reuse @Mapping definitions in Mapstruct

I have the mapper below that’s working fine, but I was wondering if there’s any way to define once and reuse the duplicate mappings on remoteToNewLocalApp and remoteToLocalAppUpdate.

@Mapper
interface AppMapper {

    @Mapping(target = "appId", source = "atemsPackage.appId", defaultExpression = "java(packageId)")
    @Mapping(target = "appOnRemote", constant = "true")
    fun remoteToNewLocalApp(atemsPackage: AtemsPackage, packageId: String): LocalApp

    @Mapping(target = "appId", source = "atemsPackage.appId", defaultExpression = "java(packageId)")
    @Mapping(target = "appOnRemote", constant = "true")
    fun remoteToLocalAppUpdate(atemsPackage: AtemsPackage, packageId: String): LocalAppRemoteUpdate

    companion object {
        val INSTANCE: AppMapper =
            Mappers.getMapper(AppMapper::class.java)
    }
}

UPDATE

@Mapper
interface AppMapper {

    @Mapping(target = "appId", source = "atemsPackage.appId", defaultExpression = "java(packageId)")
    @Mapping(target = "appOnRemote", constant = "true")
    fun remoteToNewLocalApp(atemsPackage: AtemsPackage, packageId: String): LocalApp

    @InheritConfiguration(name = "remoteToNewLocalApp")
    fun remoteToLocalAppUpdate(atemsPackage: AtemsPackage, packageId: String): LocalAppRemoteUpdate

    companion object {
        val INSTANCE: AppMapper =
            Mappers.getMapper(AppMapper::class.java)
    }
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

you can use @InheritConfiguration on the second mapper method.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading