Skip to content

Instantly share code, notes, and snippets.

@nestharus
Created October 31, 2018 21:38
Show Gist options
  • Save nestharus/484af2ce7563d756814dc102e2bec1df to your computer and use it in GitHub Desktop.
Save nestharus/484af2ce7563d756814dc102e2bec1df to your computer and use it in GitHub Desktop.
class ClassTest {
public static class A_MODEL {
public B_MODEL b;
public C_MODEL c;
}
public static class A_DTO {
public B_DTO b;
}
public static class B_MODEL {
public String name;
}
public static class C_MODEL {
public B_MODEL b;
public String name;
}
public static class B_DTO {
public String n;
public C_DTO c;
}
public static class C_DTO {
public String n;
}
}
import org.mapstruct.Qualifier;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
interface MapperTest {
@Qualifier
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.CLASS)
@interface Dto { }
@Qualifier
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.CLASS)
@interface Model { }
@Qualifier
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.CLASS)
@interface Pipe { }
}
import org.mapstruct.*;
import org.springframework.stereotype.Service;
import static ClassTest.*;
@Service
@Mapper(componentModel = "spring", injectionStrategy = InjectionStrategy.CONSTRUCTOR, uses = {
MapperTestB.class,
MapperTestC.class
})
interface MapperTestA extends MapperTest {
@Mapping(target = "b", source = "source", qualifiedBy = Pipe.class)
A_DTO map(final A_MODEL source);
@Mapping(target = "b", source = "b", qualifiedBy = Model.class)
@Mapping(target = "c", source = "source", qualifiedBy = Pipe.class)
A_MODEL map(final A_DTO source);
@Mapping(target = "bn", source = "b")
@Mapping(target = "cn", source = "c")
MapperTestB.PipePackage pipeB(final A_MODEL source);
@Mapping(target = "model", source = "b", qualifiedBy = Model.class)
@Mapping(target = "source", source = "b.c")
MapperTestC.PipePackage pipeC(final A_DTO source);
}
import org.mapstruct.*;
import org.springframework.stereotype.Service;
import static ClassTest.*;
@Service
@Mapper(componentModel = "spring", injectionStrategy = InjectionStrategy.CONSTRUCTOR, uses = {
MapperTestC.class
})
interface MapperTestB extends MapperTest {
@Dto
@Mapping(target = "n", source = "source.name")
@Mapping(target = "c", source = "cModel", qualifiedBy = MapperTestC.Dto.class)
B_DTO map(final B_MODEL source, final C_MODEL cModel);
@Model
@Mapping(target = "name", source = "n")
B_MODEL map(final B_DTO source);
@Pipe default B_DTO pipe(final PipePackage source) {
return map(source.bn, source.cn);
}
class PipePackage {
public B_MODEL bn;
public C_MODEL cn;
}
}
import org.mapstruct.*;
import org.springframework.stereotype.Service;
import ClassTest.*;
@Service
@Mapper(componentModel = "spring", injectionStrategy = InjectionStrategy.CONSTRUCTOR)
interface MapperTestC extends MapperTest {
@Dto
@Mapping(target = "n", source = "name")
C_DTO map(final C_MODEL source);
@Model
@Mapping(target = "name", source = "source.n")
@Mapping(target = "b", source = "bModel")
C_MODEL map(final C_DTO source, final B_MODEL bModel);
@Pipe default C_MODEL pipe(final PipePackage source) {
return map(source.source, source.model);
}
class PipePackage {
public C_DTO source;
public B_MODEL model;
}
}
@nestharus
Copy link
Author

nestharus commented Oct 31, 2018

Following works perfectly, but still have the pipes

package us.alchemy.connect.DTOs;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Qualifier;
import org.springframework.stereotype.Service;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@service
@Mapper(componentModel = "spring")
public interface MapperTest {
// mappings
@mapping(target = "b", source = "source", qualifiedBy = Pipe.class)
A_DTO map(final A_MODEL source);

@Mapping(target = "c", source = "source", qualifiedBy = Pipe.class)
A_MODEL map(final A_DTO source);

@Mapping(target = "n", source = "source.name")
@Mapping(target = "c", source = "cModel")
B_DTO map(final B_MODEL source, final C_MODEL cModel);

@Mapping(target = "name", source = "n")
B_MODEL map(final B_DTO source);

@Mapping(target = "n", source = "name")
C_DTO map(final C_MODEL source);

@Mapping(target = "name", source = "source.n")
@Mapping(target = "b", source = "bModel")
C_MODEL map(final C_DTO source, final B_MODEL bModel);

// pipes
@Mapping(target = "bn", source = "b")
@Mapping(target = "cn", source = "c")
PipePackageB pipeB(final A_MODEL source);

@Mapping(target = "model", source = "b")
@Mapping(target = "source", source = "b.c")
PipePackageC pipeC(final A_DTO source);

@Pipe default C_MODEL pipe(final PipePackageC source) {
    return map(source.source, source.model);
}

@Pipe default B_DTO pipe(final PipePackageB source) {
    return map(source.bn, source.cn);
}

// packages
class PipePackageB {
    public B_MODEL bn;
    public C_MODEL cn;
}
class PipePackageC {
    public C_DTO source;
    public B_MODEL model;
}

// annotations
@Qualifier
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.CLASS)
@interface Pipe { }

// objects
class A_MODEL {
    public B_MODEL b;
    public C_MODEL c;
}

class A_DTO {
    public B_DTO b;
}

class B_MODEL {
    public String name;
}

class C_MODEL {
    public B_MODEL b;
    public String name;
}

class B_DTO {
    public String n;

    public C_DTO c;
}

class C_DTO {
    public String n;
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment