Consider type converters for views

This commit is contained in:
Simon Binder 2021-06-16 21:55:26 +02:00
parent 66e93fc1df
commit 77e2764a46
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
4 changed files with 39 additions and 26 deletions

View File

@ -1479,8 +1479,8 @@ class WeirdTable extends Table with TableInfo<WeirdTable, WeirdData> {
class MyViewData extends DataClass {
final String configKey;
final String? configValue;
final int? syncState;
final int? syncStateImplicit;
final SyncType? syncState;
final SyncType? syncStateImplicit;
MyViewData(
{required this.configKey,
this.configValue,
@ -1493,10 +1493,11 @@ class MyViewData extends DataClass {
.mapFromDatabaseResponse(data['${effectivePrefix}config_key'])!,
configValue: const StringType()
.mapFromDatabaseResponse(data['${effectivePrefix}config_value']),
syncState: const IntType()
.mapFromDatabaseResponse(data['${effectivePrefix}sync_state']),
syncStateImplicit: const IntType().mapFromDatabaseResponse(
data['${effectivePrefix}sync_state_implicit']),
syncState: ConfigTable.$converter0.mapToDart(const IntType()
.mapFromDatabaseResponse(data['${effectivePrefix}sync_state'])),
syncStateImplicit: ConfigTable.$converter1.mapToDart(const IntType()
.mapFromDatabaseResponse(
data['${effectivePrefix}sync_state_implicit'])),
);
}
factory MyViewData.fromJson(Map<String, dynamic> json,
@ -1505,8 +1506,9 @@ class MyViewData extends DataClass {
return MyViewData(
configKey: serializer.fromJson<String>(json['configKey']),
configValue: serializer.fromJson<String?>(json['configValue']),
syncState: serializer.fromJson<int?>(json['syncState']),
syncStateImplicit: serializer.fromJson<int?>(json['syncStateImplicit']),
syncState: serializer.fromJson<SyncType?>(json['syncState']),
syncStateImplicit:
serializer.fromJson<SyncType?>(json['syncStateImplicit']),
);
}
factory MyViewData.fromJsonString(String encodedJson,
@ -1520,16 +1522,16 @@ class MyViewData extends DataClass {
return <String, dynamic>{
'configKey': serializer.toJson<String>(configKey),
'configValue': serializer.toJson<String?>(configValue),
'syncState': serializer.toJson<int?>(syncState),
'syncStateImplicit': serializer.toJson<int?>(syncStateImplicit),
'syncState': serializer.toJson<SyncType?>(syncState),
'syncStateImplicit': serializer.toJson<SyncType?>(syncStateImplicit),
};
}
MyViewData copyWith(
{String? configKey,
Value<String?> configValue = const Value.absent(),
Value<int?> syncState = const Value.absent(),
Value<int?> syncStateImplicit = const Value.absent()}) =>
Value<SyncType?> syncState = const Value.absent(),
Value<SyncType?> syncStateImplicit = const Value.absent()}) =>
MyViewData(
configKey: configKey ?? this.configKey,
configValue: configValue.present ? configValue.value : this.configValue,

View File

@ -68,8 +68,8 @@ void main() {
MyViewData(
configKey: entry.configKey,
configValue: entry.configValue,
syncState: entry.syncState?.index,
syncStateImplicit: entry.syncStateImplicit?.index,
syncState: entry.syncState,
syncStateImplicit: entry.syncStateImplicit,
),
],
]),

View File

@ -6,6 +6,7 @@ import 'package:moor_generator/src/analyzer/runner/steps.dart';
import 'package:moor_generator/src/analyzer/sql_queries/query_analyzer.dart';
import 'package:moor_generator/src/model/table.dart';
import 'package:moor_generator/src/model/view.dart';
import 'package:moor_generator/src/utils/type_converter_hint.dart';
import 'package:recase/recase.dart';
import 'package:sqlparser/sqlparser.dart';
@ -34,15 +35,24 @@ class ViewAnalyzer extends BaseAnalyzer {
const SchemaFromCreateTable(moorExtensions: true)
.readView(ctx, declaration);
final columns = [
for (final column in parserView.resolvedColumns)
MoorColumn(
type: mapper.resolvedToMoor(column.type),
name: ColumnName.explicitly(column.name),
nullable: column.type?.nullable == true,
dartGetterName: ReCase(column.name).camelCase,
)
];
final columns = <MoorColumn>[];
for (final column in parserView.resolvedColumns) {
final type = column.type;
UsedTypeConverter converter;
if (type != null && type.hint is TypeConverterHint) {
converter = (type.hint as TypeConverterHint).converter;
}
final moorColumn = MoorColumn(
type: mapper.resolvedToMoor(type),
name: ColumnName.explicitly(column.name),
nullable: type?.nullable == true,
dartGetterName: ReCase(column.name).camelCase,
typeConverter: converter,
);
columns.add(moorColumn);
}
view.columns = columns;
final desiredNames = declaration.moorTableName;

View File

@ -98,7 +98,6 @@ class DataClassWriter {
scope.generationOptions,
);
// finally, the mighty constructor invocation:
_buffer.write('return $dataClassName');
writer.writeArguments(_buffer);
_buffer.write(';}\n');
@ -225,7 +224,8 @@ class DataClassWriter {
if (column.typeConverter != null) {
// apply type converter before writing the variable
final converter = column.typeConverter;
final fieldName = '${table.entityInfoName}.${converter.fieldName}';
final fieldName =
'${converter.table.entityInfoName}.${converter.fieldName}';
final assertNotNull = !column.nullable && scope.generationOptions.nnbd;
_buffer
@ -318,7 +318,8 @@ class RowMappingWriter {
if (column.typeConverter != null) {
// stored as a static field
final converter = column.typeConverter;
final loaded = '${table.entityInfoName}.${converter.fieldName}';
final loaded =
'${converter.table.entityInfoName}.${converter.fieldName}';
loadType = '$loaded.mapToDart($loadType)';
}