lambda expression in API conversions
See original GitHub issueHi Alessandro Vurro, I love you framework and how fast it can get , ans would be great if you can add the following ideas
1 - mix between xml configuration and annotation digging around the code I found that xml configuration takes higher precedence, which is normal ,so what I wish for is the possiblity to mix between the two for a single class for example I have a class called currency that I applied some annotation to it , but I need to make dynamic custom conversion so I needed to do it in xml or api style, but when I do that all the annotation is gone , and I am forced to move all the annotation into the xml , would be great if the two can be applied.
2- mapping between destination and source list , for example JMapper<Dest, Source> mapper = new JMapper<>(Dest.class, Source.class); mapper.getDestinationList(source); or mapper.getDestination(// list of source and return list of destination); it will be also great if I can control the collection type , or maybe return a configured map of key value.
3- using functional API and lambda for custom conversion. to see this through I will apply a simple example with the following code.
public class Currency {
String code;
String name;
int value;
// getters and setters
}
public class Dest {
@JMap
String test;
@JMap("code")
Currency currency;
}
public class Source {
@JMap
String test;
String code;
}
public class Service {
public Currency getCurrency(String code) {
Currency curr = new Currency();
curr.setCode(code);
curr.setName("egypt");
curr.setValue(123);
return curr;
}
}
public static void main(String[] args) {
Source source = new Source();
source.setCode("EGypt");
source.setTest("test here");
ModelMapper map = new DefaultModelMapper();
JMapperAPI api = new JMapperAPI();
Service service = new Service();
api.add(JMapperAPI.mappedClass(Dest.class).add(new Attribute("currency").value("code"))
.add(new Conversion("conversion").from("code").to("currency").body(""
+ "System.out.println(\"trying dynamic covnersion\");"
+ "org.test.jmapper2.Service service = new org.test.jmapper2.Service();"
+" org.test.jmapper2.Currency cur = service.getCurrency(${source});"
+ "return cur;"))
);
JMapper<Dest, Source> mapper = new JMapper<>(Dest.class, Source.class, api);
Dest d = mapper.getDestination(source);
}
as from the example what I want to do is to custom convert between a String code , into a currency object, if I use api or xml , it will be erro prone plus , javaassist complaint a lot tell I got it right , and what if I want the vice versa of the conversion to convert from a destination to a source, so my idea is maybe if we used functional api to supply the custom conversion code then that would be great for example
Conversion convert = new Conversion("name")
convert.from("code").to("currency").body( (source) -> service.getCurrency(source.code));
I will also attach a wrapper class I made to solve this kind of problems , would love your feedback. kindly find the google groups link for the files https://groups.google.com/forum/#!topic/jmapper-framework/m8Dx9uMXsxM
Best regards Farouk Elabady
Issue Analytics
- State:
- Created 7 years ago
- Comments:13 (7 by maintainers)
Top GitHub Comments
Hi Farouk,
Thank you for the support, this encourages me to do better. In regard of your proposals:
I need to study ByteBuddy to find another solution.
sorry for the late reply here is the XML document
also here is the code I wrote, maybe I did something wrong