Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for type converters #318

Merged
merged 95 commits into from
Oct 16, 2020
Merged

Add support for type converters #318

merged 95 commits into from
Oct 16, 2020

Conversation

vitusortner
Copy link
Collaborator

@vitusortner vitusortner commented Apr 21, 2020

The long-awaited type converters are here! 🎉

Public API

First, some words about the public API of type converters. I wanted to make type converters as safe as possible at compile time. Thereby, I added an abstract TypeConverter class that required two type parameters. The first is the encoded type that is used outside of the database scope. The second type is the decoded database type. The abstract class, furthermore, comes with the two functions encode() and decode() which have to be overridden by the implementation classes. This way, we can be sure that there always is functionality provided for conversion in both directions. Room is missing this extra security as they use plain functions as type converters.

A simple type converter that converts from DateTime to int and vice versa is shown in the following.

class DateTimeConverter extends TypeConverter<DateTime, int> {
  @override
  DateTime decode(int databaseValue) {
    return DateTime.fromMillisecondsSinceEpoch(databaseValue);
  }

  @override
  int encode(DateTime value) {
    return value.millisecondsSinceEpoch;
  }
}

Such a class can then be applied to the database with different scope restrictions by using the @TypeConverters annotation. These scopes are:

  1. databases
  2. DAOs
  3. entities/views
  4. entity/view fields
  5. DAO methods
  6. DAO method parameters

Implementation

Floor generates and uses type converter as described in the following.

Processors

  1. Collect type converters with the database scope and pass these to all processors
  2. Collect type converters with the DAO scope and pass these to the specific DAO processor
  3. This basically happens for each scope level and thereby, the processors only get the relevant type converters

Writers

  1. Instantiate/write an object of each type converter class it can find (it has to be added with @TypeConverters)
  2. Use the generated type converters throughout the generated code when needed

This still needs work:

  • tests (validate type converter scopes/order and correct usage)

Closes #165
Closes #313 as we won't fall back to double when the type of a field is unknown

@vitusortner vitusortner added the feature Request and implementation of a feature (release drafter) label Apr 21, 2020
@codecov
Copy link

codecov bot commented Apr 21, 2020

Codecov Report

Merging #318 into develop will increase coverage by 1.14%.
The diff coverage is 83.01%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #318      +/-   ##
===========================================
+ Coverage    81.56%   82.71%   +1.14%     
===========================================
  Files           60       68       +8     
  Lines         1535     1660     +125     
===========================================
+ Hits          1252     1373     +121     
- Misses         283      287       +4     
Flag Coverage Δ
#floor 82.71% <83.01%> (+1.14%) ⬆️
#floor_generator 82.50% <83.01%> (+1.34%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...loor_generator/lib/value_object/change_method.dart 15.78% <ø> (+1.50%) ⬆️
floor_generator/lib/value_object/dao.dart 3.70% <0.00%> (-0.30%) ⬇️
floor_generator/lib/value_object/database.dart 10.34% <0.00%> (-1.66%) ⬇️
...or_generator/lib/value_object/deletion_method.dart 50.00% <0.00%> (-50.00%) ⬇️
...r_generator/lib/value_object/insertion_method.dart 18.18% <0.00%> (ø)
floor_generator/lib/value_object/field.dart 64.00% <40.00%> (-1.22%) ⬇️
floor_generator/lib/value_object/query_method.dart 63.33% <50.00%> (-0.96%) ⬇️
floor_generator/lib/processor/field_processor.dart 89.18% <77.77%> (-0.47%) ⬇️
floor_generator/lib/value_object/entity.dart 95.12% <87.50%> (-1.11%) ⬇️
...loor_generator/lib/writer/query_method_writer.dart 97.43% <88.88%> (-2.57%) ⬇️
... and 27 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fab7583...34f96dd. Read the comment docs.

ghost
ghost previously approved these changes May 4, 2020
vitusortner added 10 commits May 8, 2020 21:45
# Conflicts:
#	floor_generator/lib/processor/queryable_processor.dart
#	floor_generator/lib/value_object/entity.dart
#	floor_generator/lib/writer/query_method_writer.dart
#	floor_generator/test/value_object/entity_test.dart
@fnicastri
Copy link

@mqus
Thanks for your time, it was just an idea.
@avbk
Yu can definitely merge Recipe and RecipeDTO, the only downside is when you have a lot of annotations, it starts to feel messy

@fingerart
Copy link

@vitusortner
Hi. Do you have an approximate release date for this feature?

@FlashLight13
Copy link

@vitusortner Hi! Any updates on this? 🙂

@vitusortner
Copy link
Collaborator Author

I finally have some more time to look into this feature again. I'll keep you updated!

# Conflicts:
#	floor_generator/lib/processor/entity_processor.dart
#	floor_generator/lib/value_object/entity.dart
#	floor_generator/test/processor/entity_processor_test.dart
#	floor_generator/test/value_object/entity_test.dart
@vitusortner vitusortner marked this pull request as ready for review October 16, 2020 19:27
@vitusortner vitusortner merged commit 5fd7cd8 into develop Oct 16, 2020
@vitusortner vitusortner deleted the type-converters branch October 16, 2020 19:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Request and implementation of a feature (release drafter)
Development

Successfully merging this pull request may close these issues.

Can't primaryKey be String type? Add support for type converters
7 participants