atos1990 / orika

Automatically exported from code.google.com/p/orika
0 stars 0 forks source link

Allow converters for Lists (or other collections). #44

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
It would be nice to be able to do:

  public static class Product {
    private String name;
  }

  public static class Order {
    private List<Product> products;
  }

  public static class OrderDto {
    private List<String> productNames;
  }

.registerConverter("productToName", new CustomConverter<Product, String>() {

          @Override
          public String convert(Product source, Type<? extends String> destinationType) {
            return source.getName();
          }
        });

.registerClassMap(ClassMapBuilder.map(Order.class, 
OrderDto.class).fieldMap("products", 
"productNames").converter("productToName").add().toClassMap());

Currently this fails with a class cast exception because orika tries to convert 
the *list* to a string instead of the individual items.

The attached test case shows a work around (with converter from List<Product> 
to List<String>), but this is not desirable, because a converter would be 
needed for every list<type>.

Original issue reported on code.google.com by maartenw...@gmail.com on 29 Aug 2012 at 9:36

Attachments:

GoogleCodeExporter commented 9 years ago
Thanks for the test, this is the wanted behavior for identified converter :(
but we still make an exception for "Collections"

Anyway for list, you can already register unidentified converter,
just like the attached piece of code. But this will call the converter anytime 
it find Product<->String (for example)

Original comment by elaat...@gmail.com on 29 Aug 2012 at 10:07

Attachments:

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
This capability (as originally requested) is now available; there is a special 
specification which detects when a converter has been specified on an array or 
collection to array-or-collection field map and the converter's types match the 
fields' element types...
please take a look

Original comment by matt.deb...@gmail.com on 21 Jun 2013 at 8:13

GoogleCodeExporter commented 9 years ago

Original comment by elaat...@gmail.com on 5 Dec 2013 at 10:48