Mappings

open class Mappings

The Mappings builder is used to build up key/destination mappings that are applied on top of the full Dispatch payload before it is sent to a given Dispatcher.

Keys that are not mapped to are kept as they are, while a mapped destination replaces any existing value. The only exception is the root “command_name” key, where mapped values (constants or variables) are appended to any value already there, flattening arrays one level, so that multiple commands can be collected (e.g. an existing ["a"] with a mapped ["b", ["c"]] gives ["a", "b", ["c"]]). If a parent along the destination path holds a value of a different type (e.g. a string where an object is needed, or an object where an array is needed), it is replaced. Sources are always read from the original payload, so mappings don’t chain. Derived values should be computed with a dispatcher-scoped transformation, which runs before mappings.

Use the mapFrom method to supply the required source “key” and “destination” key, as well as any optional “path” entries required to access keys in nested object.

Using the following payload DataObject as an example (shown as JSON)

 {
      "source" : "value",
      "path": {
          "to" : [{
              "source": "nested value",
          }]
      }
 }

Simple usage for keys in the top level DataObject would look like so:

 mappings.mapFrom("source", to: "destination")

More complex versions requiring accessing keys that exist in nested objects and arrays would look like so:

 mappings.mapFrom(JSONPath["path"]["to"][0]["source"],
                  to: JSONPath["path"]["to"]["destination"])

The mapFrom method returns a VariableOptions that allows for setting optional properties relevant to a mapping like a ifValueEquals(:), to only perform the mapping if the value is equal to some specific string.

Use the mapConstant method to supply a constant “value” and “destination” key.

 mappings.mapConstant("value", to: "destination")

More complex versions requiring accessing keys that exist in nested objects would look like so:

 mappings.mapConstant(value, to: JSONPath["path"]["to"]["destination"])

The mapConstant method returns a ConstantOptions that allows for setting optional properties relevant to a mapping like a ifValueIn(:equals:), to only perform the mapping if a value at the given key is equal to the given target.

  • Creates an empty Mappings instance. Add operations with mapFrom or mapConstant.

    Declaration

    Swift

    required public init()
  • Some Mappings options that are mapping a key from the payload to a destination in the result payload.

    See more

    Declaration

    Swift

    public class VariableOptions : MappingsBuilder
  • Some Mappings options that are mapping a constant value to a destination in the result payload.

    See more

    Declaration

    Swift

    public class ConstantOptions : MappingsBuilder
  • A ConstantOptions instance that is mapping a command name string to the “command_name” property in the result payload. Intended to be used for so called Remote Command Dispatchers.

    See more

    Declaration

    Swift

    public class CommandOptions : ConstantOptions
  • Creates a new mapping operation builder with the specified source and destination.

    Declaration

    Swift

    @discardableResult
    func mapFrom(_ key: String, to destination: JSONObjectPath) -> VariableOptions

    Parameters

    key

    The source key from which to take the value to map.

    destination

    The destination path to a variable in the data layer.

    Return Value

    A VariableOptions mapping operation builder.

  • Creates a new mapping operation builder with the specified source and destination.

    Declaration

    Swift

    @discardableResult
    func mapFrom(_ path: JSONObjectPath, to destination: JSONObjectPath) -> VariableOptions

    Parameters

    path

    The source path from which to take the value to map.

    destination

    The destination path to a variable in the data layer.

    Return Value

    A VariableOptions mapping operation builder.

  • Creates a new mapping operation builder with the specified source and destination.

    Declaration

    Swift

    @discardableResult
    func mapFrom(_ path: JSONObjectPath, to destination: String) -> VariableOptions

    Parameters

    path

    The source path from which to take the value to map.

    destination

    The destination key to a variable in the data layer.

    Return Value

    A VariableOptions mapping operation builder.

  • Creates a new mapping operation builder with the specified source and destination.

    Declaration

    Swift

    @discardableResult
    func mapFrom(_ key: String, to destination: String) -> VariableOptions

    Parameters

    key

    The source key from which to take the value to map.

    destination

    The destination key to a variable in the data layer.

    Return Value

    A VariableOptions mapping operation builder.

  • Adds a mapping where the value to map is given by the constant value and will be mapped to the given destination located/stored at some configured level of nesting as defined by the JSONObjectPath.

    Declaration

    Swift

    @discardableResult
    func mapConstant(_ value: DataInput, to destination: JSONObjectPath) -> ConstantOptions

    Return Value

    A ConstantOptions mapping operation builder.

  • Adds a mapping where the value to map is given by the constant value and will be mapped to the given destination located/stored in the root of the data layer.

    Declaration

    Swift

    @discardableResult
    func mapConstant(_ value: DataInput, to destination: String) -> ConstantOptions

    Return Value

    A ConstantOptions mapping operation builder.

  • Adds a mapping where the value to map is given by the constant name and will be mapped to the “command_name” key located in the root of the data layer.

    Command names are always appended to any command already in “command_name”, whether it was in the original payload or mapped by a previous mapping, so multiple commands can be collected.

    Declaration

    Swift

    @discardableResult
    func mapCommand(_ name: String) -> CommandOptions

    Parameters

    name

    The command name to map to the “command_name” property key.

    Return Value

    A CommandOptions mapping operation builder.