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 GenerateAdapters annotation #35

Open
wants to merge 91 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
91 commits
Select commit Hold shift + click to select a range
44fed27
Almost there
Rexios80 Oct 18, 2024
65c688c
This is hard
Rexios80 Oct 18, 2024
5dfdcd1
Almost there
Rexios80 Oct 18, 2024
0fcfc14
What a mess
Rexios80 Oct 18, 2024
4680806
...
Rexios80 Oct 18, 2024
058bc7e
...
Rexios80 Oct 18, 2024
4f1ad99
...
Rexios80 Oct 18, 2024
17be7ae
...
Rexios80 Oct 18, 2024
071244f
...
Rexios80 Oct 18, 2024
0a567d4
...
Rexios80 Oct 18, 2024
aec65d2
...
Rexios80 Oct 18, 2024
e93e120
...
Rexios80 Oct 18, 2024
472aadb
Add special handling for ignored enum fields
Rexios80 Oct 19, 2024
5056d65
Ignore all fields on Object and Enum
Rexios80 Oct 19, 2024
92f9032
...
Rexios80 Oct 19, 2024
5714d48
...
Rexios80 Oct 19, 2024
c5e667d
...
Rexios80 Oct 19, 2024
2bbcc88
...
Rexios80 Oct 19, 2024
73b8863
Cleanup
Rexios80 Oct 19, 2024
66f07e1
Documentation
Rexios80 Oct 19, 2024
4ee5fda
Cleanup
Rexios80 Oct 19, 2024
7532405
Cleanup
Rexios80 Oct 19, 2024
196bcd8
...
Rexios80 Oct 19, 2024
fe83e39
...
Rexios80 Oct 19, 2024
b50676b
Working on documentation
Rexios80 Oct 19, 2024
1abfb3d
...
Rexios80 Oct 20, 2024
2a7f14e
...
Rexios80 Oct 20, 2024
d5eff44
Add migration guide
Rexios80 Oct 20, 2024
d84bdc6
...
Rexios80 Oct 20, 2024
f4fd915
Ignore static fields
Rexios80 Oct 20, 2024
4fb7c38
...
Rexios80 Oct 20, 2024
296592c
Switch to SharedPartBuilder for GenerateAdapters
Rexios80 Oct 20, 2024
aec3af8
Properly sort types
Rexios80 Oct 20, 2024
3fa9fd3
...
Rexios80 Oct 20, 2024
eeee9fe
Ignore getters without setters
Rexios80 Oct 20, 2024
a67fc17
Attempt 2 for this
Rexios80 Oct 20, 2024
a1eea4a
...
Rexios80 Oct 20, 2024
fc7f944
...
Rexios80 Oct 20, 2024
edebf3a
Only generate one id per field name
Rexios80 Oct 20, 2024
0b3be9f
Cleanup
Rexios80 Oct 21, 2024
abb0270
Working on documentation
Rexios80 Oct 21, 2024
64d676a
Convert example to GenerateAdapters
Rexios80 Oct 21, 2024
9d0176a
First test
Rexios80 Oct 21, 2024
e37d3f7
Initial tests
Rexios80 Oct 21, 2024
bd9ce0e
More tests
Rexios80 Oct 21, 2024
01d4b70
...
Rexios80 Oct 21, 2024
d89d73a
...
Rexios80 Oct 21, 2024
3bd8a00
Update references to old hive repo
Rexios80 Oct 21, 2024
1f123f0
...
Rexios80 Oct 21, 2024
fdddccb
Read hive_schema.yaml through the buildStep so changes affect builds
Rexios80 Oct 21, 2024
6a5a38e
...
Rexios80 Oct 21, 2024
a1cc280
Add codecov.yml
Rexios80 Oct 21, 2024
2a1e401
...
Rexios80 Oct 21, 2024
af4512d
Ignore constructors for coverage
Rexios80 Oct 21, 2024
ec6e8d7
Ignore non-static fields on enums
Rexios80 Oct 21, 2024
4c43e23
Schema validation
Rexios80 Oct 22, 2024
86ad76d
Place `hive_registrar.g.dart` with the `GenerateAdapters` annotation …
Rexios80 Oct 23, 2024
7a0792d
Add tests for registrar generation
Rexios80 Oct 23, 2024
1ceb434
Updating documentation
Rexios80 Oct 23, 2024
79b3150
Regenerate
Rexios80 Oct 23, 2024
e30faf7
Merge branch 'main' into feature/generate-adapters
Rexios80 Oct 23, 2024
6ca6bf5
Fix analysis in generated files
Rexios80 Oct 23, 2024
1f47cb8
...
Rexios80 Oct 23, 2024
62b0bd0
Do not run build_runner in hive_generator
Rexios80 Oct 23, 2024
0cea303
Add more details to registrar generation errors
Rexios80 Oct 25, 2024
64a3f7c
Fix tests
Rexios80 Oct 25, 2024
4ddce35
Sort uris in error for testing
Rexios80 Oct 25, 2024
9b8f093
Formatting
Rexios80 Oct 25, 2024
04c5cc3
Update grammar
Rexios80 Oct 25, 2024
bf2ef97
Respect configured ignores in registrar builder
Rexios80 Oct 28, 2024
90691c4
Merge branch 'main' into feature/generate-adapters
Rexios80 Oct 28, 2024
a43c060
Use correct version
Rexios80 Oct 28, 2024
bf2f873
...
Rexios80 Oct 28, 2024
cf18879
Regenerate mocks
Rexios80 Oct 28, 2024
d895cbb
Rename file
Rexios80 Oct 29, 2024
faeca12
Merge remote-tracking branch 'origin/main' into feature/generate-adap…
Rexios80 Oct 29, 2024
66e3345
Merge branch 'main' into feature/generate-adapters
Rexios80 Nov 1, 2024
055ce88
Update readme
Rexios80 Nov 1, 2024
2f56899
Merge branch 'main' into feature/generate-adapters
Rexios80 Nov 5, 2024
f30b1b7
Refactoring
Rexios80 Nov 5, 2024
6242699
...
Rexios80 Nov 5, 2024
5cd9ab7
Wrote the migrator
Rexios80 Nov 5, 2024
4e6bb8c
Testing works
Rexios80 Nov 5, 2024
5f415aa
...
Rexios80 Nov 5, 2024
a27e50f
Update readme
Rexios80 Nov 5, 2024
8893735
...
Rexios80 Nov 5, 2024
26381be
...
Rexios80 Nov 5, 2024
bc92b1a
...
Rexios80 Nov 5, 2024
d59a424
...
Rexios80 Nov 5, 2024
1f91041
...
Rexios80 Nov 5, 2024
53e3a7d
Sanitize field names
Rexios80 Nov 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions hive/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.8.0

