Comments (5)
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.
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.
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.
thanks a lot !
from remap.
You're welcome!
from remap.
Related Issues (20)
- Property path needs update to 0.1.0 HOT 1
- Property path support for set would be helpful
- Improve exception messages HOT 5
- Mapping is not applied for Set properties HOT 5
- Exception when creating mapper for generic type HOT 4
- Support for hierarchy HOT 3
- Map-Optional is missing for collections HOT 1
- Hamcrest not in test scope HOT 1
- Feature request: Allow chained/fluent setters HOT 9
- Is it possible to update an existing object instead of creating a new one? HOT 4
- Android Support HOT 10
- custom mapping HOT 1
- Support mapping contributors? HOT 1
- An illegal reflective access operation has occurred HOT 4
- Mapping collection with specific implementation HOT 6
- A read-only property causes misleading exception when omitted as destination property
- Reassign property of Type Map fails with an EmptyStack exception HOT 4
- Java 17 compatibility HOT 1
- Feature: Default setting for mapping of null values wanted
- Upgrade to Property-Path version 0.2.0.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from remap.