-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support generic types as input and output types #35
Comments
Working test case: package com.github.roookeee.datus.generics;
import com.github.roookeee.datus.api.Datus;
import com.github.roookeee.datus.api.Mapper;
import com.github.roookeee.datus.immutable.ConstructorParameter;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class GenericsTest {
@Test
public void shouldSupportGenericTypesInTheImmutableApi() {
//given
ImmutableContainer<Integer> input = new ImmutableContainer<>(5);
Mapper<ImmutableContainer<Integer>, ImmutableContainer<String>> mapper =
Datus.<ImmutableContainer<Integer>, ImmutableContainer<String>>forTypes()
// type inference hates us a little here, the generic type of immutable is inferred by the passed lambda
// which is generic and thus object -> have to specify type although it is a bit ugly
.<String>immutable(ImmutableContainer::new)
.from(ImmutableContainer::getElement).map(Object::toString).to(ConstructorParameter::bind)
.build();
//when
ImmutableContainer<String> result = mapper.convert(input);
//then
assertThat(result.getElement()).isEqualTo(result.getElement());
}
@Test
public void shouldSupportGenericTypesInTheMutableApi() {
//given
MutableContainer<Integer> input = new MutableContainer<>();
input.setElement(5);
Mapper<MutableContainer<Integer>, MutableContainer<String>> mapper =
Datus.<MutableContainer<Integer>, MutableContainer<String>>forTypes()
.mutable(MutableContainer::new)
.from(MutableContainer::getElement).map(Object::toString).into(MutableContainer::setElement)
.build();
//when
MutableContainer<String> result = mapper.convert(input);
//then
assertThat(result.getElement()).isEqualTo(result.getElement());
}
private static class ImmutableContainer<T> {
private final T element;
public ImmutableContainer(T element) {
this.element = element;
}
public T getElement() {
return element;
}
}
private static class MutableContainer<T> {
private T element;
public T getElement() {
return element;
}
public void setElement(T element) {
this.element = element;
}
}
} |
Initial implementation in branch Will do some doc and finish this sometime today or tomorrow |
Thorough tests & documentation are done, letting it sit for a while in case I think of some weird edge-case that needs to be handled / fixed. Will release |
Merged into develop (see commit above this comment) - leaving this open until I have released a new datus version. |
Datus 1.5.0 released |
Currently the
Datus
class expects twoClass<T>
parameters to infer the input and output types. These cannot be passed if e.g.T
is aList<T>
asList<T>.class
cannot be created / obtained.We should add an overload to
Datus.forTypes()
without any parameters that leaves the type definition to the caller viaDatus.<List<Integer>, List<String>>forTypes()
(it is ugly indeed, but it works, don't think there is a better way).Thanks to #33 for noticing this issue ! :)
The text was updated successfully, but these errors were encountered: