Customizing the Mapping Code

Mapping Definition Files

See Mapping Definition

Customizing Mapping Code

The mapping code extracts property values from the source dataset and copies them to the associated entity properties. When a mapping step is configured in QuickStart, Data Hub generates this code automatically and you can customize it. You can also create your own mapping code without using the UI.

The default code for an entity type named T always includes a function named extractInstanceT, based on your mapping. When the mapping step is executed, Data Hub calls this function to update the values of the entity properties in the envelope of each record. You can add custom code to this function to transform those values.

The extractInstanceT function contains a variable declaration for each entity property. This declaration extracts the value of a source property SP and assigns it to an entity property EP.

  let EPName = !fn.empty(source.xpath(pathToSP))
              ? EPType(fn.head(source.xpath(pathToSP)))
              : null;

If the source data does not include the targeted property, then the entity property is initialized to null.

To customize the handling of a property, you can usually set this variable without changing the rest of the code.

Data Hub generates default mapping code for any entity property not specified in the mapping, if the following conditions are true.

  • The source property has the same name as the entity property.
  • The source property value can be cast directly to the entity property data type.
  • The source property can be accessed with the XPath expression /_propName_.

Example: Property Name Change

If the source property is named SKU and the entity property is named sku, the default extractInstanceT function contains the following variable declaration.

  let sku = !fn.empty(source.xpath('//SKU'))
           ? xs.string(fn.head(source.xpath('//SKU')))
           : null;

Example: Simple Typecasting

If the source property (price) is a string containing numerals and the entity property (price) is decimal, the default extractInstanceT function contains the following variable declaration, which casts the source string to a decimal.

  let price = !fn.empty(source.xpath('//price'))
             ? xs.decimal(fn.head(source.xpath('//price')))
             : null;

If the source property might contain other characters besides numerals, this declaration must be customized.