diff --git a/lib/src/resource/manager.dart b/lib/src/resource/manager.dart index f42e59b140..3632575066 100644 --- a/lib/src/resource/manager.dart +++ b/lib/src/resource/manager.dart @@ -1,3 +1,5 @@ +import 'package:protobuf/protobuf.dart'; + import '../gen/common/v1/common.pb.dart'; import 'base.dart'; @@ -5,17 +7,19 @@ import 'base.dart'; class ResourceManager { /// The available resources Map resources = {}; - final Map> _shortToLongName = {}; + final Map> _resourceNamesWithoutRemotes = {}; /// Register a new [Resource] with the manager. void register(ResourceName name, Resource resource) { if (resources.containsKey(name)) { throw Exception('Duplicate registration of resource in manager'); } - final shortName = name.name.split(':').last; - final names = _shortToLongName[shortName] ?? []; + final rnWithoutRemote = name.deepCopy() + ..remotePath.clear() + ..name = name.localName; + final names = _resourceNamesWithoutRemotes[rnWithoutRemote] ?? []; names.add(name); - _shortToLongName[shortName] = names; + _resourceNamesWithoutRemotes[rnWithoutRemote] = names; resources[name] = resource; } @@ -25,22 +29,18 @@ class ResourceManager { if (resources.containsKey(name)) { resource = resources[name]; } else { - final resourcesWithoutRemotes = resources.map((rn, res) { - final rnWithoutRemote = rn - ..remotePath.clear() - ..name = rn.localName; - return MapEntry(rnWithoutRemote, res); - }); - resource = resourcesWithoutRemotes[name]; + final resourceNames = _resourceNamesWithoutRemotes[name] ?? []; + // If multiple name-without-remotes map to this resource name, + // that means there are multiple remote resources with this same short name. + // Without any means to disambiguate, we should not select any. + if (resourceNames.length > 1) { + throw Exception('Multiple remote resources with found with the name ${name.name}: $resourceNames'); + } + if (resourceNames.length == 1) { + resource = resources[resourceNames.first]; + } } if (resource == null) throw Exception('Resource not found in manager'); return resource as T; } - - /// Get a resource by its name only - T getResourceByName(String name) { - final names = _shortToLongName[name] ?? []; - if (names.isEmpty) throw Exception('Resource not found in manager'); - return getResource(names.first); - } }