parseRelations static method

List<RelationMeta> parseRelations(
  1. List raw
)

Parses a raw list of relation inputs into a list of RelationMeta.

Supports multiple input formats:

  • Simple strings: 'posts', 'user:paginated', 'comments:page=1:perPage=10'
  • Nested relations: 'user.posts'
  • Complex objects: {'posts': {'paginate': true, 'page': 1, 'perPage': 10, 'with': ['comments']}}

Parameters:

  • raw: List of relation specifications (strings or maps)

Returns: List of parsed RelationMeta objects containing relation metadata

Implementation

static List<RelationMeta> parseRelations(List<dynamic> raw) {
  final result = <RelationMeta>[];

  for (final entry in raw) {
    if (entry is String) {
      final nestedSplit = entry.split('.');
      final mainPart = nestedSplit.first;
      final nested =
          nestedSplit.length > 1 ? [nestedSplit.sublist(1).join('.')] : [];

      final segments = mainPart.split(':');

      final key = segments.first;
      bool paginate = false;
      int? page;
      int? perPage;

      for (var segment in segments.skip(1)) {
        if (segment == 'paginated') {
          paginate = true;
        } else if (segment.startsWith('page=')) {
          page = int.tryParse(segment.replaceFirst('page=', ''));
        } else if (segment.startsWith('perPage=')) {
          perPage = int.tryParse(segment.replaceFirst('perPage=', ''));
        }
      }

      result.add(
        RelationMeta(
          key: key,
          paginate: paginate,
          page: page,
          perPage: perPage,
          nested: nested,
        ),
      );
    } else if (entry is Map<String, dynamic>) {
      for (final key in entry.keys) {
        final val = entry[key] as Map<String, dynamic>;
        result.add(
          RelationMeta(
            key: key,
            paginate: val['paginate'] ?? false,
            page: val['page'],
            perPage: val['perPage'],
            nested: val['with'] ?? [],
            query: val['query'],
          ),
        );
      }
    }
  }

  return result;
}