ExpediaGroup/bull

Enable one-to-many field mapping and field transformer

fborriello opened this issue · 1 comments

Description

As of today, it's possible to define a one-to-one FieldMapping and FieldTransformer between a source field and a destination one.

The purpose of this is to add the possibility to map the same source field to multiple destination fields:

var fieldMapping = new FieldMapping<String, List<String>>("sourceFieldName", "destinationFieldOne", "destinationFieldTwo");
var multipleDestinationFieldTransformer =
                new FieldTransformer<String, String>(List.of("sourceFieldName", "destinationFieldOne"),
                        val -> val.toUpperCase()));

Expected behaviour

Given the following source class:

public class SourceClass {
    private final String name;
    private final int id;
}

the following destination class:

public class DestinationClass {
    private final String name;
    private final int id;
    private final int index;
}

and the following operations:

var sourceObj = new SourceClass("foo", 123);

var multipleFieldMapping = new FieldMapping<>("id", "index", "identifier");

var multipleDestinationFieldTransformer =
                new FieldTransformer<String, String>(List.of("id", "index"),
                        val -> val + 1));

var destObj = new BeanUtils().getBeanTransformer()
                     .withFieldMapping(multipleFieldMapping)
                     .transform(sourceObj, DestinationClass.class);

System.out.println("name = " + destObj.getName());
System.out.println("id = " + destObj.getId());
System.out.println("index = " + destObj.getIndex());

the output will be:

name = foo
id = 124
index = 124

Acceptance Criteria

  • The transformer is mapping the specified source field value into all the specified destinations one

The feature is available starting from the following library version: