mapProperties function

Map<String, Object?> mapProperties(
  1. Map<String, dynamic>? properties,
  2. Map<String, PropertyMapper> mappings
)

Implementation

Map<String, Object?> mapProperties(
    Map<String, dynamic>? properties, Map<String, PropertyMapper> mappings) {
  final Map<String, Object?> output = {};

  if (properties == null) {
    return {};
  }

  for (final entry in properties.entries) {
    final sourceKey = entry.key;
    final sourceValue = properties[sourceKey];
    if (mappings.containsKey(sourceKey)) {
      final mapping = mappings[sourceKey]!;

      output[mapping.targetKey] = (mapping.fromJson != null)
          ? mapping.fromJson!(sourceValue)
          : sourceValue;
    } else {
      output[sourceKey] = sourceValue;
    }
  }

  return output;
}