getGenericType method

GenericType getGenericType(
  1. TypeInfo type
)

Retrieves the appropriate generic type for the given type information.

First checks custom types, then falls back to default user types.

@param type The type information to find a generic type for. @returns The GenericType that matches the specified type. @throws JsonSerializerException if the type is not registered.

Implementation

GenericType getGenericType(TypeInfo type) {
  final genericType = types
          .where((userType) => userType.name == type.name)
          .firstOrNull ??
      defaultUserTypes
          .where((userType) => userType.name == type.name)
          .firstOrNull;

  if (genericType == null) {
    final registeredTypes = [...types, ...defaultUserTypes]
        .map((userType) => userType.name)
        .toList()
        .join(', ');

    throw JsonSerializerException(
      'Type "${type.name}" is not registered.\n'
      'You need to register this type using:\n'
      '  - UserType<${type.name}>() for custom types\n'
      '  - EnumType<${type.name}>() for enums\n'
      'Currently registered types: ${registeredTypes.isEmpty ? "(none)" : registeredTypes}',
    );
  }

  return genericType;
}