Skip to content
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

Transform Map<K1,V> -> Map<K2,V> #84

Closed
kirpi4ik opened this issue Aug 8, 2019 · 7 comments
Closed

Transform Map<K1,V> -> Map<K2,V> #84

kirpi4ik opened this issue Aug 8, 2019 · 7 comments
Assignees
Labels
invalid This doesn't seem right

Comments

@kirpi4ik
Copy link

kirpi4ik commented Aug 8, 2019

In case of multiple distributed services which are exchanging data, in many cases the most generic deserialization format can be a simple Map<String, Object> collection with some LinkedMap or even more concrete/specific implementation. In many cases this helps to reduce the boilerplate code and avoid writing third-party beans.
Would it make sense or possible to add such feature, where map<k1,v> can be deep transformed into map<k2,v>?

@fborriello fborriello added the enhancement New feature or request label Aug 9, 2019
@fborriello fborriello added this to To do in BULL:: Roadmap via automation Aug 9, 2019
@fborriello
Copy link
Contributor

Hi @kirpi4ik, actually the library is already able to transform Maps, but, as it is now, the Map needs to be in a Java Bean, for example:

public class A {
    private final String name;
    private final Map<String, String> sampleMap;
    private final Map<String, List<String>> complexMap;
    private final Map<String, Map<String, String>> veryComplexMap;
}

is automatically transformed in something like:

public class B {
    private final String name;
    private final int[] phoneNumbers;
    private final Map<String, String> sampleMap;
    private final Map<String, List<String>> complexMap;
    private final Map<String, Map<String, String>> veryComplexMap;
}

by the way, by implementing the feature you suggested, would make BULL able to transform "primitive types" too even if they are not encapsulated in a Java Bean, I think this would be a good feature to add.
I've added it in the "to do" activities and it will be picked up soon.
If you want to contribute in the development you'll be more than welcome.

@fborriello fborriello moved this from To do to In progress in BULL:: Roadmap Aug 9, 2019
@fborriello fborriello self-assigned this Aug 9, 2019
@kirpi4ik
Copy link
Author

kirpi4ik commented Aug 9, 2019

@fborriello Actually my use case would be more about iterating/transforming across the map object which from what I understand not possible now :

class MapWrapper {
 private final Map<String, Object> map;
 public MapWrapper (Map<String, Object> map) {
        this.map = map;
    }  
}

Map<String, Object> mapSource =  = new HashMap<String, String>() {{
    put("key1", "string1");
    put("key2", 22);
    put("key3", Arrays.asList("Larry", "Moe", "Curly"));
    put("key4", new ComplexObject("title", "description"));
}};

//And then perform the field mapping and transformation through the map
new BeanUtils().getTransformer()
                         .withFieldMapping(new FieldMapping("map.key1", "map.newKey1"))
                         .withFieldMapping(new FieldMapping("map.key3", "map.newKey3"))
                         .withFieldMapping(new FieldMapping("map.key4.title", "map.key4.someOtherFieldName"))
        .transform(new MapWrapper(mapSource) , MapWrapper.class);

@fborriello
Copy link
Contributor

@kirpi4ik no you're wrong, BULL already supports this kind of transformations.
To get your object transformed, you don't need to define any FieldMapping, you just need to use this instruction:

MapWrapper transformedObject = new BeanUtils().getTransformer()
                               .transform(new MapWrapper(mapSource), MapWrapper.class);

If this is what you were looking for, we can close the issue.
Please, let me know

kirpi4ik added a commit to kirpi4ik/bull that referenced this issue Aug 9, 2019
kirpi4ik added a commit to kirpi4ik/bull that referenced this issue Aug 9, 2019
@kirpi4ik
Copy link
Author

kirpi4ik commented Aug 9, 2019

you don't need to define any FieldMapping,

@fborriello not sure I understand how I will remap the keys in a map without FieldMap usage, added a short test maybe it will make more sense ?
https://github.com/kirpi4ik/bull/blob/issue-84/bean-utils-library/src/test/java/com/hotels/beans/transformer/HashMapTransformationTest.java#L28-L30

@fborriello
Copy link
Contributor

fborriello commented Aug 9, 2019

Each element of a map is transformed automatically from the library.
I saw your test and It’s still not clear to me why you are defining field mappings.
Could you kindly try to remove the field mappings you defined and run the test again?
I don’t know if it’s the expected behavior, but, I noticed that the first assertion seems to be wrong, because the element with key1 will be not null.
If you would you can join the slack channel where we can continue this discussion and find answers to questions like this made by other users.
You can join by clicking here.

@fborriello
Copy link
Contributor

fborriello commented Aug 11, 2019

@kirpi4ik did you manage to get the expected result?
Anyway, your test should be as following:

@Test
public void testMapGenericFieldTypeWorksProperly() {
    Map<String, Object> mapSource =  new HashMap<String, Object>() {{
        put("key1", "string1");
        put("key2", 22);
        put("key3", Arrays.asList("Larry", "Moe", "Curly"));
    }};

    final MapWrapper mapWrapperTarget = new BeanUtils().getTransformer()
                                                       .transform(new MapWrapper().setMap(mapSource), MapWrapper.class);
    isNotNull(mapWrapperTarget.map.get("key1"));
    assertEquals(mapSource.get("key1"), mapWrapperTarget.map.get("newKey1"));
    assertEquals(mapSource.get("key3"), mapWrapperTarget.map.get("newKey3"));
}

Please, let me know if this helps.

@fborriello fborriello added invalid This doesn't seem right and removed enhancement New feature or request labels Aug 11, 2019
@fborriello
Copy link
Contributor

@kirpi4ik, I'm closing this issue as a dedicated one has been created: #88.
This new feature should be exactly what you need.
Please, feel free to join in the implementation

BULL:: Roadmap automation moved this from In progress to Done Aug 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right
Projects
Development

No branches or pull requests

2 participants