buildCollectionMapper function
Implementation
String buildCollectionMapper(
Iterable<List<String>> typeData,
TTypeMappers mappers,
) {
var output = '#x0';
// Loop through type data elements.
for (final element in typeData) {
final collectionEvent = CollectionMapperEvent().._ltypes = element.skip(2);
final pLength = collectionEvent._ltypes.length;
collectionEvent
.._lhashes = Iterable.generate(pLength, (n) => n).map((n) => '#p$n')
.._lparams = Iterable.generate(pLength, (n) => n).map((n) => 'p$n')
.._largs = Iterable.generate(pLength, (n) => n).map((n) => 'p$n')
.._type = element[1];
final argIdMatch = RegExp(r'#x(\d+)').firstMatch(output);
collectionEvent._nameIndex =
argIdMatch != null && argIdMatch.groupCount > 0 //
? int.tryParse(argIdMatch.group(1)!)
: null;
final xHash = '#x${collectionEvent._nameIndex}';
final formula = _buildMapper(collectionEvent, mappers);
if (formula != null) {
output = output.replaceFirst(xHash, formula);
} else {
final type = collectionEvent.type;
Log.printRed(
'buildCollectionMapper: no mapper matched collection type `$type`. '
'Add a mapper for it or the generated code will not compile.',
);
assert(false, 'Collection type-mapper not found for `$type`!');
output = output.replaceFirst(xHash, _missingMapperSentinel(type));
}
// Loop through object types.
for (var n = 0; n < pLength; n++) {
final objectEvent = ObjectMapperEvent()
.._nameIndex = n
.._type = collectionEvent._ltypes.elementAt(n);
final pHash = '#p$n';
// If the object type is the next type data element, replace the slot
// with `#x<n>` so the next iteration's collection mapper fills it.
// Guarded against empty type strings — `type[0]` would otherwise throw
// RangeError.
final objectType = objectEvent.type;
if (objectType != null && objectType.isNotEmpty && objectType[0] == '*') {
final xHash = '#x$n';
output = output.replaceFirst(pHash, xHash);
}
// If the object type is something else like num, int, double, bool or
// String.
else {
final formula = _buildMapper(objectEvent, mappers);
if (formula != null) {
output = output.replaceFirst(pHash, formula);
} else {
Log.printRed(
'buildCollectionMapper: no mapper matched object type '
'`$objectType`. Add a mapper for it or the generated code will '
'not compile.',
);
assert(false, 'Object type-mapper not found for `$objectType`!');
output = output.replaceFirst(
pHash,
_missingMapperSentinel(objectType),
);
}
}
}
}
return output;
}