question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

lambda expression in API conversions

See original GitHub issue

Hi 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:closed
  • Created 7 years ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
avurrocommented, Jun 19, 2016

Hi Farouk,

Thank you for the support, this encourages me to do better. In regard of your proposals:

  • 1 It should be a feature that already exists, if as you say it does not work, it’s a bug.
  • 2 I think it is more correct to encapsulate the list into a class and pass these to JMapper
  • 3 You can do this with static conversion in annotation. In XML/API is mandatory write body in string format actually, a workaround can be call a method that have custom conversion inside, for example:
<conversion>
return package.Service.conversion(${source});
</conversion>

I need to study ByteBuddy to find another solution.

0reactions
faroukelabadycommented, Jun 21, 2016

sorry for the late reply here is the XML document


<?xml version="1.0" encoding="UTF-8"?>
<jmapper
   xmlns="http://jmapper-framework.github.io"
   xmlns:xsi="http://jmapper-framework.github.io/jmapper-core"
   xsi:noNamespaceSchemaLocation="http://jmapper-framework.github.io/jmapper-core/jmapper-1.6.0.xsd">
   <class name = "org.test.jmapper2.Dest">
      <conversion  name= "conversion" from ="code" to ="currency">return ${source};
      </conversion>
   </class>
</jmapper>

also here is the code I wrote, maybe I did something wrong


public class Dest {

    @JMap
    String test;

    @JMap("code")
    String currency;

    public String getTest() {
        return test;
    }


    public void setTest(String test) {
        this.test = test;
    }


    public String getCurrency() {
        return currency;
    }

    public void setCurrency(String currency) {
        this.currency = currency;
    }

}
public class Source {

    String test;
    String code;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getTest() {
        return test;
    }

    public void setTest(String test) {
        this.test = test;
    }
}


public class Main {

    public static void main(String[] args) {

        JMapperAPI api = new JMapperAPI().add(
                mappedClass(Dest.class).add(
                    conversion("conversion").from("code").to("currency")
                        .body("return ${source};")));
System.out.println(api.toXStream().toString());
      Source source = new Source();
      source.setCode("USD");
      source.setTest("test  here");

      JMapper<Dest, Source> mapper = new JMapper<>(Dest.class, Source.class, api);
      Dest d = mapper.getDestination(source);   
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Build an API Gateway REST API with Lambda integration
Learn how to create and test an API to expose a Lambda function with the Lambda integration using the API Gateway console.
Read more >
Translation of Lambda Expressions
This document outlines the strategy for translating lambda expressions and method references from Java source code into bytecode. Lambda expressions for Java ...
Read more >
Lambda improvements - C# 10.0 draft specifications
Those conversions are sufficient for converting to strongly-typed delegate types and expression tree types. The function_type conversions above ...
Read more >
Build a scalable, reliable, and inexpensive RESTful API using ...
Since we are building a RESTful API, we need to use API Gateway to trigger the Lambda function. API Gateway is a fully...
Read more >
Implicit conversion from lambda expression to user-defined type
@Sajjad: I have different lambda expressions which "mean" to be a function from an element of type OPTelement to T. My aim is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found