From 8a34a41fba2da0c8b96a5d9a3243cba98393bd07 Mon Sep 17 00:00:00 2001 From: ValentinVignal Date: Sat, 3 Dec 2022 16:16:13 +0800 Subject: [PATCH] :recycle: apply the recase option to table name --- drift_dev/lib/src/analysis/options.dart | 24 ++++++++++++++++++- .../src/analysis/resolver/dart/column.dart | 24 ------------------- .../lib/src/analysis/resolver/discover.dart | 4 ++-- 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/drift_dev/lib/src/analysis/options.dart b/drift_dev/lib/src/analysis/options.dart index 8231b3b6..31c44fa2 100644 --- a/drift_dev/lib/src/analysis/options.dart +++ b/drift_dev/lib/src/analysis/options.dart @@ -2,6 +2,7 @@ import 'package:charcode/ascii.dart'; import 'package:drift/drift.dart' show SqlDialect; import 'package:json_annotation/json_annotation.dart'; import 'package:meta/meta.dart'; +import 'package:recase/recase.dart'; import 'package:sqlparser/sqlparser.dart' show BasicType, ResolvedType, SchemaFromCreateTable, SqliteVersion; import 'package:string_scanner/string_scanner.dart'; @@ -406,5 +407,26 @@ enum CaseFromDartToSql { /// /// `myColumn` -> `MYCOLUMN`. @JsonValue('UPPERCASE') - upper, + upper; + + /// Applies the correct case to the given [name]. + String apply(String name) { + final reCase = ReCase(name); + switch (this) { + case CaseFromDartToSql.preserve: + return name; + case CaseFromDartToSql.camel: + return reCase.camelCase; + case CaseFromDartToSql.constant: + return reCase.constantCase; + case CaseFromDartToSql.snake: + return reCase.snakeCase; + case CaseFromDartToSql.pascal: + return reCase.pascalCase; + case CaseFromDartToSql.lower: + return name.toLowerCase(); + case CaseFromDartToSql.upper: + return name.toUpperCase(); + } + } } diff --git a/drift_dev/lib/src/analysis/resolver/dart/column.dart b/drift_dev/lib/src/analysis/resolver/dart/column.dart index 014af449..efec5b4d 100644 --- a/drift_dev/lib/src/analysis/resolver/dart/column.dart +++ b/drift_dev/lib/src/analysis/resolver/dart/column.dart @@ -3,8 +3,6 @@ import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/dart/element/type.dart'; import 'package:collection/collection.dart'; import 'package:drift/drift.dart' show DriftSqlType; -import 'package:drift_dev/src/analysis/options.dart'; -import 'package:recase/recase.dart'; import 'package:sqlparser/sqlparser.dart' show ReferenceAction; import '../../driver/error.dart'; @@ -491,25 +489,3 @@ class PendingColumnInformation { PendingColumnInformation(this.column, {this.referencesColumnInSameTable}); } - -extension on CaseFromDartToSql { - String apply(String name) { - final reCase = ReCase(name); - switch (this) { - case CaseFromDartToSql.preserve: - return name; - case CaseFromDartToSql.camel: - return reCase.camelCase; - case CaseFromDartToSql.constant: - return reCase.constantCase; - case CaseFromDartToSql.snake: - return reCase.snakeCase; - case CaseFromDartToSql.pascal: - return reCase.pascalCase; - case CaseFromDartToSql.lower: - return name.toLowerCase(); - case CaseFromDartToSql.upper: - return name.toUpperCase(); - } - } -} diff --git a/drift_dev/lib/src/analysis/resolver/discover.dart b/drift_dev/lib/src/analysis/resolver/discover.dart index a43c3bb0..337ef1d2 100644 --- a/drift_dev/lib/src/analysis/resolver/discover.dart +++ b/drift_dev/lib/src/analysis/resolver/discover.dart @@ -2,7 +2,6 @@ import 'package:analyzer/dart/ast/ast.dart' as dart; import 'package:analyzer/dart/constant/value.dart'; import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/dart/element/visitor.dart'; -import 'package:recase/recase.dart'; import 'package:source_gen/source_gen.dart'; import 'package:sqlparser/sqlparser.dart' hide AnalysisError; @@ -217,7 +216,8 @@ class _FindDartElements extends RecursiveElementVisitor { } String _defaultNameForTableOrView(ClassElement definingElement) { - return ReCase(definingElement.name).snakeCase; + return _discoverStep._driver.options.caseFromDartToSql + .apply(definingElement.name); } DartObject? _driftViewAnnotation(ClassElement view) {