Skip to content

Commit

Permalink
Merge branch 'develop' into better-escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
vitusortner authored May 5, 2021
2 parents ea29b29 + 57b4899 commit a6488ab
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 30 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
![Floor](https://raw.githubusercontent.com/vitusortner/floor/develop/img/floor.png)

**See the [project's website](https://vitusortner.github.io/floor/) for the full documentation.**
**[Vote for our domain name 🗳](https://github.com/vitusortner/floor/discussions/546)**
**See the [project's website](https://floor.codes) for the full documentation.**

Floor provides a neat SQLite abstraction for your Flutter applications inspired by the [Room persistence library](https://developer.android.com/topic/libraries/architecture/room).
It comes with automatic mapping between in-memory objects and database rows while still offering full control of the database with the use of SQL.
Expand Down
1 change: 1 addition & 0 deletions docs/CNAME
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
floor.codes
3 changes: 1 addition & 2 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ description: >
The typesafe, reactive and lightweight SQLite abstraction for your Flutter applications.
This is an example on how to use the library.
version: 1.0.0
homepage: https://github.com/vitusortner/floor
author: vitusortner
homepage: https://floor.codes
publish_to: none

environment:
Expand Down
2 changes: 1 addition & 1 deletion floor/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
![Floor](https://raw.githubusercontent.com/vitusortner/floor/develop/img/floor.png)

**See the [project's website](https://vitusortner.github.io/floor/) for the full documentation.**
**See the [project's website](https://floor.codes) for the full documentation.**

Floor provides a neat SQLite abstraction for your Flutter applications inspired by the [Room persistence library](https://developer.android.com/topic/libraries/architecture/room).
It comes with automatic mapping between in-memory objects and database rows while still offering full control of the database with the use of SQL.
Expand Down
3 changes: 1 addition & 2 deletions floor/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ description: >
The typesafe, reactive, and lightweight SQLite abstraction for your Flutter applications.
This library is the runtime dependency.
version: 1.1.0
homepage: https://github.com/vitusortner/floor
author: Vitus Ortner <[email protected]>
homepage: https://floor.codes
publish_to: none

environment:
Expand Down
3 changes: 1 addition & 2 deletions floor_annotation/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ description: >
The typesafe, reactive, and lightweight SQLite abstraction for your Flutter applications.
Don't use this package directly. Import the floor package instead.
version: 1.0.0
homepage: https://github.com/vitusortner/floor
author: Vitus Ortner <[email protected]>
homepage: https://floor.codes

environment:
sdk: '>=2.12.0 <3.0.0'
Expand Down
25 changes: 9 additions & 16 deletions floor_generator/lib/processor/queryable_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:collection/collection.dart';
import 'package:floor_annotation/floor_annotation.dart' as annotations;
import 'package:floor_generator/misc/extension/dart_type_extension.dart';
import 'package:floor_generator/misc/extension/set_extension.dart';
import 'package:floor_generator/misc/extension/string_extension.dart';
import 'package:floor_generator/misc/extension/type_converter_element_extension.dart';
Expand Down Expand Up @@ -79,7 +80,6 @@ abstract class QueryableProcessor<T extends Queryable> extends Processor<T> {
if (parameterElement.type.isDefaultSqlType) {
parameterValue = databaseValue.cast(
parameterElement.type,
field.isNullable,
parameterElement,
);
} else {
Expand All @@ -88,7 +88,6 @@ abstract class QueryableProcessor<T extends Queryable> extends Processor<T> {
.getClosest(parameterElement.type);
final castedDatabaseValue = databaseValue.cast(
typeConverter.databaseType,
field.isNullable,
parameterElement,
);

Expand All @@ -107,27 +106,21 @@ abstract class QueryableProcessor<T extends Queryable> extends Processor<T> {
}

extension on String {
String cast(
DartType dartType,
bool isNullable,
ParameterElement parameterElement,
) {
String cast(DartType dartType, ParameterElement parameterElement) {
if (dartType.isDartCoreBool) {
if (isNullable) {
if (dartType.isNullable) {
// if the value is null, return null
// if the value is not null, interpret 1 as true and 0 as false
return '$this == null ? null : ($this as int) != 0';
} else {
return '($this as int) != 0';
}
} else if (dartType.isDartCoreString) {
return '$this as String${isNullable ? '?' : ''}';
} else if (dartType.isDartCoreInt) {
return '$this as int${isNullable ? '?' : ''}';
} else if (dartType.isUint8List) {
return '$this as Uint8List${isNullable ? '?' : ''}';
} else if (dartType.isDartCoreDouble) {
return '$this as double${isNullable ? '?' : ''}';
} else if (dartType.isDartCoreString ||
dartType.isDartCoreInt ||
dartType.isUint8List ||
dartType.isDartCoreDouble) {
final typeString = dartType.getDisplayString(withNullability: true);
return '$this as $typeString';
} else {
throw InvalidGenerationSourceError(
'Trying to convert unsupported type $dartType.',
Expand Down
4 changes: 2 additions & 2 deletions floor_generator/lib/writer/transaction_method_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class TransactionMethodWriter implements Writer {
return Method((builder) => builder
..annotations.add(overrideAnnotationExpression)
..returns = refer(method.returnType.getDisplayString(
withNullability: false,
withNullability: true,
))
..name = method.name
..requiredParameters.addAll(_generateParameters())
Expand Down Expand Up @@ -47,7 +47,7 @@ class TransactionMethodWriter implements Writer {
return Parameter((builder) => builder
..name = parameter.name
..type = refer(parameter.type.getDisplayString(
withNullability: false,
withNullability: true,
)));
}).toList();
}
Expand Down
3 changes: 1 addition & 2 deletions floor_generator/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ description: >
The typesafe, reactive, and lightweight SQLite abstraction for your Flutter applications.
This library is the dev dependency.
version: 1.1.0
homepage: https://github.com/vitusortner/floor
author: Vitus Ortner <[email protected]>
homepage: https://floor.codes
publish_to: none

environment:
Expand Down
82 changes: 82 additions & 0 deletions floor_generator/test/writer/transaction_method_writer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,88 @@ void main() {
}
'''));
});

test('Generate transaction method with nullable future return', () async {
final transactionMethod = await _createTransactionMethod('''
@transaction
Future<Person>? replacePersons(List<Person> persons) async {
return null;
}
''');

final actual = TransactionMethodWriter(transactionMethod).write();

expect(actual, equalsDart(r'''
@override
Future<Person>? replacePersons(List<Person> persons) async {
if (database is sqflite.Transaction) {
return super.replacePersons(persons);
} else {
return (database as sqflite.Database)
.transaction<Person>((transaction) async {
final transactionDatabase = _$TestDatabase(changeListener)
..database = transaction;
return transactionDatabase.personDao.replacePersons(persons);
});
}
}
'''));
});

test('Generate transaction method with nullable return type parameter',
() async {
final transactionMethod = await _createTransactionMethod('''
@transaction
Future<Person?> replacePersons(List<Person> persons) async {
return null;
}
''');

final actual = TransactionMethodWriter(transactionMethod).write();

expect(actual, equalsDart(r'''
@override
Future<Person?> replacePersons(List<Person> persons) async {
if (database is sqflite.Transaction) {
return super.replacePersons(persons);
} else {
return (database as sqflite.Database)
.transaction<Person>((transaction) async {
final transactionDatabase = _$TestDatabase(changeListener)
..database = transaction;
return transactionDatabase.personDao.replacePersons(persons);
});
}
}
'''));
});

test('Generate transaction method with nullable parameter', () async {
final transactionMethod = await _createTransactionMethod('''
@transaction
Future<Person>? replacePersons(Person? person) async {
return null;
}
''');

final actual = TransactionMethodWriter(transactionMethod).write();

expect(actual, equalsDart(r'''
@override
Future<Person>? replacePersons(Person? person) async {
if (database is sqflite.Transaction) {
return super.replacePersons(person);
} else {
return (database as sqflite.Database)
.transaction<Person>((transaction) async {
final transactionDatabase = _$TestDatabase(changeListener)
..database = transaction;
return transactionDatabase.personDao.replacePersons(person);
});
}
}
'''));
});
}

Future<TransactionMethod> _createTransactionMethod(
Expand Down
2 changes: 1 addition & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
site_name: Floor SQLite
site_description: "Floor - The typesafe, reactive, and lightweight SQLite abstraction for your Flutter applications"
site_url: https://vitusortner.github.io/floor/
site_url: https://floor.codes
repo_name: floor
repo_url: https://github.com/vitusortner/floor
edit_uri: edit/develop/docs/
Expand Down

0 comments on commit a6488ab

Please sign in to comment.