Mapper

Field mapper for transforming data between source and target APIs.

class apilinker.core.mapper.FieldMapper[source]

Bases: object

Maps fields between source and target APIs, including transformations.

This class handles the mapping of fields from source to target format, including nested fields, transformations, and filtering.

add_mapping(source: str, target: str, fields: List[Dict[str, Any]]) None[source]

Add a field mapping between source and target endpoints.

Parameters:
  • source – Source endpoint name

  • target – Target endpoint name

  • fields – List of field mappings (dicts with source, target, and optional transform)

apply_transform(value: Any, transform: str | List[str] | Dict[str, Any], params: Dict[str, Any] | None = None) Any[source]

Apply a transformation to a value.

Parameters:
  • value – The value to transform

  • transform – The transformation to apply (string name or list of transforms)

  • params – Optional parameters for the transformer

Returns:

Transformed value

get_first_mapping() Dict[str, Any] | None[source]

Get the first mapping if any exists.

get_mappings() List[Dict[str, Any]][source]

Get all defined mappings.

get_value_from_path(data: Dict[str, Any], path: str) Any[source]

Extract a value from nested dictionary using dot notation.

Parameters:
  • data – Source data dictionary

  • path – Path to the value using dot notation (e.g., “user.address.city”)

Returns:

The value at the specified path or None if not found

load_custom_transformer(module_path: str, function_name: str, alias: str | None = None) None[source]

Load a custom transformer function from a Python module.

Parameters:
  • module_path – Import path for the module

  • function_name – Name of the function in the module

  • alias – Optional alternative name to register the transformer under

map_data(source_endpoint: str, target_endpoint: str, data: Dict[str, Any] | List[Dict[str, Any]]) Dict[str, Any] | List[Dict[str, Any]][source]

Map data from source format to target format.

Parameters:
  • source_endpoint – Source endpoint name

  • target_endpoint – Target endpoint name

  • data – Data to map

Returns:

Mapped data in target format

map_fields(item: Dict[str, Any], fields: List[Dict[str, Any]]) Dict[str, Any][source]

Apply a list of field mappings to a single item.

This is the item-level counterpart to map_data and reuses the same field mapping semantics for callers that need projection without registering a source/target mapping pair first.

register_transformer(name: str, func: Callable) None[source]

Register a custom transformation function.

Parameters:
  • name – Name of the transformer

  • func – Function that performs the transformation

set_value_at_path(data: Dict[str, Any], path: str, value: Any) None[source]

Set a value in a nested dictionary using dot notation.

Parameters:
  • data – Target data dictionary

  • path – Path where to set the value using dot notation

  • value – Value to set

transform(value: Any, transformer: str | Dict[str, Any]) Any[source]

Convenience wrapper to apply a single transformer to a value.

Parameters:
  • value – Input value

  • transformer – Transformer name or transformer dict {name, params}

Returns:

Transformed value