- Adds `GenerateAdapters` annotation and relevant documentation

## 2.7.0+1

- Adds a storage benchmark to compare Hive CE with Hive v4
Expand Down
87 changes: 63 additions & 24 deletions hive/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ Hive CE is a spiritual continuation of Hive v2 with the following new features:
- Support for constructor parameter defaults
- Freezed support
- Support for generating adapters with classes that use named imports
- Automatic type adapter generation using the `GenerateAdapters` annotation
- No more manually adding annotations to every type and field
- Generate adapters for classes outside the current package

## Hive CE (v2) vs Hive v4 (Isar)

Expand Down Expand Up @@ -187,19 +190,34 @@ Hive not only supports primitives, lists, and maps but also any Dart object you
```dart
import 'package:hive_ce/hive.dart';

@HiveType(typeId: 0)
class Person extends HiveObject {
Person({required this.name, required this.age});

@HiveField(0)
String name;

@HiveField(1)
int age;
}

```

### Create a `GeneateAdapters` annotation

Usually this is placed in `lib/hive/hive_adapters.dart`

<!-- embedme readme/store_objects/hive_adapters.dart -->

```dart
import 'package:hive_ce/hive.dart';
import 'person.dart';

part 'hive_adapters.g.dart';

@GenerateAdapters([AdapterSpec<Person>()])
// Annotations must be on some element
// ignore: unused_element
void _() {}

```

