applyProtocolReferences method
Applies protocol references. This makes the protocol: prefix optional. First, the protocol definition is parsed, then it's check for the protocol: prefix in types. Whenever no url is set and user specified a class/enum with the same symbol name it defaults to the protocol: prefix.
Implementation
TypeDefinition applyProtocolReferences(
List<SerializableModelDefinition> classDefinitions,
) {
// Only the models sharing this type's name can match any of the lookups
// below, so narrow to those once instead of walking the whole list four
// times. Order within the group is the list's own, keeping `firstOrNull`
// selecting what it selected before.
var candidates =
modelsByClassName(classDefinitions)[className] ??
const <SerializableModelDefinition>[];
var modelDefinition = candidates
.where((c) => c.type.moduleAlias == defaultModuleAlias)
.firstOrNull;
// Resolve shared-package model when url is that package's module alias
var sharedModelDefinition = (url != null && url != defaultModuleAlias)
? candidates.where((c) => c.type.moduleAlias == url).firstOrNull
: null;
// Resolve shared-package model/enum when type is protocol-scoped but only
// defined in a shared package (e.g. field type SharedModel or SharedEnum)
if (modelDefinition == null &&
sharedModelDefinition == null &&
(url == defaultModuleAlias || url == null)) {
sharedModelDefinition = candidates
.where((c) => c.isSharedModel)
.firstOrNull;
}
bool isProjectModel =
url == defaultModuleAlias || (url == null && modelDefinition != null);
var resolvedModuleAlias = isProjectModel
? defaultModuleAlias
: sharedModelDefinition?.type.moduleAlias ?? moduleAlias;
var resolvedModel = candidates
.where((model) => model.type.moduleAlias == resolvedModuleAlias)
.firstOrNull;
return TypeDefinition(
className: className,
nullable: nullable,
customClass: customClass,
dartType: dartType,
projectModelDefinition: isModuleType
? null
: modelDefinition ?? sharedModelDefinition,
classDefinition: switch (resolvedModel) {
ClassDefinition() => resolvedModel,
// Future-call analysis resolves project models a second time without
// their module definitions. Keep the previously resolved module class.
null when isModuleType => classDefinition,
_ => null,
},
generics: generics
.map((e) => e.applyProtocolReferences(classDefinitions))
.toList(),
serializationDataType: serializationDataType,
enumDefinition: enumDefinition,
url: isProjectModel
? defaultModuleAlias
: sharedModelDefinition?.type.moduleAlias ?? url,
recordFieldName: recordFieldName,
vectorDimension: vectorDimension,
);
}