diff --git a/mpple-core/pom.xml b/mpple-core/pom.xml index 8a3b451..97fcc5b 100644 --- a/mpple-core/pom.xml +++ b/mpple-core/pom.xml @@ -32,6 +32,12 @@ joda-time true + + io.vavr + vavr + 0.9.2 + true + diff --git a/mpple-core/src/main/java/ml/wonwoo/mapped/converter/JavasLangConverter.java b/mpple-core/src/main/java/ml/wonwoo/mapped/converter/JavasLangConverter.java new file mode 100644 index 0000000..d93e944 --- /dev/null +++ b/mpple-core/src/main/java/ml/wonwoo/mapped/converter/JavasLangConverter.java @@ -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 Seq collectionCreate(Class clazz) { + if (clazz.isInterface()) { + return List.Nil.instance(); + } else { + return (Seq) 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 collection = collectionCreate((Class) target); + Seq 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; + } + } +} diff --git a/mpple-core/src/main/java/ml/wonwoo/mapped/mapping/MappingInstanceImpl.java b/mpple-core/src/main/java/ml/wonwoo/mapped/mapping/MappingInstanceImpl.java index 1fe4166..4751d0f 100644 --- a/mpple-core/src/main/java/ml/wonwoo/mapped/mapping/MappingInstanceImpl.java +++ b/mpple-core/src/main/java/ml/wonwoo/mapped/mapping/MappingInstanceImpl.java @@ -4,6 +4,7 @@ import ml.wonwoo.mapped.converter.ArrayConverter; import ml.wonwoo.mapped.converter.CollectionConverter; import ml.wonwoo.mapped.converter.EnumConverter; +import ml.wonwoo.mapped.converter.JavasLangConverter; import ml.wonwoo.mapped.converter.MapConverter; import ml.wonwoo.mapped.converter.MappedConverter; import ml.wonwoo.mapped.converter.MappedConverterComposite; @@ -48,6 +49,10 @@ private void defaultConverter() { new ArrayConverter(this), new EnumConverter() ); + + if (ClassUtils.isJavasLang()) { + this.converter.addConverter(new JavasLangConverter(this)); + } } public void addConverter(MappedConverter mappedConverter) { diff --git a/mpple-core/src/main/java/ml/wonwoo/util/ClassUtils.java b/mpple-core/src/main/java/ml/wonwoo/util/ClassUtils.java index 89bfbc0..252779d 100644 --- a/mpple-core/src/main/java/ml/wonwoo/util/ClassUtils.java +++ b/mpple-core/src/main/java/ml/wonwoo/util/ClassUtils.java @@ -67,7 +67,6 @@ public abstract class ClassUtils { javaType.put(CurrencyUnit.class, CurrencyUnit.class); javaType.put(MonetaryAmount.class, MonetaryAmount.class); } - wrapperType = Collections.unmodifiableMap(wrapper); defaultJavaType = Collections.unmodifiableMap(javaType); @@ -89,6 +88,10 @@ public static boolean isObject(Class type) { return true; } + public static boolean isJavasLang() { + return isPresent("io.vavr.collection.Seq"); + } + public static boolean isJava8() { return isPresent("java.util.Optional"); } diff --git a/mpple-core/src/test/java/ml/wonwoo/mapped/converter/JavasLangConverterTests.java b/mpple-core/src/test/java/ml/wonwoo/mapped/converter/JavasLangConverterTests.java new file mode 100644 index 0000000..2619010 --- /dev/null +++ b/mpple-core/src/test/java/ml/wonwoo/mapped/converter/JavasLangConverterTests.java @@ -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 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 foos = List.of(foo); + FooList fooList = new FooList(); + fooList.setFoos(foos); + Object result = javasLangConverter.convert(FooList.class, foos, List.class, "setFoos"); + List list = (List) result; + assertThat(list.get(0).getLastName()).isEqualTo("last"); + assertThat(list.get(0).getFirstName()).isEqualTo("first"); + } + + + public static class FooList { + + private List strs; + private List foos; + + public List getStrs() { + return strs; + } + + public List getFoos() { + return foos; + } + + public void setStrs(List strs) { + this.strs = strs; + } + + public void setFoos(List foos) { + this.foos = foos; + } + } +} \ No newline at end of file diff --git a/mpple-sample/pom.xml b/mpple-sample/pom.xml index f82ef28..fa9cc93 100644 --- a/mpple-sample/pom.xml +++ b/mpple-sample/pom.xml @@ -40,6 +40,12 @@ mpple-spring-boot-starter + + io.vavr + vavr + 0.9.2 + + org.springframework.boot spring-boot-starter-test diff --git a/mpple-sample/src/main/java/ml/wonwoo/sample/domain/JavasCustomer.java b/mpple-sample/src/main/java/ml/wonwoo/sample/domain/JavasCustomer.java new file mode 100644 index 0000000..ad7e287 --- /dev/null +++ b/mpple-sample/src/main/java/ml/wonwoo/sample/domain/JavasCustomer.java @@ -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 items; + + public Date getDate() { + return date; + } + + public void setDate(Date date) { + this.date = date; + } + + public List getItems() { + return items; + } + + public void setItems(List items) { + this.items = items; + } + + @Override + public String toString() { + return "OrderDto{" + + "date=" + date + + ", items=" + items + + '}'; + } +} diff --git a/mpple-sample/src/main/java/ml/wonwoo/sample/dto/JavasCustomerDto.java b/mpple-sample/src/main/java/ml/wonwoo/sample/dto/JavasCustomerDto.java new file mode 100644 index 0000000..e16c8a5 --- /dev/null +++ b/mpple-sample/src/main/java/ml/wonwoo/sample/dto/JavasCustomerDto.java @@ -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 items; + + public Date getDate() { + return date; + } + + public void setDate(Date date) { + this.date = date; + } + + public List getItems() { + return items; + } + + public void setItems(List items) { + this.items = items; + } + + @Override + public String toString() { + return "OrderDto{" + + "date=" + date + + ", items=" + items + + '}'; + } +} diff --git a/mpple-sample/src/main/java/ml/wonwoo/sample/mapper/CustomerMapper.java b/mpple-sample/src/main/java/ml/wonwoo/sample/mapper/CustomerMapper.java index 955577f..08bad0b 100644 --- a/mpple-sample/src/main/java/ml/wonwoo/sample/mapper/CustomerMapper.java +++ b/mpple-sample/src/main/java/ml/wonwoo/sample/mapper/CustomerMapper.java @@ -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); } diff --git a/mpple-sample/src/test/java/ml/wonwoo/sample/JavasLangTests.java b/mpple-sample/src/test/java/ml/wonwoo/sample/JavasLangTests.java new file mode 100644 index 0000000..6eacd91 --- /dev/null +++ b/mpple-sample/src/test/java/ml/wonwoo/sample/JavasLangTests.java @@ -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 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); + } +}