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

Fix migration for freezed classes #54

Merged
merged 9 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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_generator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.8.1

- Fixes a bug in the migrator affecting `freezed` classes

## 1.8.0

- Adds support for the `GenerateAdapters` annotation. See the [hive_ce documentation](https://pub.dev/packages/hive_ce) for more information.
Expand Down
4 changes: 4 additions & 0 deletions hive_generator/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ builders:
auto_apply: dependents
build_to: cache
applies_builders: ["source_gen|combining_builder"]
required_inputs:
- .freezed.dart
hive_schema_migrator:
import: "package:hive_ce_generator/hive_generator.dart"
builder_factories: ["getSchemaMigratorBuilder"]
build_extensions:
{ "$lib$": ["hive/hive_adapters.yaml", "hive/hive_adapters.g.yaml"] }
auto_apply: none
build_to: source
required_inputs:
- .freezed.dart
5 changes: 4 additions & 1 deletion hive_generator/lib/src/builder/schema_migrator_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ class SchemaMigratorBuilder implements Builder {
final uri = library.source.uri;
final isEnum = cls.thisType.isEnum;
final constructor = getConstructor(cls);
final accessors = cls.accessors;
final accessors = [
...cls.accessors,
...cls.allSupertypes.expand((it) => it.accessors),
];
final info = _SchemaInfo(
uri: uri,
className: className,
Expand Down
2 changes: 1 addition & 1 deletion hive_generator/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: hive_ce_generator
description: Extension for Hive. Automatically generates TypeAdapters to store any class.
version: 1.8.0
version: 1.8.1
homepage: https://github.com/IO-Design-Team/hive_ce/tree/main/hive_generator
documentation: https://docs.hivedb.dev/

Expand Down
18 changes: 9 additions & 9 deletions hive_generator/test/adapters_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void main() {
test('fresh', () {
expectGeneration(
input: {
...pubspec,
...pubspec(),
'lib/hive/hive_adapters.dart': '''
$directives

Expand All @@ -49,7 +49,7 @@ class Person {
// the order in the annotation
expectGeneration(
input: {
...pubspec,
...pubspec(),
'lib/hive/hive_adapters.dart': '''
$directives

Expand Down Expand Up @@ -101,7 +101,7 @@ types:
// typeId of 1
expectGeneration(
input: {
...pubspec,
...pubspec(),
'lib/hive/hive_adapters.dart': '''
$directives

Expand Down Expand Up @@ -137,7 +137,7 @@ types:
// A new field on Person should have the last index no matter the order
expectGeneration(
input: {
...pubspec,
...pubspec(),
'lib/hive/hive_adapters.dart': '''
$directives

Expand Down Expand Up @@ -175,7 +175,7 @@ types:
test('add and remove field', () {
expectGeneration(
input: {
...pubspec,
...pubspec(),
'lib/hive/hive_adapters.dart': '''
$directives

Expand Down Expand Up @@ -211,7 +211,7 @@ types:
test('with invalid next type ID', () {
expectGeneration(
input: {
...pubspec,
...pubspec(),
'lib/hive/hive_adapters.dart': '''
$directives

Expand All @@ -234,7 +234,7 @@ types:
test('with invalid next field index', () {
expectGeneration(
input: {
...pubspec,
...pubspec(),
'lib/hive/hive_adapters.dart': '''
$directives

Expand All @@ -259,7 +259,7 @@ types:
test('with duplicate type ID', () {
expectGeneration(
input: {
...pubspec,
...pubspec(),
'lib/hive/hive_adapters.dart': '''
$directives

Expand All @@ -286,7 +286,7 @@ types:
test('with duplicate field index', () {
expectGeneration(
input: {
...pubspec,
...pubspec(),
'lib/hive/hive_adapters.dart': '''
$directives

Expand Down
12 changes: 6 additions & 6 deletions hive_generator/test/registrar_builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ void main() {
test('outputs to lib folder by default', () {
expectGeneration(
input: {
...pubspec,
...pubspec(),
'lib/nested/type.dart': '''
import 'package:hive_ce/hive.dart';
part 'type.g.dart';
Expand All @@ -25,7 +25,7 @@ class Type {}
test('does not output with no types', () {
expectGeneration(
input: {
...pubspec,
...pubspec(),
'lib/hive_adapters.dart': '''
import 'package:hive_ce/hive.dart';
part 'hive_adapters.g.dart';
Expand All @@ -43,7 +43,7 @@ void _() {}
test('outputs next to GenerateAdapters annotation', () {
expectGeneration(
input: {
...pubspec,
...pubspec(),
'lib/hive/hive_adapters.dart': '''
import 'package:hive_ce/hive.dart';
part 'hive_adapters.g.dart';
Expand All @@ -61,7 +61,7 @@ class Type {}
test('fails with multiple GenerateAdapters in same file', () {
expectGeneration(
input: {
...pubspec,
...pubspec(),
'lib/hive_adapters.dart': '''
import 'package:hive_ce/hive.dart';
part 'hive_adapters.g.dart';
Expand All @@ -81,7 +81,7 @@ class Type2 {}
test('fails with multiple GenerateAdapters on same element', () {
expectGeneration(
input: {
...pubspec,
...pubspec(),
'lib/hive_adapters.dart': '''
import 'package:hive_ce/hive.dart';
part 'hive_adapters.g.dart';
Expand All @@ -101,7 +101,7 @@ class Type2 {}
test('fails with multiple GenerateAdapters files', () {
expectGeneration(
input: {
...pubspec,
...pubspec(),
'lib/hive_adapters.dart': '''
import 'package:hive_ce/hive.dart';
part 'hive_adapters.g.dart';
Expand Down
63 changes: 57 additions & 6 deletions hive_generator/test/schema_migrator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void main() {
test('does not run if not enabled', () {
expectGeneration(
input: {
...pubspec,
...pubspec(),
...adapters,
},
output: {
Expand All @@ -75,7 +75,7 @@ void main() {
test('generates schema', () {
expectGeneration(
input: {
...pubspec,
...pubspec(),
...buildYaml,
...adapters,
},
Expand Down Expand Up @@ -137,7 +137,7 @@ types:
test('throws with default value', () {
expectGeneration(
input: {
...pubspec,
...pubspec(),
...buildYaml,
'lib/adapters.dart': '''
import 'package:hive_ce/hive.dart';
Expand All @@ -159,7 +159,7 @@ class Class {
test('throws with no public setter', () {
expectGeneration(
input: {
...pubspec,
...pubspec(),
...buildYaml,
'lib/adapters.dart': '''
import 'package:hive_ce/hive.dart';
Expand All @@ -183,7 +183,7 @@ class Class {
test('throws with no public getter', () {
expectGeneration(
input: {
...pubspec,
...pubspec(),
...buildYaml,
'lib/adapters.dart': '''
import 'package:hive_ce/hive.dart';
Expand All @@ -207,7 +207,7 @@ class Class {
test('throws with schema mismatch', () {
expectGeneration(
input: {
...pubspec,
...pubspec(),
...buildYaml,
'lib/adapters.dart': '''
import 'package:hive_ce/hive.dart';
Expand All @@ -227,5 +227,56 @@ class Class {
),
);
});

test('works with freezed classes', () {
expectGeneration(
input: {
...pubspec(
dependencies: {'freezed_annotation: any'},
devDependencies: {'freezed: any'},
),
...buildYaml,
'lib/adapters.dart': '''
import 'package:hive_ce/hive.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

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

@HiveType(typeId: 0)
@freezed
class Class with _\$Class {
factory Class({@HiveField(0) required int value}) = _Class;
}
''',
},
output: {
'lib/hive/hive_adapters.dart': '''
import 'package:hive_ce/hive.dart';
import 'package:hive_ce_generator_test/adapters.dart';

part 'hive_adapters.g.dart';

@GenerateAdapters([
AdapterSpec<Class>(),
])
// This is for code generation
// ignore: unused_element
void _() {}
''',
'lib/hive/hive_adapters.g.yaml': '''
$schemaComment
nextTypeId: 1
types:
Class:
typeId: 0
nextIndex: 1
fields:
value:
index: 0
''',
},
);
});
});
}
7 changes: 6 additions & 1 deletion hive_generator/test/test_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ String createTestProject(Map<String, String> project) {
return directory.path;
}

Map<String, String> get pubspec {
Map<String, String> pubspec({
Set<String> dependencies = const {},
Set<String> devDependencies = const {},
}) {
final hivePath = path.absolute(path.current, '..', 'hive');
final hiveGeneratorPath = path.absolute(path.current);

Expand All @@ -74,10 +77,12 @@ environment:

dependencies:
hive_ce: any
${dependencies.join('\n ')}

dev_dependencies:
build_runner: any
hive_ce_generator: any
${devDependencies.join('\n ')}

dependency_overrides:
hive_ce:
Expand Down