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.

@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
}

We can use ShapeShift to map Address to AddressDisplay. But what we do if Address is a subclass? We use the ImplicitMappingTransformer.

@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
}

The ImplicitMappingTransformer uses the ShapeShift instance internally to map Address to AddressDisplay.

ImplicitCollectionMappingTransformer

ImplicitCollectionMappingTransformer has the same job as ImplicitMappingTransformer but for collections.

@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
}

Last updated