mirror of https://github.com/AMT-Cheif/drift.git
Prepare for separating ClassElement, EnumElement, and MixinElement. (#2025)
* Prepare for separating ClassElement, EnumElement, and MixinElement. * Improve error UX when casting to ClassElement Co-authored-by: Simon Binder <oss@simonbinder.eu>
This commit is contained in:
parent
98cb4be7cc
commit
b399db78e3
|
@ -23,7 +23,7 @@ Future<FoundDartClass?> findDartClass(
|
||||||
}
|
}
|
||||||
|
|
||||||
final foundElement = library.exportNamespace.get(identifier);
|
final foundElement = library.exportNamespace.get(identifier);
|
||||||
if (foundElement is ClassElement) {
|
if (foundElement is InterfaceElement) {
|
||||||
return FoundDartClass(foundElement, null);
|
return FoundDartClass(foundElement, null);
|
||||||
} else if (foundElement is TypeAliasElement) {
|
} else if (foundElement is TypeAliasElement) {
|
||||||
final innerType = foundElement.aliasedType;
|
final innerType = foundElement.aliasedType;
|
||||||
|
|
|
@ -22,13 +22,13 @@ class HelperLibrary {
|
||||||
/// Returns `null` if [type] is not a subtype of `TypeConverter`.
|
/// Returns `null` if [type] is not a subtype of `TypeConverter`.
|
||||||
InterfaceType? asTypeConverter(DartType type) {
|
InterfaceType? asTypeConverter(DartType type) {
|
||||||
final converter =
|
final converter =
|
||||||
helperLibrary.exportNamespace.get('TypeConverter') as ClassElement;
|
helperLibrary.exportNamespace.get('TypeConverter') as InterfaceElement;
|
||||||
return type.asInstanceOf(converter);
|
return type.asInstanceOf(converter);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isJsonAwareTypeConverter(DartType? type, LibraryElement context) {
|
bool isJsonAwareTypeConverter(DartType? type, LibraryElement context) {
|
||||||
final jsonMixin =
|
final jsonMixin = helperLibrary.exportNamespace.get('JsonTypeConverter')
|
||||||
helperLibrary.exportNamespace.get('JsonTypeConverter') as ClassElement;
|
as InterfaceElement;
|
||||||
final jsonConverterType = jsonMixin.instantiate(
|
final jsonConverterType = jsonMixin.instantiate(
|
||||||
typeArguments: [
|
typeArguments: [
|
||||||
context.typeProvider.dynamicType,
|
context.typeProvider.dynamicType,
|
||||||
|
|
|
@ -122,15 +122,18 @@ class ParseDartStep extends Step {
|
||||||
Future<List<DriftTable>> parseTables(
|
Future<List<DriftTable>> parseTables(
|
||||||
Iterable<DartType> types, Element initializedBy) {
|
Iterable<DartType> types, Element initializedBy) {
|
||||||
return Future.wait(types.map((type) {
|
return Future.wait(types.map((type) {
|
||||||
if (!_tableTypeChecker.isAssignableFromType(type)) {
|
final element = type is InterfaceType ? type.element2 : null;
|
||||||
|
|
||||||
|
if (!_tableTypeChecker.isAssignableFromType(type) ||
|
||||||
|
element is! ClassElement) {
|
||||||
reportError(ErrorInDartCode(
|
reportError(ErrorInDartCode(
|
||||||
severity: Severity.criticalError,
|
severity: Severity.criticalError,
|
||||||
message: 'The type $type is not a moor table',
|
message: 'The type $type is not a drift table class.',
|
||||||
affectedElement: initializedBy,
|
affectedElement: initializedBy,
|
||||||
));
|
));
|
||||||
return Future.value(null);
|
return Future.value(null);
|
||||||
} else {
|
} else {
|
||||||
return _parseTable((type as InterfaceType).element2 as ClassElement);
|
return _parseTable(element);
|
||||||
}
|
}
|
||||||
})).then((list) {
|
})).then((list) {
|
||||||
// only keep tables that were resolved successfully
|
// only keep tables that were resolved successfully
|
||||||
|
@ -145,16 +148,18 @@ class ParseDartStep extends Step {
|
||||||
Future<List<MoorView>> parseViews(Iterable<DartType> types,
|
Future<List<MoorView>> parseViews(Iterable<DartType> types,
|
||||||
Element initializedBy, List<DriftTable> tables) {
|
Element initializedBy, List<DriftTable> tables) {
|
||||||
return Future.wait(types.map((type) {
|
return Future.wait(types.map((type) {
|
||||||
if (!_viewTypeChecker.isAssignableFromType(type)) {
|
final element = type is InterfaceType ? type.element2 : null;
|
||||||
|
|
||||||
|
if (!_viewTypeChecker.isAssignableFromType(type) ||
|
||||||
|
element is! ClassElement) {
|
||||||
reportError(ErrorInDartCode(
|
reportError(ErrorInDartCode(
|
||||||
severity: Severity.criticalError,
|
severity: Severity.criticalError,
|
||||||
message: 'The type $type is not a drift view',
|
message: 'The type $type is not a drift view class.',
|
||||||
affectedElement: initializedBy,
|
affectedElement: initializedBy,
|
||||||
));
|
));
|
||||||
return Future.value(null);
|
return Future.value(null);
|
||||||
} else {
|
} else {
|
||||||
return _parseView(
|
return _parseView(element, tables);
|
||||||
(type as InterfaceType).element2 as ClassElement, tables);
|
|
||||||
}
|
}
|
||||||
})).then((list) {
|
})).then((list) {
|
||||||
// only keep tables that were resolved successfully
|
// only keep tables that were resolved successfully
|
||||||
|
|
Loading…
Reference in New Issue