-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
272 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
mpple-core/src/main/java/ml/wonwoo/mapped/converter/JavasLangConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package ml.wonwoo.mapped.converter; | ||
|
||
import io.vavr.collection.List; | ||
import io.vavr.collection.Seq; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Type; | ||
import ml.wonwoo.mapped.mapping.MappingInstance; | ||
import ml.wonwoo.util.ClassUtils; | ||
import net.jodah.typetools.TypeResolver; | ||
|
||
public class JavasLangConverter implements MappedConverter { | ||
|
||
private final MappingInstance mappingInstance; | ||
|
||
public JavasLangConverter(MappingInstance mappingInstance) { | ||
this.mappingInstance = mappingInstance; | ||
} | ||
|
||
@Override | ||
public boolean supports(Class<?> target) { | ||
return Seq.class.isAssignableFrom(target); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
protected <T> Seq<T> collectionCreate(Class<T> clazz) { | ||
if (clazz.isInterface()) { | ||
return List.Nil.instance(); | ||
} else { | ||
return (Seq<T>) ClassUtils.instantiateClass(clazz); | ||
} | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public Object convert(Class<?> rootClass, Object value, Class<?> target, Object context) { | ||
try { | ||
Method method = rootClass.getDeclaredMethod((String) context, target); | ||
Type genericType = ClassUtils.getGenericType(method); | ||
Class<?> type = TypeResolver.resolveRawArgument(genericType, rootClass); | ||
Seq<Object> collection = collectionCreate((Class<Object>) target); | ||
Seq<Object> list = (Seq) value; | ||
for (Object obj : list) { | ||
if (ClassUtils.isObject(type)) { | ||
collection = collection.append(mappingInstance.map(obj, type)); | ||
} else { | ||
collection = collection.append(obj); | ||
} | ||
} | ||
return collection; | ||
} catch (Exception e) { | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
mpple-core/src/test/java/ml/wonwoo/mapped/converter/JavasLangConverterTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package ml.wonwoo.mapped.converter; | ||
|
||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import io.vavr.collection.List; | ||
import io.vavr.collection.Seq; | ||
import ml.wonwoo.mapped.mapping.MappingInstanceImpl; | ||
import ml.wonwoo.model.Foo; | ||
import org.junit.Test; | ||
|
||
public class JavasLangConverterTests { | ||
|
||
|
||
private final JavasLangConverter javasLangConverter | ||
= new JavasLangConverter(new MappingInstanceImpl()); | ||
|
||
@Test | ||
public void supports() { | ||
assertThat(javasLangConverter.supports(List.class)).isTrue(); | ||
assertThat(javasLangConverter.supports(Seq.class)).isTrue(); | ||
assertThat(javasLangConverter.supports(Foo.class)).isFalse(); | ||
} | ||
|
||
@Test | ||
public void convertWrapperType() { | ||
List<String> strs = List.of("test", "foo"); | ||
FooList fooList = new FooList(); | ||
fooList.setStrs(strs); | ||
Object result = javasLangConverter.convert(FooList.class, strs, List.class, "setStrs"); | ||
List list = (List) result; | ||
assertThat(list.get(0)).isEqualTo("test"); | ||
assertThat(list.get(1)).isEqualTo("foo"); | ||
} | ||
|
||
@Test | ||
public void convertObjectType() { | ||
Foo foo = new Foo(); | ||
foo.setLastName("last"); | ||
foo.setFirstName("first"); | ||
List<Foo> foos = List.of(foo); | ||
FooList fooList = new FooList(); | ||
fooList.setFoos(foos); | ||
Object result = javasLangConverter.convert(FooList.class, foos, List.class, "setFoos"); | ||
List<Foo> list = (List) result; | ||
assertThat(list.get(0).getLastName()).isEqualTo("last"); | ||
assertThat(list.get(0).getFirstName()).isEqualTo("first"); | ||
} | ||
|
||
|
||
public static class FooList { | ||
|
||
private List<String> strs; | ||
private List<Foo> foos; | ||
|
||
public List<String> getStrs() { | ||
return strs; | ||
} | ||
|
||
public List<Foo> getFoos() { | ||
return foos; | ||
} | ||
|
||
public void setStrs(List<String> strs) { | ||
this.strs = strs; | ||
} | ||
|
||
public void setFoos(List<Foo> foos) { | ||
this.foos = foos; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
mpple-sample/src/main/java/ml/wonwoo/sample/domain/JavasCustomer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package ml.wonwoo.sample.domain; | ||
|
||
import io.vavr.collection.List; | ||
import java.util.Date; | ||
|
||
public class JavasCustomer { | ||
|
||
private Date date; | ||
|
||
private List<OrderItem> items; | ||
|
||
public Date getDate() { | ||
return date; | ||
} | ||
|
||
public void setDate(Date date) { | ||
this.date = date; | ||
} | ||
|
||
public List<OrderItem> getItems() { | ||
return items; | ||
} | ||
|
||
public void setItems(List<OrderItem> items) { | ||
this.items = items; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "OrderDto{" + | ||
"date=" + date + | ||
", items=" + items + | ||
'}'; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
mpple-sample/src/main/java/ml/wonwoo/sample/dto/JavasCustomerDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package ml.wonwoo.sample.dto; | ||
|
||
import io.vavr.collection.List; | ||
import java.util.Date; | ||
|
||
public class JavasCustomerDto { | ||
|
||
private Date date; | ||
|
||
private List<OrderItemDto> items; | ||
|
||
public Date getDate() { | ||
return date; | ||
} | ||
|
||
public void setDate(Date date) { | ||
this.date = date; | ||
} | ||
|
||
public List<OrderItemDto> getItems() { | ||
return items; | ||
} | ||
|
||
public void setItems(List<OrderItemDto> items) { | ||
this.items = items; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "OrderDto{" + | ||
"date=" + date + | ||
", items=" + items + | ||
'}'; | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
mpple-sample/src/main/java/ml/wonwoo/sample/mapper/CustomerMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
package ml.wonwoo.sample.mapper; | ||
|
||
import ml.wonwoo.sample.domain.Customer; | ||
import ml.wonwoo.sample.domain.JavasCustomer; | ||
import ml.wonwoo.sample.dto.CustomerDto; | ||
import ml.wonwoo.sample.dto.JavasCustomerDto; | ||
|
||
public interface CustomerMapper { | ||
|
||
CustomerDto customerDto(Customer customer); | ||
|
||
JavasCustomerDto javasCustomerDto(JavasCustomer customer); | ||
} |
52 changes: 52 additions & 0 deletions
52
mpple-sample/src/test/java/ml/wonwoo/sample/JavasLangTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package ml.wonwoo.sample; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import io.vavr.collection.List; | ||
import java.util.Date; | ||
import java.util.Iterator; | ||
import ml.wonwoo.Mpple; | ||
import ml.wonwoo.sample.domain.JavasCustomer; | ||
import ml.wonwoo.sample.domain.OrderItem; | ||
import ml.wonwoo.sample.dto.JavasCustomerDto; | ||
import ml.wonwoo.sample.dto.OrderItemDto; | ||
import ml.wonwoo.sample.mapper.CustomerMapper; | ||
import org.junit.Test; | ||
|
||
public class JavasLangTests { | ||
|
||
@Test | ||
public void default_sample() { | ||
CustomerMapper mapper = Mpple.builder() | ||
.target(CustomerMapper.class); | ||
JavasCustomer customer = createCustomer(); | ||
JavasCustomerDto javasCustomerDto = mapper.javasCustomerDto(customer); | ||
assertThatCustomerDto(javasCustomerDto); | ||
} | ||
|
||
private JavasCustomer createCustomer() { | ||
|
||
OrderItem orderItem = new OrderItem(); | ||
orderItem.setName("iphone"); | ||
orderItem.setPrice(9999999); | ||
|
||
OrderItem orderItem1 = new OrderItem(); | ||
orderItem1.setName("ipad"); | ||
orderItem1.setPrice(100000000); | ||
|
||
JavasCustomer javasCustomer = new JavasCustomer(); | ||
javasCustomer.setDate(new Date()); | ||
javasCustomer.setItems(List.of(orderItem, orderItem1)); | ||
return javasCustomer; | ||
} | ||
|
||
private void assertThatCustomerDto(JavasCustomerDto customerDto) { | ||
Iterator<OrderItemDto> iterator = customerDto.getItems().iterator(); | ||
OrderItemDto orderItemDto = iterator.next(); | ||
assertThat(orderItemDto.getName()).isEqualTo("iphone"); | ||
assertThat(orderItemDto.getPrice()).isEqualTo(9999999); | ||
OrderItemDto orderItemDto1 = iterator.next(); | ||
assertThat(orderItemDto1.getName()).isEqualTo("ipad"); | ||
assertThat(orderItemDto1.getPrice()).isEqualTo(100000000); | ||
} | ||
} |