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
Mappingsinstance. Add operations withmapFromormapConstant.Declaration
Swift
required public init() -
Some
See moreMappingsoptions that are mapping a key from the payload to a destination in the result payload.Declaration
Swift
public class VariableOptions : MappingsBuilder -
Some
See moreMappingsoptions that are mapping a constant value to a destination in the result payload.Declaration
Swift
public class ConstantOptions : MappingsBuilder -
A
See moreConstantOptionsinstance 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.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) -> VariableOptionsParameters
keyThe source key from which to take the value to map.
destinationThe destination path to a variable in the data layer.
Return Value
A
VariableOptionsmapping operation builder. -
Creates a new mapping operation builder with the specified source and destination.
Declaration
Swift
@discardableResult func mapFrom(_ path: JSONObjectPath, to destination: JSONObjectPath) -> VariableOptionsParameters
pathThe source path from which to take the value to map.
destinationThe destination path to a variable in the data layer.
Return Value
A
VariableOptionsmapping operation builder. -
Creates a new mapping operation builder with the specified source and destination.
Declaration
Swift
@discardableResult func mapFrom(_ path: JSONObjectPath, to destination: String) -> VariableOptionsParameters
pathThe source path from which to take the value to map.
destinationThe destination key to a variable in the data layer.
Return Value
A
VariableOptionsmapping operation builder. -
Creates a new mapping operation builder with the specified source and destination.
Declaration
Swift
@discardableResult func mapFrom(_ key: String, to destination: String) -> VariableOptionsParameters
keyThe source key from which to take the value to map.
destinationThe destination key to a variable in the data layer.
Return Value
A
VariableOptionsmapping operation builder. -
Adds a mapping where the value to map is given by the constant
valueand will be mapped to the givendestinationlocated/stored at some configured level of nesting as defined by theJSONObjectPath.Declaration
Swift
@discardableResult func mapConstant(_ value: DataInput, to destination: JSONObjectPath) -> ConstantOptionsReturn Value
A
ConstantOptionsmapping operation builder. -
Adds a mapping where the value to map is given by the constant
valueand will be mapped to the givendestinationlocated/stored in the root of the data layer.Declaration
Swift
@discardableResult func mapConstant(_ value: DataInput, to destination: String) -> ConstantOptionsReturn Value
A
ConstantOptionsmapping operation builder. -
Adds a mapping where the value to map is given by the constant
nameand 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) -> CommandOptionsParameters
nameThe command name to map to the “command_name” property key.
Return Value
A
CommandOptionsmapping operation builder.
View on GitHub