map<S, T> method
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;
}