Use json_serializable for the build options

This commit is contained in:
Simon Binder 2019-11-28 20:33:52 +01:00
parent 75d4463085
commit 9e7e6d0922
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
11 changed files with 124 additions and 65 deletions

View File

@ -0,0 +1,62 @@
import 'package:json_annotation/json_annotation.dart';
part 'options.g.dart';
/// Controllable options to define the behavior of the analyzer and the
/// generator.
@JsonSerializable(
checked: true,
disallowUnrecognizedKeys: true,
fieldRename: FieldRename.snake,
createToJson: false,
)
class MoorOptions {
/// Whether moor should generate a `fromJsonString` factory for data classes.
/// It basically wraps the regular `fromJson` constructor in a `json.decode`
/// call.
@JsonKey(name: 'write_from_json_string_constructor')
final bool generateFromJsonStringConstructor;
/// Overrides [Object.hashCode] and [Object.==] in classes generated for
/// custom queries.
@JsonKey(name: 'override_hash_and_equals_in_result_sets')
final bool overrideHashAndEqualsInResultSets;
/// Also enable the compact query methods from moor files on queries defined
/// in a `UseMoor` annotation. Compact queries return a `Selectable` instead
/// of generating two methods (with one returning a stream and another
/// returning a future)
@JsonKey(name: 'compact_query_methods')
final bool compactQueryMethods;
/// Remove verification logic in the generated code.
@JsonKey(name: 'skip_verification_code')
final bool skipVerificationCode;
/// Use a `<data-class>Companion` pattern instead of `<table-class>Companion`
/// when naming companions.
@JsonKey(name: 'use_data_class_name_for_companions')
final bool useDataClassNameForCompanions;
/// For a column defined in a moor file, use the name directly instead of
/// the transformed `camelCaseDartGetter`.
@JsonKey(name: 'use_column_name_as_json_key_when_defined_in_moor_file')
final bool useColumnNameAsJsonKeyWhenDefinedInMoorFile;
/// Generate a `connect` constructor in database superclasses. This is
/// required to run databases in a background isolate.
@JsonKey(name: 'generate_connect_constructor')
final bool generateConnectConstructor;
const MoorOptions(
{this.generateFromJsonStringConstructor = false,
this.overrideHashAndEqualsInResultSets = false,
this.compactQueryMethods = false,
this.skipVerificationCode = false,
this.useDataClassNameForCompanions = false,
this.useColumnNameAsJsonKeyWhenDefinedInMoorFile = false,
this.generateConnectConstructor = false});
factory MoorOptions.fromJson(Map<String, dynamic> json) =>
_$MoorOptionsFromJson(json);
}

View File

@ -0,0 +1,51 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'options.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
MoorOptions _$MoorOptionsFromJson(Map<String, dynamic> json) {
return $checkedNew('MoorOptions', json, () {
$checkKeys(json, allowedKeys: const [
'generate_from_json_string_constructor',
'override_hash_and_equals_in_result_sets',
'compact_query_methods',
'skip_verification_code',
'use_data_class_name_for_companions',
'use_column_name_as_json_key_when_defined_in_moor_file',
'generate_connect_constructor'
]);
final val = MoorOptions(
generateFromJsonStringConstructor: $checkedConvert(
json, 'generate_from_json_string_constructor', (v) => v as bool),
overrideHashAndEqualsInResultSets: $checkedConvert(
json, 'override_hash_and_equals_in_result_sets', (v) => v as bool),
compactQueryMethods:
$checkedConvert(json, 'compact_query_methods', (v) => v as bool),
skipVerificationCode:
$checkedConvert(json, 'skip_verification_code', (v) => v as bool),
useDataClassNameForCompanions: $checkedConvert(
json, 'use_data_class_name_for_companions', (v) => v as bool),
useColumnNameAsJsonKeyWhenDefinedInMoorFile: $checkedConvert(
json,
'use_column_name_as_json_key_when_defined_in_moor_file',
(v) => v as bool),
generateConnectConstructor: $checkedConvert(
json, 'generate_connect_constructor', (v) => v as bool),
);
return val;
}, fieldKeyMap: const {
'generateFromJsonStringConstructor':
'generate_from_json_string_constructor',
'overrideHashAndEqualsInResultSets':
'override_hash_and_equals_in_result_sets',
'compactQueryMethods': 'compact_query_methods',
'skipVerificationCode': 'skip_verification_code',
'useDataClassNameForCompanions': 'use_data_class_name_for_companions',
'useColumnNameAsJsonKeyWhenDefinedInMoorFile':
'use_column_name_as_json_key_when_defined_in_moor_file',
'generateConnectConstructor': 'generate_connect_constructor'
});
}

View File

