map<S, T> method

T? map<S, T>(
  1. S source, {
  2. MappingContext? context,
  3. Mapping<S, T>? mapping,
})

map a source object adn return the resulting target. S the source type T the target type source the source object context optional MappingContext in case of recursive maps.

Implementation

T? map<S,T>(S source, {MappingContext? context, Mapping<S,T>? mapping}) {
  if ( source == null)
    return null;

  mapping = mapping ?? getMapping<S,T>();

  context ??= MappingContext(mapper: this);

  dynamic target = context.mappedObject(source);

  var lazyCreate = false;
  if ( target == null) {
    lazyCreate = mapping.lazy;
    if (lazyCreate)
      target = context; // we need to set something....
    else {
      target = mapping.newInstance();

      context.remember(source, target);
    }
  }

  var state = mapping.setupContext(context);
  try {
    mapping.transformTarget(source, target, context);

    if ( lazyCreate ) {
      target = context.currentState!.result;

      context.remember(source, target);
    }
  }
  finally {
    state.restore(context);
  }

  // run finalizer

  for (Function finalizer in mapping.finalizer)
    finalizer(source, target);

  // done

  return target;
}