From 8c7679568c83711f27b87d06300498b8e8c1b236 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Tue, 9 Mar 2021 21:33:54 +0100 Subject: [PATCH] generator: Migration option parsing to null-safety --- moor_generator/lib/src/analyzer/options.dart | 1 - .../lib/src/analyzer/options.g.dart | 92 +++++++++---------- moor_generator/moor_generator.build.yaml | 3 + moor_generator/pubspec.yaml | 2 +- 4 files changed, 47 insertions(+), 51 deletions(-) create mode 100644 moor_generator/moor_generator.build.yaml diff --git a/moor_generator/lib/src/analyzer/options.dart b/moor_generator/lib/src/analyzer/options.dart index 36f5fd87..12e2b617 100644 --- a/moor_generator/lib/src/analyzer/options.dart +++ b/moor_generator/lib/src/analyzer/options.dart @@ -1,4 +1,3 @@ -//@dart=2.9 import 'package:json_annotation/json_annotation.dart'; part 'options.g.dart'; diff --git a/moor_generator/lib/src/analyzer/options.g.dart b/moor_generator/lib/src/analyzer/options.g.dart index 323c3fdc..846d30ac 100644 --- a/moor_generator/lib/src/analyzer/options.g.dart +++ b/moor_generator/lib/src/analyzer/options.g.dart @@ -1,4 +1,3 @@ -//@dart=2.9 // GENERATED CODE - DO NOT MODIFY BY HAND part of 'options.dart'; @@ -25,60 +24,60 @@ MoorOptions _$MoorOptionsFromJson(Map json) { 'apply_converters_on_variables', 'generate_values_in_copy_with', 'named_parameters', - 'new_sql_code_generation', + 'new_sql_code_generation' ]); final val = MoorOptions( generateFromJsonStringConstructor: $checkedConvert( - json, 'write_from_json_string_constructor', (v) => v as bool) ?? + json, 'write_from_json_string_constructor', (v) => v as bool?) ?? false, overrideHashAndEqualsInResultSets: $checkedConvert(json, - 'override_hash_and_equals_in_result_sets', (v) => v as bool) ?? + 'override_hash_and_equals_in_result_sets', (v) => v as bool?) ?? false, compactQueryMethods: - $checkedConvert(json, 'compact_query_methods', (v) => v as bool) ?? + $checkedConvert(json, 'compact_query_methods', (v) => v as bool?) ?? true, skipVerificationCode: - $checkedConvert(json, 'skip_verification_code', (v) => v as bool) ?? + $checkedConvert(json, 'skip_verification_code', (v) => v as bool?) ?? false, useDataClassNameForCompanions: $checkedConvert( - json, 'use_data_class_name_for_companions', (v) => v as bool) ?? + json, 'use_data_class_name_for_companions', (v) => v as bool?) ?? false, useColumnNameAsJsonKeyWhenDefinedInMoorFile: $checkedConvert( json, 'use_column_name_as_json_key_when_defined_in_moor_file', - (v) => v as bool) ?? + (v) => v as bool?) ?? true, generateConnectConstructor: $checkedConvert( - json, 'generate_connect_constructor', (v) => v as bool) ?? + json, 'generate_connect_constructor', (v) => v as bool?) ?? false, eagerlyLoadDartAst: - $checkedConvert(json, 'eagerly_load_dart_ast', (v) => v as bool) ?? + $checkedConvert(json, 'eagerly_load_dart_ast', (v) => v as bool?) ?? false, - dataClassToCompanions: - $checkedConvert(json, 'data_class_to_companions', (v) => v as bool) ?? - true, + dataClassToCompanions: $checkedConvert( + json, 'data_class_to_companions', (v) => v as bool?) ?? + true, generateMutableClasses: - $checkedConvert(json, 'mutable_classes', (v) => v as bool) ?? false, + $checkedConvert(json, 'mutable_classes', (v) => v as bool?) ?? false, rawResultSetData: - $checkedConvert(json, 'raw_result_set_data', (v) => v as bool) ?? + $checkedConvert(json, 'raw_result_set_data', (v) => v as bool?) ?? false, applyConvertersOnVariables: $checkedConvert( - json, 'apply_converters_on_variables', (v) => v as bool) ?? + json, 'apply_converters_on_variables', (v) => v as bool?) ?? false, generateValuesInCopyWith: $checkedConvert( - json, 'generate_values_in_copy_with', (v) => v as bool) ?? + json, 'generate_values_in_copy_with', (v) => v as bool?) ?? false, generateNamedParameters: - $checkedConvert(json, 'named_parameters', (v) => v as bool) ?? false, + $checkedConvert(json, 'named_parameters', (v) => v as bool?) ?? false, newSqlCodeGeneration: - $checkedConvert(json, 'new_sql_code_generation', (v) => v as bool) ?? + $checkedConvert(json, 'new_sql_code_generation', (v) => v as bool?) ?? false, modules: $checkedConvert( json, 'sqlite_modules', - (v) => (v as List) - ?.map((e) => _$enumDecodeNullable(_$SqlModuleEnumMap, e)) - ?.toList()) ?? + (v) => (v as List?) + ?.map((e) => _$enumDecode(_$SqlModuleEnumMap, e)) + .toList()) ?? [], ); return val; @@ -95,44 +94,39 @@ MoorOptions _$MoorOptionsFromJson(Map json) { 'eagerlyLoadDartAst': 'eagerly_load_dart_ast', 'dataClassToCompanions': 'data_class_to_companions', 'generateMutableClasses': 'mutable_classes', - 'modules': 'sqlite_modules', 'rawResultSetData': 'raw_result_set_data', + 'applyConvertersOnVariables': 'apply_converters_on_variables', 'generateValuesInCopyWith': 'generate_values_in_copy_with', 'generateNamedParameters': 'named_parameters', 'newSqlCodeGeneration': 'new_sql_code_generation', + 'modules': 'sqlite_modules' }); } -T _$enumDecode( - Map enumValues, - dynamic source, { - T unknownValue, +K _$enumDecode( + Map enumValues, + Object? source, { + K? unknownValue, }) { if (source == null) { - throw ArgumentError('A value must be provided. Supported values: ' - '${enumValues.values.join(', ')}'); + throw ArgumentError( + 'A value must be provided. Supported values: ' + '${enumValues.values.join(', ')}', + ); } - final value = enumValues.entries - .singleWhere((e) => e.value == source, orElse: () => null) - ?.key; - - if (value == null && unknownValue == null) { - throw ArgumentError('`$source` is not one of the supported values: ' - '${enumValues.values.join(', ')}'); - } - return value ?? unknownValue; -} - -T _$enumDecodeNullable( - Map enumValues, - dynamic source, { - T unknownValue, -}) { - if (source == null) { - return null; - } - return _$enumDecode(enumValues, source, unknownValue: unknownValue); + return enumValues.entries.singleWhere( + (e) => e.value == source, + orElse: () { + if (unknownValue == null) { + throw ArgumentError( + '`$source` is not one of the supported values: ' + '${enumValues.values.join(', ')}', + ); + } + return MapEntry(unknownValue, enumValues.values.first); + }, + ).key; } const _$SqlModuleEnumMap = { diff --git a/moor_generator/moor_generator.build.yaml b/moor_generator/moor_generator.build.yaml new file mode 100644 index 00000000..c8966aec --- /dev/null +++ b/moor_generator/moor_generator.build.yaml @@ -0,0 +1,3 @@ +# Build config without moor-specific builders. When running a build in this directory, this file replaces build.yaml, +# which is what we want! Our builders depend on generated code, so they break the generated build script at the start of +# a build. diff --git a/moor_generator/pubspec.yaml b/moor_generator/pubspec.yaml index 95f39e5c..e0d55d08 100644 --- a/moor_generator/pubspec.yaml +++ b/moor_generator/pubspec.yaml @@ -6,7 +6,7 @@ homepage: https://moor.simonbinder.eu/ issue_tracker: https://github.com/simolus3/moor/issues environment: - sdk: '>=2.6.0 <3.0.0' + sdk: '>=2.12.0 <3.0.0' dependencies: charcode: ^1.2.0