@ -3,9 +3,10 @@ import 'dart:async';
import 'package:moor_generator/src/analyzer/runner/file_graph.dart';
import 'package:moor_generator/src/analyzer/runner/task.dart';
import 'package:moor_generator/src/backends/backend.dart';
import 'package:moor_generator/src/backends/build/moor_builder.dart';
import 'package:path/path.dart' as p;
import 'options.dart';
const _fileEndings = {
'.moor': FileType.moor,
'.dart': FileType.dart,

View File

@ -1,4 +1,5 @@
import 'package:build/build.dart';
import 'package:moor_generator/src/analyzer/options.dart';
import 'package:moor_generator/src/analyzer/runner/results.dart';
import 'package:moor_generator/src/analyzer/session.dart';
import 'package:moor_generator/src/backends/build/build_backend.dart';
@ -7,8 +8,6 @@ import 'package:moor_generator/src/backends/build/generators/moor_generator.dart
import 'package:moor_generator/src/writer/writer.dart';
import 'package:source_gen/source_gen.dart';
part 'options.dart';
class MoorBuilder extends SharedPartBuilder {
final MoorOptions options;
@ -19,7 +18,7 @@ class MoorBuilder extends SharedPartBuilder {
: super(generators, name);
factory MoorBuilder(BuilderOptions options) {
final parsedOptions = MoorOptions.fromBuilder(options.config);
final parsedOptions = MoorOptions.fromJson(options.config);
final generators = <Generator>[
MoorGenerator(),

View File

@ -1,56 +0,0 @@
part of 'moor_builder.dart';
class MoorOptions {
final bool generateFromJsonStringConstructor;
final bool overrideHashAndEqualsInResultSets;
final bool compactQueryMethods;
final bool skipVerificationCode;
final bool useDataClassNameForCompanions;
final bool useColumnNameAsJsonKeyWhenDefinedInMoorFile;
final bool generateConnectConstructor;
const MoorOptions(
{this.generateFromJsonStringConstructor = false,
this.overrideHashAndEqualsInResultSets = false,
this.compactQueryMethods = false,
this.skipVerificationCode = false,
this.useDataClassNameForCompanions = false,
this.useColumnNameAsJsonKeyWhenDefinedInMoorFile = false,
this.generateConnectConstructor = false});
factory MoorOptions.fromBuilder(Map<String, dynamic> config) {
final writeFromString =
config['write_from_json_string_constructor'] as bool ?? false;
final overrideInResultSets =
config['override_hash_and_equals_in_result_sets'] as bool ?? false;
final compactQueryMethods =
config['compact_query_methods'] as bool ?? false;
final skipVerificationCode =
config['skip_verification_code'] as bool ?? false;
final dataClassNamesForCompanions =
config['use_data_class_name_for_companions'] as bool ?? false;
final useColumnNameAsJsonKeyForMoor =
config['use_column_name_as_json_key_when_defined_in_moor_file']
as bool ??
false;
final generateConnectConstructor =
config['generate_connect_constructor'] as bool ?? false;
return MoorOptions(
generateFromJsonStringConstructor: writeFromString,
overrideHashAndEqualsInResultSets: overrideInResultSets,
compactQueryMethods: compactQueryMethods,
skipVerificationCode: skipVerificationCode,
useDataClassNameForCompanions: dataClassNamesForCompanions,
useColumnNameAsJsonKeyWhenDefinedInMoorFile:
useColumnNameAsJsonKeyForMoor,
generateConnectConstructor: generateConnectConstructor,
);
}
}

View File

@ -1,5 +1,5 @@
import 'package:moor_generator/src/analyzer/options.dart';
import 'package:moor_generator/src/analyzer/sql_queries/meta/declarations.dart';
import 'package:moor_generator/src/backends/build/moor_builder.dart';
import 'package:moor_generator/src/model/used_type_converter.dart';
/// The column types in sql.

View File

@ -1,5 +1,5 @@
import 'package:moor_generator/src/analyzer/options.dart';
import 'package:moor_generator/src/analyzer/sql_queries/meta/declarations.dart';
import 'package:moor_generator/src/backends/build/moor_builder.dart';
import 'package:moor_generator/src/model/specified_column.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:moor_generator/src/model/used_type_converter.dart';

View File

@ -1,6 +1,6 @@
import 'dart:math' show max;
import 'package:moor_generator/src/backends/build/moor_builder.dart';
import 'package:moor_generator/src/analyzer/options.dart';
import 'package:moor_generator/src/model/specified_column.dart';
import 'package:moor_generator/src/model/sql_query.dart';
import 'package:moor_generator/src/utils/string_escaper.dart';

View File

@ -1,5 +1,5 @@
import 'package:meta/meta.dart';
import 'package:moor_generator/src/backends/build/moor_builder.dart';
import 'package:moor_generator/src/analyzer/options.dart';
/// Manages a tree structure which we use to generate code.
///

View File

@ -17,6 +17,7 @@ dependencies:
meta: ^1.1.0
path: ^1.6.0
logging: '>=0.11.0 <1.0.0'
json_annotation: ^3.0.0
# Moor-specific analysis
moor: ^2.0.1
@ -37,6 +38,7 @@ dev_dependencies:
test_core: ^0.2.0
build_runner: ^1.6.7
build_test: ^0.10.0
json_serializable: ^3.0.0
dependency_overrides:
sqlparser:

View File

@ -1,4 +1,4 @@
import 'package:moor_generator/src/backends/build/moor_builder.dart';
import 'package:moor_generator/src/analyzer/options.dart';
import 'package:moor_generator/src/model/specified_table.dart';
import 'package:test/test.dart';