Skip to content

Custom Transformations

Anton edited this page Jul 16, 2017 · 4 revisions

To perform transformations of a single or multiple values, JSONModelKit provides the ability to map multiple values from a dictionary response, and process them using the JMTransformerProtocol. Currently all transformed output properties must be optional, and the script will error out with a description in the build log.

public protocol JMTransformerProtocol {
    func transformValues(_ inputValues : Dictionary<String, Any>?) -> Any?
}

Transformations are great approach, especially for Date transforms by reusing the NSDateFormatter - and possibly creating attributed strings by reusing NSMutableParagraphStyles. Let's observe a response with a simple user object.

Response Dictionary

{
    "user_id"		: 9223123456754776000,
    "first_name"	: "John",
    "last_name"		: "Doe"
}

JMTransformerProtocol Implementation

Assuming the need to store the user's full name as a single property, the transformer implementation below takes in a user's first name and last name as a parsed dictionary of strings keyed according to the property mapping defined and transforms them into a single full name property value before being returned and assigned.

let firstNameKey    = "first_name"
let lastNameKey     = "last_name"

public class FullNameValueTransformer : JMTransformerProtocol {

    public func transformValues(inputValues : Dictionary<String, Any>?) -> Any? {
        var fullNameString : String = ""

        if let componentDictionary = inputValues as? Dictionary<String, String> {

            if let firstName = componentDictionary[firstNameKey] {
                fullNameString += firstName
            }

            if fullNameString.isEmpty == false { fullNameString += " " }

            if let lastName = componentDictionary[lastNameKey] {
                fullNameString += lastName
            }
        }

        if fullNameString.isEmpty { return nil }

        return fullNameString
    }
}

Mapping

To implement the transformer as part of the model mapping, observe how the key property in the mapping has become an array and takes in multiple values to transform into a fullName property. To use the custom transformer created above, add the transformer key to the property mapping defining which transformer class to use.

{
    "uuid" : { ... },
    "fullName" : {
	"transformer" : "FullNameValueTransformer",
        "key" : [
	    "first_name",
	    "last_name"
	]
    }
}

Note: The the keys defined in the property mapping correspond to the keys in the dictionary of values passed to the public func transformValues(inputValues : Dictionary<String, Any>?) -> Any? method defined by the protocol.

Compound Value Transformer

Currently the only packaged transformer shipped with the framework is the JSONModelKitCompoundValueTransformer which takes in a dictionary of values per JMTransformerProtocol, and creates a compound output value from the values passed into it.

There is potential for more transformers to be added in future releases.