castMap method
Casts map
to this type (Map<K,V>
), resolving the casting for
K
and V
if there's a ClassReflection or EnumReflection
for them registered at ReflectionFactory.
Implementation
Map castMap(Map map, {bool nullable = false}) {
var keyType = isCollection
? (argumentType(0) ?? TypeInfo.tDynamic)
: TypeInfo.tDynamic;
var valueType = isCollection
? (argumentType(1) ?? TypeInfo.tDynamic)
: TypeInfo.tDynamic;
var reflectionFactory = ReflectionFactory();
var keyReflection =
reflectionFactory.getRegisterClassReflection(keyType.type) ??
reflectionFactory.getRegisterEnumReflection(keyType.type);
var valueReflection =
reflectionFactory.getRegisterClassReflection(valueType.type) ??
reflectionFactory.getRegisterEnumReflection(valueType.type);
if (keyReflection != null && valueReflection != null) {
var tMap = TypeInfo.fromMapType(
keyReflection.typeInfo, valueReflection.typeInfo);
if (valueReflection == keyReflection) {
return keyReflection.castMap(map, tMap, nullable: nullable) ?? map;
} else {
var mapKeysCast =
keyReflection.castMapKeys(map, tMap, nullable: nullable) ?? map;
var mapCast = valueReflection.castMapValues(mapKeysCast, this,
nullable: nullable) ??
map;
return mapCast;
}
} else if (keyReflection != null) {
return keyReflection.castMapKeys(map, this, nullable: nullable) ?? map;
} else if (valueReflection != null) {
return valueReflection.castMapValues(map, this, nullable: nullable) ??
map;
}
if (keyType.isValidGenericType && valueType.isValidGenericType) {
return keyType.callCasted(<K>() {
return valueType.callCasted(<V>() => map.cast<K, V>());
});
} else if (keyType.isValidGenericType) {
return keyType.callCasted(<K>() => map.cast<K, dynamic>());
} else if (valueType.isValidGenericType) {
return valueType.callCasted(<V>() => map.cast<dynamic, V>());
}
return map;
}