Skip to content

Commit

Permalink
Merge pull request #35 from IO-Design-Team/feature/generate-adapters
Browse files Browse the repository at this point in the history
Add generator support for `GenerateAdapters` annotation
  • Loading branch information
Rexios80 authored Nov 19, 2024
2 parents 6ca6e68 + d58565b commit cd182e3
Show file tree
Hide file tree
Showing 30 changed files with 1,751 additions and 226 deletions.
7 changes: 1 addition & 6 deletions hive/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,4 @@ dependency_overrides:
hive_ce:
path: ../
hive_ce_generator:
# TODO: Revert
# path: ../../hive_generator
git:
url: https://github.com/IO-Design-Team/hive_ce
ref: 4f06756e5eee0b027f2e9d623e8136620309afcb
path: hive_generator
path: ../../hive_generator
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.0

- Adds support for the `GenerateAdapters` annotation. See the [hive_ce documentation](https://pub.dev/packages/hive_ce) for more information.

## 1.7.0

- Supports named imports
Expand Down
14 changes: 14 additions & 0 deletions hive_generator/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,17 @@ builders:
build_extensions: { "$lib$": ["hive_registrar.g.dart"] }
auto_apply: dependents
build_to: source
hive_adapters_generator:
import: "package:hive_ce_generator/hive_generator.dart"
builder_factories: ["getAdaptersBuilder"]
build_extensions: { ".dart": ["hive_adapters_generator.g.part"] }
auto_apply: dependents
build_to: cache
applies_builders: ["source_gen|combining_builder"]
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
36 changes: 36 additions & 0 deletions hive_generator/example/lib/hive/hive_adapters.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'package:hive_ce/hive.dart';

part 'hive_adapters.g.dart';

@GenerateAdapters(
[
AdapterSpec<ClassSpec1>(),
AdapterSpec<ClassSpec2>(),
AdapterSpec<EnumSpec>(),
],
firstTypeId: 50,
)
// This is for code generation
// ignore: unused_element
void _() {}

class ClassSpec1 {
final int value;
final int value2;

ClassSpec1(this.value, this.value2);
}

class ClassSpec2 {
final String value;
final String value2;

ClassSpec2(this.value, this.value2);
}

enum EnumSpec {
value1,
value2;

EnumSpec get getter => EnumSpec.value2;
}
120 changes: 120 additions & 0 deletions hive_generator/example/lib/hive/hive_adapters.g.dart

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

29 changes: 29 additions & 0 deletions hive_generator/example/lib/hive/hive_adapters.g.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Hive CE
# Manual modifications may be necessary for certain migrations
# Check in to version control
nextTypeId: 53
types:
ClassSpec1:
typeId: 50
nextIndex: 2
fields:
value:
index: 0
value2:
index: 1
ClassSpec2:
typeId: 51
nextIndex: 2
fields:
value:
index: 0
value2:
index: 1
EnumSpec:
typeId: 52
nextIndex: 2
fields:
value1:
index: 0
value2:
index: 1
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@
// ignore_for_file: unnecessary_const, require_trailing_commas, document_ignores

import 'package:hive_ce/hive.dart';
import 'package:example/named_import.dart';
import 'package:example/hive/hive_adapters.dart';
import 'package:example/types.dart';

extension HiveRegistrar on HiveInterface {
void registerAdapters() {
registerAdapter(Class1Adapter());
registerAdapter(Class2Adapter());
registerAdapter(ClassSpec1Adapter());
registerAdapter(ClassSpec2Adapter());
registerAdapter(ConstructorDefaultsAdapter());
registerAdapter(EmptyClassAdapter());
registerAdapter(Enum1Adapter());
registerAdapter(EnumSpecAdapter());
registerAdapter(IterableClassAdapter());
registerAdapter(NamedImportTypeAdapter());
registerAdapter(NamedImportsAdapter());
registerAdapter(NullableTypesAdapter());
}
Expand Down
5 changes: 0 additions & 5 deletions hive_generator/example/lib/named_import.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
import 'package:hive_ce/hive.dart';

part 'named_import.g.dart';

@HiveType(typeId: 100)
class NamedImportType {}
34 changes: 0 additions & 34 deletions hive_generator/example/lib/named_import.g.dart

This file was deleted.

16 changes: 13 additions & 3 deletions hive_generator/lib/hive_generator.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'package:build/build.dart';
import 'package:hive_ce_generator/src/registrar_builder.dart';
import 'package:hive_ce_generator/src/registrar_intermediate_builder.dart';
import 'package:hive_ce_generator/src/type_adapter_generator.dart';
import 'package:hive_ce_generator/src/builder/schema_migrator_builder.dart';
import 'package:hive_ce_generator/src/generator/adapters_generator.dart';
import 'package:hive_ce_generator/src/builder/registrar_builder.dart';
import 'package:hive_ce_generator/src/builder/registrar_intermediate_builder.dart';
import 'package:hive_ce_generator/src/generator/type_adapter_generator.dart';
import 'package:source_gen/source_gen.dart';

/// Builds Hive TypeAdapters
Expand All @@ -14,3 +16,11 @@ Builder getRegistrarIntermediateBuilder(BuilderOptions options) =>

/// Builds the HiveRegistrar extension
Builder getRegistrarBuilder(BuilderOptions options) => RegistrarBuilder();

/// Builds Hive TypeAdapters from the GenerateAdapters annotation
Builder getAdaptersBuilder(BuilderOptions options) =>
SharedPartBuilder([AdaptersGenerator()], 'hive_adapters_generator');

/// Builds a Hive schema from existing HiveType annotations
Builder getSchemaMigratorBuilder(BuilderOptions options) =>
SchemaMigratorBuilder();
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import 'package:analyzer/dart/element/type.dart';

/// Metadata about a field in a class adapter
class AdapterField {
/// The corresponding element for this field
final PropertyAccessorElement element;

/// The index of the field
///
/// Determines the order fields are read and written
Expand All @@ -23,6 +26,7 @@ class AdapterField {

/// Constructor
AdapterField(
this.element,
this.index,
this.name,
this.type,
Expand All @@ -32,7 +36,7 @@ class AdapterField {
}

/// TODO: Document this!
abstract class Builder {
abstract class AdapterBuilder {
/// TODO: Document this!
final InterfaceElement cls;

Expand All @@ -43,7 +47,11 @@ abstract class Builder {
final List<AdapterField> setters;

/// TODO: Document this!
Builder(this.cls, this.getters, [this.setters = const <AdapterField>[]]);
AdapterBuilder(
this.cls,
this.getters, [
this.setters = const <AdapterField>[],
]);

/// TODO: Document this!
String buildRead();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import 'package:analyzer/dart/element/nullability_suffix.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:collection/collection.dart';
import 'package:hive_ce/hive.dart';
import 'package:hive_ce_generator/src/builder.dart';
import 'package:hive_ce_generator/src/adapter_builder/adapter_builder.dart';
import 'package:hive_ce_generator/src/helper/helper.dart';
import 'package:source_gen/source_gen.dart';

import 'package:hive_ce_generator/src/helper/type_helper.dart';

/// TODO: Document this!
class ClassBuilder extends Builder {
class ClassAdapterBuilder extends AdapterBuilder {
/// TODO: Document this!
ClassBuilder(
ClassAdapterBuilder(
super.cls,
super.getters,
super.setters,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'package:hive_ce_generator/src/builder.dart';
import 'package:hive_ce_generator/src/adapter_builder/adapter_builder.dart';

/// TODO: Document this!
class EnumBuilder extends Builder {
class EnumAdapterBuilder extends AdapterBuilder {
/// TODO: Document this!
EnumBuilder(super.cls, super.getters);
EnumAdapterBuilder(super.cls, super.getters);

@override
String buildRead() {
Expand Down
Loading

0 comments on commit cd182e3

Please sign in to comment.