applyProtocolReferences method

TypeDefinition applyProtocolReferences(
  1. List<SerializableModelDefinition> classDefinitions
)

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,
) {
  var modelDefinition = classDefinitions
      .where((c) => c.className == className)
      .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)
      ? classDefinitions
            .where((c) => c.className == className)
            .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 = classDefinitions
        .where((c) => c.className == className)
        .where((c) => c.isSharedModel)
        .firstOrNull;
  }
  bool isProjectModel =
      url == defaultModuleAlias || (url == null && modelDefinition != null);
  return TypeDefinition(
    className: className,
    nullable: nullable,
    customClass: customClass,
    dartType: dartType,
    projectModelDefinition: isModuleType
        ? null
        : modelDefinition ?? sharedModelDefinition,
    generics: generics
        .map((e) => e.applyProtocolReferences(classDefinitions))
        .toList(),
    enumDefinition: enumDefinition,
    url: isProjectModel ? defaultModuleAlias : url,
    recordFieldName: recordFieldName,
    vectorDimension: vectorDimension,
  );
}