castIterable method

Iterable castIterable(
  1. Iterable itr, {
  2. bool nullable = false,
})

Casts itr to this type (Iterable<T>) if a ClassReflection or EnumReflection for T is registered at ReflectionFactory.

Implementation

Iterable castIterable(Iterable itr, {bool nullable = false}) {
  var mainType = isCollection ? (argumentType(0) ?? TypeInfo.tDynamic) : this;

  var reflectionFactory = ReflectionFactory();

  var reflection =
      reflectionFactory.getRegisterClassReflection(mainType.type) ??
          reflectionFactory.getRegisterEnumReflection(mainType.type);

  if (reflection != null) {
    return reflection.castIterable(itr, mainType.type, nullable: nullable) ??
        itr;
  }

  if (mainType.isValidGenericType) {
    return mainType.callCasted(<E>() => itr.cast<E>());
  } else {
    return itr;
  }
}