From 77e2764a46f3237ee8343c83d1771a3aedf7a7b9 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Wed, 16 Jun 2021 21:55:26 +0200 Subject: [PATCH] Consider type converters for views --- moor/test/data/tables/custom_tables.g.dart | 26 +++++++++-------- .../moor_files_integration_test.dart | 4 +-- .../lib/src/analyzer/view/view_analyzer.dart | 28 +++++++++++++------ .../src/writer/tables/data_class_writer.dart | 7 +++-- 4 files changed, 39 insertions(+), 26 deletions(-) diff --git a/moor/test/data/tables/custom_tables.g.dart b/moor/test/data/tables/custom_tables.g.dart index 05d4a438..0e777070 100644 --- a/moor/test/data/tables/custom_tables.g.dart +++ b/moor/test/data/tables/custom_tables.g.dart @@ -1479,8 +1479,8 @@ class WeirdTable extends Table with TableInfo { 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 json, @@ -1505,8 +1506,9 @@ class MyViewData extends DataClass { return MyViewData( configKey: serializer.fromJson(json['configKey']), configValue: serializer.fromJson(json['configValue']), - syncState: serializer.fromJson(json['syncState']), - syncStateImplicit: serializer.fromJson(json['syncStateImplicit']), + syncState: serializer.fromJson(json['syncState']), + syncStateImplicit: + serializer.fromJson(json['syncStateImplicit']), ); } factory MyViewData.fromJsonString(String encodedJson, @@ -1520,16 +1522,16 @@ class MyViewData extends DataClass { return { 'configKey': serializer.toJson(configKey), 'configValue': serializer.toJson(configValue), - 'syncState': serializer.toJson(syncState), - 'syncStateImplicit': serializer.toJson(syncStateImplicit), + 'syncState': serializer.toJson(syncState), + 'syncStateImplicit': serializer.toJson(syncStateImplicit), }; } MyViewData copyWith( {String? configKey, Value configValue = const Value.absent(), - Value syncState = const Value.absent(), - Value syncStateImplicit = const Value.absent()}) => + Value syncState = const Value.absent(), + Value syncStateImplicit = const Value.absent()}) => MyViewData( configKey: configKey ?? this.configKey, configValue: configValue.present ? configValue.value : this.configValue, diff --git a/moor/test/integration_tests/moor_files_integration_test.dart b/moor/test/integration_tests/moor_files_integration_test.dart index beb9b60d..9e4e993c 100644 --- a/moor/test/integration_tests/moor_files_integration_test.dart +++ b/moor/test/integration_tests/moor_files_integration_test.dart @@ -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, ), ], ]), diff --git a/moor_generator/lib/src/analyzer/view/view_analyzer.dart b/moor_generator/lib/src/analyzer/view/view_analyzer.dart index cab487cf..61d2378f 100644 --- a/moor_generator/lib/src/analyzer/view/view_analyzer.dart +++ b/moor_generator/lib/src/analyzer/view/view_analyzer.dart @@ -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 = []; + 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; diff --git a/moor_generator/lib/src/writer/tables/data_class_writer.dart b/moor_generator/lib/src/writer/tables/data_class_writer.dart index 5399a215..82439d66 100644 --- a/moor_generator/lib/src/writer/tables/data_class_writer.dart +++ b/moor_generator/lib/src/writer/tables/data_class_writer.dart @@ -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)'; }