Implicit Transformers Useful transformers for subclasses mapping.
In this guide, we'll review two new built in transformers added in ShapeShift version 0.0.7, ImplicitMappingTransformer
and ImplicitCollectionMappingTransformer
.
ImplicitMappingTransformer
ImplicitMappingTransformer is used to transform subclasses mapped with ShapeShift. Lets look at the following example:
We have mapped Address
class to its display class AddressDisplay
.
Kotlin Java
Copy @DefaultMappingTarget (AddressDisplay:: class )
class Address {
@MappedField
var country: String ? = null
@MappedField
var city: String ? = null
@MappedField
var address: String ? = null
}
class AddressDisplay {
var country: String ? = null
var city: String ? = null
var address: String ? = null
}
Copy @ DefaultMappingTarget (AddressDisplay :: class)
public class Address {
@ MappedField
private String country;
@ MappedField
private String city;
@ MappedField
private String address;
// Getters and Setters...
}
public class AddressDisplay {
private String country;
private String city;
private String address;
// Getters and Setters...
}
We can use ShapeShift to map Address
to AddressDisplay
. But what we do if Address
is a subclass? We use the ImplicitMappingTransformer
.
Kotlin Java
Copy @DefaultMappingTarget (UserDisplay:: class )
class User {
@MappedField
var name: String ? = null
@MappedField (transformer = ImplicitMappingTransformer:: class )
var address: Address ? = null
}
class UserDisplay {
var name: String ? = null
var address: AddressDisplay ? = null
}
Copy @ DefaultMappingTarget (UserDisplay :: class)
public class User {
@ MappedField
private String name;
@ MappedField (transformer = ImplicitMappingTransformer . class )
private Address address;
// Getters and Setters...
}
public class UserDisplay {
private String name;
private AddressDisplay address;
// Getters and Setters...
}
The ImplicitMappingTransformer
uses the ShapeShift instance internally to map Address
to AddressDisplay
.
ImplicitCollectionMappingTransformer
ImplicitCollectionMappingTransformer
has the same job as ImplicitMappingTransformer
but for collections.
Kotlin Java
Copy @DefaultMappingTarget (UserDisplay:: class )
class User {
@MappedField
var name: String ? = null
@MappedField (transformer = ImplicitCollectionMappingTransformer:: class )
var addresses: List < Address >? = null
}
class UserDisplay {
var name: String ? = null
var addresses: List < AddressDisplay >? = null
}
Copy @ DefaultMappingTarget (UserDisplay :: class)
public class User {
@ MappedField
private String name;
@ MappedField (transformer = ImplicitCollectionMappingTransformer . class )
private List < Address > addresses;
// Getters and Setters...
}
public class UserDisplay {
private String name;
private List < AddressDisplay > addresses;
// Getters and Setters...
}