Giter Club home page Giter Club logo

Comments (5)

schuettec avatar schuettec commented on August 26, 2024 1

This kind of mapping feature would break testability, which was one of the main design goals of ReMap. In the above case you deactivated ReMap and your mapping reduces to a custom mapping method.

But if I got that right you can resolve the above example in the following way: ReMap supports multiple field mappings. That means you can define multiple transformations for the same source field - this is what you want in your example.

Please have a look:

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import com.remondis.remap.AssertMapping;
import com.remondis.remap.Mapper;
import com.remondis.remap.Mapping;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

public class MapperTest {

  @NoArgsConstructor
  @AllArgsConstructor
  @Data
  @Builder
  public static class AccountEntity {
    private BillingAddress billingAddress;
  }

  @NoArgsConstructor
  @AllArgsConstructor
  @Data
  @Builder
  public static class BillingAddress {
    private String street;
    private String city;
    private String zip;
    private String state;
    private String country;
  }

  @NoArgsConstructor
  @AllArgsConstructor
  @Data
  @Builder
  public static class SfAccount {
    private String billingStreet;
    private String billingCity;
    private String billingPostalCode;
    private String billingState;
    private String billingCountry;

  }

  @Test
  public void multiMapping() {
    Mapper<AccountEntity, SfAccount> mapper = Mapping.from(AccountEntity.class)
        .to(SfAccount.class)
        .replace(AccountEntity::getBillingAddress, SfAccount::getBillingStreet)
        .withSkipWhenNull(BillingAddress::getStreet)
        .replace(AccountEntity::getBillingAddress, SfAccount::getBillingCity)
        .withSkipWhenNull(BillingAddress::getCity)
        .replace(AccountEntity::getBillingAddress, SfAccount::getBillingPostalCode)
        .withSkipWhenNull(BillingAddress::getZip)
        .replace(AccountEntity::getBillingAddress, SfAccount::getBillingState)
        .withSkipWhenNull(BillingAddress::getState)
        .replace(AccountEntity::getBillingAddress, SfAccount::getBillingCountry)
        .withSkipWhenNull(BillingAddress::getCountry)
        .mapper();
    BillingAddress billingAddress = BillingAddress.builder()
        .city("city")
        .country("country")
        .state("state")
        .street("street")
        .zip("zip")
        .build();
    AccountEntity account = AccountEntity.builder()
        .billingAddress(billingAddress)
        .build();
    SfAccount sfaccount = mapper.map(account);
    assertEquals(billingAddress.getStreet(), sfaccount.getBillingStreet());
    assertEquals(billingAddress.getCity(), sfaccount.getBillingCity());
    assertEquals(billingAddress.getZip(), sfaccount.getBillingPostalCode());
    assertEquals(billingAddress.getState(), sfaccount.getBillingState());
    assertEquals(billingAddress.getCountry(), sfaccount.getBillingCountry());

    // The following would be the assert for your tests:
    AssertMapping.of(mapper)
        .expectReplace(AccountEntity::getBillingAddress, SfAccount::getBillingStreet)
        .andSkipWhenNull()
        .expectReplace(AccountEntity::getBillingAddress, SfAccount::getBillingCity)
        .andSkipWhenNull()
        .expectReplace(AccountEntity::getBillingAddress, SfAccount::getBillingPostalCode)
        .andSkipWhenNull()
        .expectReplace(AccountEntity::getBillingAddress, SfAccount::getBillingState)
        .andSkipWhenNull()
        .expectReplace(AccountEntity::getBillingAddress, SfAccount::getBillingCountry)
        .andSkipWhenNull()
        .ensure();
  }

}

This way you get full support for testability.

from remap.

schuettec avatar schuettec commented on August 26, 2024 1

Just the same way - just add:

        .replace(AccountEntity::getBillingAddress, SfAccount::getBillingLatitude)
        .withSkipWhenNull(billingAddress -> billingAddress.getLocation()[0])
        .replace(AccountEntity::getBillingAddress, SfAccount::getBillingLongitude)
        .withSkipWhenNull(billingAddress -> billingAddress.getLocation()[1])

from remap.

mmaryo avatar mmaryo commented on August 26, 2024

it's nice thanks !
and have you a solution for
private Double[] location;
in BillingAddress
to be map to
private Double BillingLatitude;
private Double BillingLongitude;
in SfAccount ?

from remap.

mmaryo avatar mmaryo commented on August 26, 2024

thanks a lot !

from remap.

schuettec avatar schuettec commented on August 26, 2024

You're welcome!

from remap.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.