Customizing the Default Mapping Code

Default Mapping Code

When a mapping step is configured, Data Hub generates code that extracts property values from the sources specified in the mapping and copies them to the associated entity properties. You can customize this generated code.

All mapping files are stored locally in the directory your-project-root/mappings/. The mapping configuration file is stored as a JSON file in your-project-root/mappings/yourflowname-yourmappingstepname/yourflowname-yourmappingstepname-version.mapping.json. For example, the first version of a mapping called ProductMapping is stored as your-project-root/mappings/MyFlow-ProductMapping/MyFlow-ProductMapping-0.mapping.json.

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.