Rename to @CustomRowClass

This commit is contained in:
Simon Binder 2021-04-09 23:33:58 +02:00
parent 6682c1f909
commit 6bbff77271
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
4 changed files with 114 additions and 25 deletions

View File

@ -137,7 +137,7 @@ class DataClassName {
/// An annotation specifying an existing class to be used as a data class.
@Target({TargetKind.classType})
@experimental
class UseDataClass {
class UseRowClass {
/// The existing class
///
/// This type must refer to an existing class. All other types, like functions
@ -147,6 +147,6 @@ class UseDataClass {
/// Customize the class used by moor to hold an instance of an annotated
/// table.
///
/// For details, see the overall documentation on [UseDataClass].
const UseDataClass(this.type);
/// For details, see the overall documentation on [UseRowClass].
const UseRowClass(this.type);
}

View File

@ -58,7 +58,7 @@ class SharedTodos extends Table {
const _uuid = Uuid();
@UseDataClass(CustomRowClass)
@UseRowClass(CustomRowClass)
class TableWithoutPK extends Table {
IntColumn get notReallyAnId => integer()();
RealColumn get someFloat => real()();

View File

@ -1070,7 +1070,105 @@ class $SharedTodosTable extends SharedTodos
}
}
class TableWithoutPKCompanion extends UpdateCompanion<CustomRowClass> {
class TableWithoutPKData extends DataClass
implements Insertable<TableWithoutPKData> {
final int notReallyAnId;
final double someFloat;
final MyCustomObject custom;
TableWithoutPKData(
{required this.notReallyAnId,
required this.someFloat,
required this.custom});
factory TableWithoutPKData.fromData(
Map<String, dynamic> data, GeneratedDatabase db,
{String? prefix}) {
final effectivePrefix = prefix ?? '';
final intType = db.typeSystem.forDartType<int>();
final doubleType = db.typeSystem.forDartType<double>();
final stringType = db.typeSystem.forDartType<String>();
return TableWithoutPKData(
notReallyAnId: intType
.mapFromDatabaseResponse(data['${effectivePrefix}not_really_an_id'])!,
someFloat: doubleType
.mapFromDatabaseResponse(data['${effectivePrefix}some_float'])!,
custom: $TableWithoutPKTable.$converter0.mapToDart(stringType
.mapFromDatabaseResponse(data['${effectivePrefix}custom']))!,
);
}
@override
Map<String, Expression> toColumns(bool nullToAbsent) {
final map = <String, Expression>{};
map['not_really_an_id'] = Variable<int>(notReallyAnId);
map['some_float'] = Variable<double>(someFloat);
{
final converter = $TableWithoutPKTable.$converter0;
map['custom'] = Variable<String>(converter.mapToSql(custom)!);
}
return map;
}
TableWithoutPKCompanion toCompanion(bool nullToAbsent) {
return TableWithoutPKCompanion(
notReallyAnId: Value(notReallyAnId),
someFloat: Value(someFloat),
custom: Value(custom),
);
}
factory TableWithoutPKData.fromJson(Map<String, dynamic> json,
{ValueSerializer? serializer}) {
serializer ??= moorRuntimeOptions.defaultSerializer;
return TableWithoutPKData(
notReallyAnId: serializer.fromJson<int>(json['notReallyAnId']),
someFloat: serializer.fromJson<double>(json['someFloat']),
custom: serializer.fromJson<MyCustomObject>(json['custom']),
);
}
factory TableWithoutPKData.fromJsonString(String encodedJson,
{ValueSerializer? serializer}) =>
TableWithoutPKData.fromJson(
DataClass.parseJson(encodedJson) as Map<String, dynamic>,
serializer: serializer);
@override
Map<String, dynamic> toJson({ValueSerializer? serializer}) {
serializer ??= moorRuntimeOptions.defaultSerializer;
return <String, dynamic>{
'notReallyAnId': serializer.toJson<int>(notReallyAnId),
'someFloat': serializer.toJson<double>(someFloat),
'custom': serializer.toJson<MyCustomObject>(custom),
};
}
TableWithoutPKData copyWith(
{int? notReallyAnId, double? someFloat, MyCustomObject? custom}) =>
TableWithoutPKData(
notReallyAnId: notReallyAnId ?? this.notReallyAnId,
someFloat: someFloat ?? this.someFloat,
custom: custom ?? this.custom,
);
@override
String toString() {
return (StringBuffer('TableWithoutPKData(')
..write('notReallyAnId: $notReallyAnId, ')
..write('someFloat: $someFloat, ')
..write('custom: $custom')
..write(')'))
.toString();
}
@override
int get hashCode => $mrjf($mrjc(
notReallyAnId.hashCode, $mrjc(someFloat.hashCode, custom.hashCode)));
@override
bool operator ==(dynamic other) =>
identical(this, other) ||
(other is TableWithoutPKData &&
other.notReallyAnId == this.notReallyAnId &&
other.someFloat == this.someFloat &&
other.custom == this.custom);
}
class TableWithoutPKCompanion extends UpdateCompanion<TableWithoutPKData> {
final Value<int> notReallyAnId;
final Value<double> someFloat;
final Value<MyCustomObject> custom;
@ -1085,7 +1183,7 @@ class TableWithoutPKCompanion extends UpdateCompanion<CustomRowClass> {
this.custom = const Value.absent(),
}) : notReallyAnId = Value(notReallyAnId),
someFloat = Value(someFloat);
static Insertable<CustomRowClass> createCustom({
static Insertable<TableWithoutPKData> createCustom({
Expression<int>? notReallyAnId,
Expression<double>? someFloat,
Expression<MyCustomObject>? custom,
@ -1136,7 +1234,7 @@ class TableWithoutPKCompanion extends UpdateCompanion<CustomRowClass> {
}
class $TableWithoutPKTable extends TableWithoutPK
with TableInfo<$TableWithoutPKTable, CustomRowClass> {
with TableInfo<$TableWithoutPKTable, TableWithoutPKData> {
final GeneratedDatabase _db;
final String? _alias;
$TableWithoutPKTable(this._db, [this._alias]);
@ -1183,7 +1281,7 @@ class $TableWithoutPKTable extends TableWithoutPK
@override
final String actualTableName = 'table_without_p_k';
@override
VerificationContext validateIntegrity(Insertable<CustomRowClass> instance,
VerificationContext validateIntegrity(Insertable<TableWithoutPKData> instance,
{bool isInserting = false}) {
final context = VerificationContext();
final data = instance.toColumns(true);
@ -1208,18 +1306,9 @@ class $TableWithoutPKTable extends TableWithoutPK
@override
Set<GeneratedColumn> get $primaryKey => <GeneratedColumn>{};
@override
CustomRowClass map(Map<String, dynamic> data, {String? tablePrefix}) {
TableWithoutPKData map(Map<String, dynamic> data, {String? tablePrefix}) {
final effectivePrefix = tablePrefix != null ? '$tablePrefix.' : null;
final intType = _db.typeSystem.forDartType<int>();
final doubleType = _db.typeSystem.forDartType<double>();
final stringType = _db.typeSystem.forDartType<String>();
return CustomRowClass(
intType
.mapFromDatabaseResponse(data['${effectivePrefix}not_really_an_id'])!,
doubleType.mapFromDatabaseResponse(data['${effectivePrefix}some_float'])!,
custom: $TableWithoutPKTable.$converter0.mapToDart(stringType
.mapFromDatabaseResponse(data['${effectivePrefix}custom']))!,
);
return TableWithoutPKData.fromData(data, _db, prefix: effectivePrefix);
}
@override

View File

@ -47,7 +47,7 @@ class TableParser {
_DataClassInformation _readDataClassInformation(
List<MoorColumn> columns, ClassElement element) {
DartObject dataClassName;
DartObject useDataClass;
DartObject useRowClass;
for (final annotation in element.metadata) {
final computed = annotation.computeConstantValue();
@ -55,12 +55,12 @@ class TableParser {
if (annotationClass == 'DataClassName') {
dataClassName = computed;
} else if (annotationClass == 'UseDataClass') {
useDataClass = computed;
} else if (annotationClass == 'UseRowClass') {
useRowClass = computed;
}
}
if (dataClassName != null && useDataClass != null) {
if (dataClassName != null && useRowClass != null) {
base.step.reportError(ErrorInDartCode(
message: "A table can't be annotated with both @DataClassName and "
'@UseDataClass',
@ -77,8 +77,8 @@ class TableParser {
name = dataClassNameForClassName(element.name);
}
if (useDataClass != null) {
final type = useDataClass.getField('type').toTypeValue();
if (useRowClass != null) {
final type = useRowClass.getField('type').toTypeValue();
if (type is InterfaceType) {
existingClass = type.element;