### Update `pubspec.yaml`

```yaml
Expand All @@ -214,7 +232,15 @@ dev_dependencies:
dart pub run build_runner build --delete-conflicting-outputs
```

This will generate all of your `TypeAdapter`s as well as a Hive extension to register them all in one go
This will generate the following:

- TypeAdapters for the specified AdapterSpecs
- TypeAdapters for all explicitly defined HiveTypes
- A `hive_adapters.g.dart` file containing all adapters generated from the `GenerateAdapters` annotation
- A `hive_adapters.g.yaml` file
- A `hive_registrar.g.dart` file containing an extension method to register all generated adapters

All of the generated files should be checked into version control. These files are explained in more detail below.

### Use the Hive registrar

Expand Down Expand Up @@ -256,6 +282,38 @@ void example() async {

```

### About `hive_adapters.g.yaml`

The Hive schema is a generated yaml file that contains the information necessary to incrementally update the generated TypeAdapters as your model classes evolve.

Some migrations might require manual modifications to the Hive schema file. One example is field renaming. Without manual intervention, the generator will see both an added and removed field. To resolve this, manually rename the field in the schema.

### Migrating to `GenerateAdapters`

1. Set up the `GenerateAdapters` annotation as described above
2. Add the following to your `build.yaml` file:

```yaml
targets:
$default:
builders:
hive_ce_generator|hive_schema_migrator:
enabled: true
```

2. Run the `build_runner`
3. Replace the content of `lib/hive/hive_adapters.g.yaml` with the content of `hive_schema.g.yaml`
4. Remove all explicit `HiveType` and `HiveField` annotations from your model classes
5. Run the `build_runner` again
6. Revert the `build.yaml` changes
7. Delete the `hive_schema.g.yaml` file

### Explicitly defining HiveTypes

The old method of defining HiveTypes is still supported, but should be unnecessary now that Hive CE supports constructor parameter defaults. If you have a use-case that `GenerateAdapters` does not support, please [create an issue on GitHub](https://github.com/IO-Design-Team/hive_ce/issues/new).

Unfortunately it is not possible for `GenerateAdapters` to handle private fields. You can use `@protected` instead if necessary.

## Add fields to objects

When adding a new non-nullable field to an existing object, you need to specify a default value to ensure compatibility with existing data.
Expand All @@ -267,14 +325,10 @@ For example, consider an existing database with a `Person` object:
```dart
import 'package:hive_ce/hive.dart';

@HiveType(typeId: 0)
class Person extends HiveObject {
Person({required this.name, required this.age});

@HiveField(0)
String name;

@HiveField(1)
int age;
}

Expand All @@ -287,31 +341,16 @@ If you want to add a `balance` field, you must specify a default value or else r
```dart
import 'package:hive_ce/hive.dart';

@HiveType(typeId: 0)
class Person extends HiveObject {
Person({required this.name, required this.age, this.balance = 0});

@HiveField(0)
String name;

@HiveField(1)
int age;

@HiveField(2)
double balance;
}

```

Or specify it in the `HiveField` annotation:

```dart
@HiveField(2, defaultValue: 0)
int balance;
```

Alternatively, you can write custom migration code to handle the transition.

After modifying the model, remember to run `build_runner` to regenerate the TypeAdapters

## Hive ❤️ Flutter
Expand Down
9 changes: 3 additions & 6 deletions hive/example/lib/freezed.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:hive_ce/hive.dart';

part 'freezed.freezed.dart';
part 'freezed.g.dart';

@freezed
@HiveType(typeId: 100)
class FreezedPerson with _$FreezedPerson {
const factory FreezedPerson({
@HiveField(0) required String firstName,
@HiveField(1) required String lastName,
@HiveField(2) required int age,
required String firstName,
required String lastName,
required int age,
}) = _FreezedPerson;
}
29 changes: 6 additions & 23 deletions hive/example/lib/freezed.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ final _privateConstructorUsedError = UnsupportedError(

/// @nodoc
mixin _$FreezedPerson {
@HiveField(0)
String get firstName => throw _privateConstructorUsedError;
@HiveField(1)
String get lastName => throw _privateConstructorUsedError;
@HiveField(2)
int get age => throw _privateConstructorUsedError;

/// Create a copy of FreezedPerson
Expand All @@ -36,10 +33,7 @@ abstract class $FreezedPersonCopyWith<$Res> {
FreezedPerson value, $Res Function(FreezedPerson) then) =
_$FreezedPersonCopyWithImpl<$Res, FreezedPerson>;
@useResult
$Res call(
{@HiveField(0) String firstName,
@HiveField(1) String lastName,
@HiveField(2) int age});
$Res call({String firstName, String lastName, int age});
}

/// @nodoc
Expand Down Expand Up @@ -86,10 +80,7 @@ abstract class _$$FreezedPersonImplCopyWith<$Res>
__$$FreezedPersonImplCopyWithImpl<$Res>;
@override
@useResult
$Res call(
{@HiveField(0) String firstName,
@HiveField(1) String lastName,
@HiveField(2) int age});
$Res call({String firstName, String lastName, int age});
}

/// @nodoc
Expand Down Expand Up @@ -130,18 +121,13 @@ class __$$FreezedPersonImplCopyWithImpl<$Res>

class _$FreezedPersonImpl implements _FreezedPerson {
const _$FreezedPersonImpl(
{@HiveField(0) required this.firstName,
@HiveField(1) required this.lastName,
@HiveField(2) required this.age});
{required this.firstName, required this.lastName, required this.age});

@override
@HiveField(0)
final String firstName;
@override
@HiveField(1)
final String lastName;
@override
@HiveField(2)
final int age;

@override
Expand Down Expand Up @@ -175,18 +161,15 @@ class _$FreezedPersonImpl implements _FreezedPerson {

abstract class _FreezedPerson implements FreezedPerson {
const factory _FreezedPerson(
{@HiveField(0) required final String firstName,
@HiveField(1) required final String lastName,
@HiveField(2) required final int age}) = _$FreezedPersonImpl;
{required final String firstName,
required final String lastName,
required final int age}) = _$FreezedPersonImpl;

@override
@HiveField(0)
String get firstName;
@override
@HiveField(1)
String get lastName;
@override
@HiveField(2)
int get age;

/// Create a copy of FreezedPerson
Expand Down
13 changes: 13 additions & 0 deletions hive/example/lib/hive/hive_adapters.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:example/freezed.dart';
import 'package:example/main.dart';
import 'package:hive_ce/hive.dart';

part 'hive_adapters.g.dart';

@GenerateAdapters([
AdapterSpec<Person>(),
AdapterSpec<FreezedPerson>(),
])
// This is for code generation
// ignore: unused_element
void _() {}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions hive/example/lib/hive/hive_adapters.g.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Hive CE
# Manual modifications may be necessary for certain migrations
# Check in to version control
nextTypeId: 2
types:
Person:
typeId: 0
nextIndex: 3
fields:
name:
index: 0
age:
index: 1
friends:
index: 2
FreezedPerson:
typeId: 1
nextIndex: 3
fields:
firstName:
index: 0
lastName:
index: 1
age:
index: 2
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
// Check in to version control

import 'package:hive_ce/hive.dart';
import 'package:example/freezed.dart';
import 'package:example/main.dart';
import 'package:example/hive/hive_adapters.dart';

extension HiveRegistrar on HiveInterface {
void registerAdapters() {
Expand Down
10 changes: 1 addition & 9 deletions hive/example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
import 'dart:io';

import 'package:example/hive_registrar.g.dart';
import 'package:example/hive/hive_registrar.g.dart';
import 'package:hive_ce/hive.dart';

part 'main.g.dart';

@HiveType(typeId: 1)
class Person {
Person({required this.name, required this.age, required this.friends});

@HiveField(0)
String name;

@HiveField(1)
int age;

@HiveField(2)
List<String> friends;

@override
Expand Down
Loading
Loading