mirror of https://github.com/AMT-Cheif/drift.git
Add type checks to parent class option
This commit is contained in:
parent
4c2e74d5e0
commit
46ca0462bf
|
@ -76,7 +76,8 @@ class TableParser {
|
||||||
|
|
||||||
if (dataClassName != null) {
|
if (dataClassName != null) {
|
||||||
name = dataClassName.getField('name')!.toStringValue()!;
|
name = dataClassName.getField('name')!.toStringValue()!;
|
||||||
customParentClass = parseCustomParentClass(dataClassName, element, base);
|
customParentClass =
|
||||||
|
parseCustomParentClass(name, dataClassName, element, base);
|
||||||
} else {
|
} else {
|
||||||
name = dataClassNameForClassName(element.name);
|
name = dataClassNameForClassName(element.name);
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@ class ViewParser {
|
||||||
_DataClassInformation _readDataClassInformation(
|
_DataClassInformation _readDataClassInformation(
|
||||||
List<MoorColumn> columns, ClassElement element) {
|
List<MoorColumn> columns, ClassElement element) {
|
||||||
DartObject? useRowClass;
|
DartObject? useRowClass;
|
||||||
String? dataClassName;
|
DartObject? driftView;
|
||||||
String? customParentClass;
|
String? customParentClass;
|
||||||
|
|
||||||
for (final annotation in element.metadata) {
|
for (final annotation in element.metadata) {
|
||||||
|
@ -44,14 +44,13 @@ class ViewParser {
|
||||||
final annotationClass = computed!.type!.element!.name;
|
final annotationClass = computed!.type!.element!.name;
|
||||||
|
|
||||||
if (annotationClass == 'DriftView') {
|
if (annotationClass == 'DriftView') {
|
||||||
dataClassName = computed.getField('dataClassName')?.toStringValue();
|
driftView = computed;
|
||||||
customParentClass = parseCustomParentClass(computed, element, base);
|
|
||||||
} else if (annotationClass == 'UseRowClass') {
|
} else if (annotationClass == 'UseRowClass') {
|
||||||
useRowClass = computed;
|
useRowClass = computed;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dataClassName != null && useRowClass != null) {
|
if (driftView != null && useRowClass != null) {
|
||||||
base.step.reportError(ErrorInDartCode(
|
base.step.reportError(ErrorInDartCode(
|
||||||
message: "A table can't be annotated with both @DataClassName and "
|
message: "A table can't be annotated with both @DataClassName and "
|
||||||
'@UseRowClass',
|
'@UseRowClass',
|
||||||
|
@ -63,7 +62,15 @@ class ViewParser {
|
||||||
String? constructorInExistingClass;
|
String? constructorInExistingClass;
|
||||||
bool? generateInsertable;
|
bool? generateInsertable;
|
||||||
|
|
||||||
var name = dataClassName ?? dataClassNameForClassName(element.name);
|
var name = dataClassNameForClassName(element.name);
|
||||||
|
|
||||||
|
if (driftView != null) {
|
||||||
|
final dataClassName =
|
||||||
|
driftView.getField('dataClassName')?.toStringValue();
|
||||||
|
name = dataClassName ?? dataClassNameForClassName(element.name);
|
||||||
|
customParentClass =
|
||||||
|
parseCustomParentClass(name, driftView, element, base);
|
||||||
|
}
|
||||||
|
|
||||||
if (useRowClass != null) {
|
if (useRowClass != null) {
|
||||||
final type = useRowClass.getField('type')!.toTypeValue();
|
final type = useRowClass.getField('type')!.toTypeValue();
|
||||||
|
|
|
@ -3,6 +3,7 @@ import 'package:analyzer/dart/element/element.dart';
|
||||||
import 'package:analyzer/dart/element/type.dart';
|
import 'package:analyzer/dart/element/type.dart';
|
||||||
import 'package:drift_dev/src/analyzer/dart/parser.dart';
|
import 'package:drift_dev/src/analyzer/dart/parser.dart';
|
||||||
import 'package:drift_dev/src/analyzer/errors.dart';
|
import 'package:drift_dev/src/analyzer/errors.dart';
|
||||||
|
import 'package:drift_dev/src/utils/type_utils.dart';
|
||||||
|
|
||||||
String dataClassNameForClassName(String tableName) {
|
String dataClassNameForClassName(String tableName) {
|
||||||
// This implementation is very primitive at the moment. The basic idea is
|
// This implementation is very primitive at the moment. The basic idea is
|
||||||
|
@ -21,19 +22,53 @@ String dataClassNameForClassName(String tableName) {
|
||||||
return '${tableName}Data';
|
return '${tableName}Data';
|
||||||
}
|
}
|
||||||
|
|
||||||
String? parseCustomParentClass(
|
String? parseCustomParentClass(String dartTypeName, DartObject dataClassName,
|
||||||
DartObject dataClassName, ClassElement element, MoorDartParser base) {
|
ClassElement element, MoorDartParser base) {
|
||||||
final extending = dataClassName.getField('extending');
|
final extending = dataClassName.getField('extending');
|
||||||
if (extending != null && !extending.isNull) {
|
if (extending != null && !extending.isNull) {
|
||||||
final extendingType = extending.toTypeValue();
|
final extendingType = extending.toTypeValue();
|
||||||
if (extendingType is InterfaceType) {
|
if (extendingType is InterfaceType) {
|
||||||
|
final superType = extendingType.allSupertypes
|
||||||
|
.any((type) => isFromMoor(type) && type.element.name == 'DataClass');
|
||||||
|
if (!superType) {
|
||||||
|
base.step.reportError(
|
||||||
|
ErrorInDartCode(
|
||||||
|
message: 'Parameter `extending` in @DataClassName must be subtype '
|
||||||
|
'of DataClass',
|
||||||
|
affectedElement: element,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (extendingType.typeArguments.length > 1) {
|
||||||
|
base.step.reportError(
|
||||||
|
ErrorInDartCode(
|
||||||
|
message: 'Parameter `extending` in @DataClassName must have zero or'
|
||||||
|
' one type parameter',
|
||||||
|
affectedElement: element,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
final className = extendingType.element.name;
|
final className = extendingType.element.name;
|
||||||
if (extendingType.typeArguments.length == 1) {
|
if (extendingType.typeArguments.length == 1) {
|
||||||
final genericType = extendingType.typeArguments[0].element?.name;
|
final genericType = extendingType.typeArguments[0].element?.name;
|
||||||
if (genericType == 'dynamic') {
|
if (genericType == 'Object') {
|
||||||
return '$className<dynamic>';
|
return '$className<$dartTypeName>';
|
||||||
|
} else {
|
||||||
|
base.step.reportError(
|
||||||
|
ErrorInDartCode(
|
||||||
|
message: 'Parameter `extending` in @DataClassName can only have '
|
||||||
|
'`Object` as type parameter: `YourType<Object>`',
|
||||||
|
affectedElement: element,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return className;
|
return className;
|
||||||
} else {
|
} else {
|
||||||
base.step.reportError(
|
base.step.reportError(
|
||||||
|
|
|
@ -24,9 +24,7 @@ class DataClassWriter {
|
||||||
: 'driftRuntimeOptions';
|
: 'driftRuntimeOptions';
|
||||||
|
|
||||||
void write() {
|
void write() {
|
||||||
final customParent = table.customParentClass
|
final parentClass = table.customParentClass ?? 'DataClass';
|
||||||
?.replaceFirst('<dynamic>', '<${table.dartTypeName}>');
|
|
||||||
final parentClass = customParent ?? 'DataClass';
|
|
||||||
_buffer.write('class ${table.dartTypeName} extends $parentClass ');
|
_buffer.write('class ${table.dartTypeName} extends $parentClass ');
|
||||||
|
|
||||||
if (isInsertable) {
|
if (isInsertable) {
|
||||||
|
|
Loading…
Reference in New Issue