resolveType method

Future<String> resolveType(
  1. String type, {
  2. CancelToken? cancelToken,
})

Resolve MVR-named types inside type to their fully qualified type tags. Returns type unchanged if it contains no MVR names.

Implementation

Future<String> resolveType(String type, {CancelToken? cancelToken}) async {
  if (!_hasMvrName(type)) return type;
  final names = _mvrTypeRef
      .allMatches(type)
      .map((m) => m.group(0)!)
      .toSet()
      .toList();
  if (names.isEmpty) return type;

  final resolution = await _bulk(
    '/v1/struct-definition/bulk',
    'types',
    names,
    cancelToken: cancelToken,
  );
  // Replace longest names first so a name that is a prefix of another
  // (e.g. `@a/b::m::T` vs `@a/b::m::T2`) doesn't corrupt the longer one.
  names.sort((a, b) => b.length.compareTo(a.length));
  var result = type;
  for (final name in names) {
    final tag = (resolution[name] as Map?)?['type_tag'] as String?;
    if (tag == null) {
      throw StateError('MVR could not resolve type "$name"');
    }
    result = result.replaceAll(name, tag);
  }
  return result;
}