getEntityProperties function

Map<String, EntityPropertyData> getEntityProperties(
  1. Type type, {
  2. ClassMirror? classMirror,
})

Implementation

Map<String, EntityPropertyData> getEntityProperties(Type type,
    {ClassMirror? classMirror}) {
  classMirror ??= reflectType(type);

  final metadata = classMirror.metadata.whereType<EntityMeta>().firstOrNull;

  final typeProps = classMirror.declarations.values
      .whereType<VariableMirror>()
      .fold<Map<String, EntityPropertyData>>({}, (prev, curr) {
    final propertyMeta = curr.metadata.whereType<EntityProperty>().firstOrNull;
    return prev
      ..[curr.simpleName] = EntityPropertyData(curr.simpleName,
          propertyMeta?.name ?? curr.simpleName, curr.reflectedType);
  });
  if (metadata == null || !metadata.timestamps) return typeProps;

  typeProps[metadata.createdAtColumn] ??=
      EntityPropertyData('createdAt', metadata.createdAtColumn, DateTime);

  typeProps[metadata.updatedAtColumn] ??=
      EntityPropertyData('updatedAt', metadata.updatedAtColumn, DateTime);

  return typeProps;
}