From 2fe78ed0920219ba53f91d8b8bc686d981cb06ea Mon Sep 17 00:00:00 2001 From: Rexios Date: Fri, 11 Oct 2024 12:38:14 -0400 Subject: [PATCH 01/12] Support generating adapters with named imports --- .../example/lib/hive_registrar.g.dart | 3 ++ hive_generator/example/lib/named_import.dart | 6 +++ .../example/lib/named_import.g.dart | 34 +++++++++++++++ hive_generator/example/lib/types.dart | 23 ++++++++++ hive_generator/example/lib/types.g.dart | 43 +++++++++++++++++++ hive_generator/lib/src/class_builder.dart | 32 ++++++++++++-- 6 files changed, 137 insertions(+), 4 deletions(-) create mode 100644 hive_generator/example/lib/named_import.dart create mode 100644 hive_generator/example/lib/named_import.g.dart diff --git a/hive_generator/example/lib/hive_registrar.g.dart b/hive_generator/example/lib/hive_registrar.g.dart index 259ae86f..ab8e40b9 100644 --- a/hive_generator/example/lib/hive_registrar.g.dart +++ b/hive_generator/example/lib/hive_registrar.g.dart @@ -1,4 +1,5 @@ import 'package:hive_ce/hive.dart'; +import 'package:example/named_import.dart'; import 'package:example/types.dart'; extension HiveRegistrar on HiveInterface { @@ -9,6 +10,8 @@ extension HiveRegistrar on HiveInterface { registerAdapter(EmptyClassAdapter()); registerAdapter(Enum1Adapter()); registerAdapter(IterableClassAdapter()); + registerAdapter(NamedImportTypeAdapter()); + registerAdapter(NamedImportsAdapter()); registerAdapter(NullableTypesAdapter()); } } diff --git a/hive_generator/example/lib/named_import.dart b/hive_generator/example/lib/named_import.dart new file mode 100644 index 00000000..588bd7a9 --- /dev/null +++ b/hive_generator/example/lib/named_import.dart @@ -0,0 +1,6 @@ +import 'package:hive_ce/hive.dart'; + +part 'named_import.g.dart'; + +@HiveType(typeId: 100) +class NamedImportType {} diff --git a/hive_generator/example/lib/named_import.g.dart b/hive_generator/example/lib/named_import.g.dart new file mode 100644 index 00000000..2ced755f --- /dev/null +++ b/hive_generator/example/lib/named_import.g.dart @@ -0,0 +1,34 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +// ignore_for_file: unnecessary_const, require_trailing_commas, unnecessary_breaks, document_ignores + +part of 'named_import.dart'; + +// ************************************************************************** +// TypeAdapterGenerator +// ************************************************************************** + +class NamedImportTypeAdapter extends TypeAdapter { + @override + final int typeId = 100; + + @override + NamedImportType read(BinaryReader reader) { + return NamedImportType(); + } + + @override + void write(BinaryWriter writer, NamedImportType obj) { + writer.writeByte(0); + } + + @override + int get hashCode => typeId.hashCode; + + @override + bool operator ==(Object other) => + identical(this, other) || + other is NamedImportTypeAdapter && + runtimeType == other.runtimeType && + typeId == other.typeId; +} diff --git a/hive_generator/example/lib/types.dart b/hive_generator/example/lib/types.dart index 545cddde..11c454b0 100644 --- a/hive_generator/example/lib/types.dart +++ b/hive_generator/example/lib/types.dart @@ -1,4 +1,5 @@ import 'package:hive_ce/hive.dart'; +import 'package:example/named_import.dart' as named; part 'types.g.dart'; @@ -114,3 +115,25 @@ class NullableTypes { @HiveField(2) final bool? c; } + +@HiveType(typeId: 8) +class NamedImports { + NamedImports( + this.namedImportType, + this.namedImportTypeList, + this.namedImportTypeNullable, + this.namedImportTypeMap, + ); + + @HiveField(0) + final named.NamedImportType namedImportType; + + @HiveField(1) + final List namedImportTypeList; + + @HiveField(2) + final named.NamedImportType? namedImportTypeNullable; + + @HiveField(3) + final Map namedImportTypeMap; +} diff --git a/hive_generator/example/lib/types.g.dart b/hive_generator/example/lib/types.g.dart index d457d26a..224327a8 100644 --- a/hive_generator/example/lib/types.g.dart +++ b/hive_generator/example/lib/types.g.dart @@ -248,6 +248,49 @@ class NullableTypesAdapter extends TypeAdapter { typeId == other.typeId; } +class NamedImportsAdapter extends TypeAdapter { + @override + final int typeId = 8; + + @override + NamedImports read(BinaryReader reader) { + final numOfFields = reader.readByte(); + final fields = { + for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(), + }; + return NamedImports( + fields[0] as named.NamedImportType, + (fields[1] as List).cast(), + fields[2] as named.NamedImportType?, + (fields[3] as Map).cast(), + ); + } + + @override + void write(BinaryWriter writer, NamedImports obj) { + writer + ..writeByte(4) + ..writeByte(0) + ..write(obj.namedImportType) + ..writeByte(1) + ..write(obj.namedImportTypeList) + ..writeByte(2) + ..write(obj.namedImportTypeNullable) + ..writeByte(3) + ..write(obj.namedImportTypeMap); + } + + @override + int get hashCode => typeId.hashCode; + + @override + bool operator ==(Object other) => + identical(this, other) || + other is NamedImportsAdapter && + runtimeType == other.runtimeType && + typeId == other.typeId; +} + class Enum1Adapter extends TypeAdapter { @override final int typeId = 3; diff --git a/hive_generator/lib/src/class_builder.dart b/hive_generator/lib/src/class_builder.dart index 7c3d082c..03da22d0 100644 --- a/hive_generator/lib/src/class_builder.dart +++ b/hive_generator/lib/src/class_builder.dart @@ -1,5 +1,6 @@ import 'dart:typed_data'; +import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/dart/element/nullability_suffix.dart'; import 'package:analyzer/dart/element/type.dart'; import 'package:hive_ce/hive.dart'; @@ -123,7 +124,7 @@ class ClassBuilder extends Builder { } else if (type.isDartCoreDouble) { return '($variable as num$suffix)$suffix.toDouble()'; } else { - return '$variable as ${type.getDisplayString()}'; + return '$variable as ${type.getPrefixedDisplayString(cls.library)}'; } } @@ -154,7 +155,7 @@ class ClassBuilder extends Builder { return '$suffix.map((e) => ${_cast(arg, 'e')})$cast'; } else { - return '$suffix.cast<${arg.getDisplayString()}>()'; + return '$suffix.cast<${arg.getPrefixedDisplayString(cls.library)}>()'; } } @@ -167,8 +168,8 @@ class ClassBuilder extends Builder { return '$suffix.map((dynamic k, dynamic v)=>' 'MapEntry(${_cast(arg1, 'k')},${_cast(arg2, 'v')}))'; } else { - return '$suffix.cast<${arg1.getDisplayString()}, ' - '${arg2.getDisplayString()}>()'; + return '$suffix.cast<${arg1.getPrefixedDisplayString(cls.library)}, ' + '${arg2.getPrefixedDisplayString(cls.library)}>()'; } } @@ -219,3 +220,26 @@ String _suffixFromType(DartType type) { _ => '', }; } + +extension on DartType { + String getPrefixedDisplayString(LibraryElement currentLibrary) { + final element = this.element; + if (element == null) return getDisplayString(); + + final definingLibrary = element.library; + if (definingLibrary == currentLibrary) return getDisplayString(); + + for (final import in currentLibrary.libraryImports) { + if (import.importedLibrary == definingLibrary) { + final prefix = import.prefix; + if (prefix != null) { + return '${prefix.element.name}.${getDisplayString()}'; + } else { + return getDisplayString(); + } + } + } + + return getDisplayString(); + } +} From 326c43d8990d8f32f77ff9d03f9fa6ccd35792dc Mon Sep 17 00:00:00 2001 From: Rexios Date: Fri, 11 Oct 2024 12:44:20 -0400 Subject: [PATCH 02/12] Use Dart 3.4.0 in actions again --- .github/workflows/test.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 81db8b7e..d04c1207 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -48,6 +48,12 @@ jobs: - uses: actions/checkout@v4 - uses: browser-actions/setup-chrome@v1 - uses: dart-lang/setup-dart@v1 + with: + sdk: 3.4.0 + # rexios_lints requires a newer SDK + - name: Remove rexios_lints + run: | + sed -i "" "/rexios_lints: \.+/d" pubspec.yaml - name: Install dependencies run: dart pub get - name: Run tests From fca1aed53068cb0328e8ca58b18a84f72222e835 Mon Sep 17 00:00:00 2001 From: Rexios Date: Fri, 11 Oct 2024 12:45:23 -0400 Subject: [PATCH 03/12] Preparing for release --- hive_generator/CHANGELOG.md | 4 ++++ hive_generator/pubspec.yaml | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/hive_generator/CHANGELOG.md b/hive_generator/CHANGELOG.md index a67f27d3..9620fb06 100644 --- a/hive_generator/CHANGELOG.md +++ b/hive_generator/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.7.0 + +- Supports named imports + ## 1.6.0 - Adds `.freezed.dart` to `required_inputs` to support `freezed` diff --git a/hive_generator/pubspec.yaml b/hive_generator/pubspec.yaml index 16f744d5..f5ba7170 100644 --- a/hive_generator/pubspec.yaml +++ b/hive_generator/pubspec.yaml @@ -1,6 +1,6 @@ name: hive_ce_generator description: Extension for Hive. Automatically generates TypeAdapters to store any class. -version: 1.6.0 +version: 1.7.0 homepage: https://github.com/IO-Design-Team/hive_ce/tree/main/hive_generator documentation: https://docs.hivedb.dev/ From f9d448884a306638ec68b3d28dd8f8af882210aa Mon Sep 17 00:00:00 2001 From: Rexios Date: Fri, 11 Oct 2024 12:49:57 -0400 Subject: [PATCH 04/12] ... --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d04c1207..31480fc5 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -53,7 +53,7 @@ jobs: # rexios_lints requires a newer SDK - name: Remove rexios_lints run: | - sed -i "" "/rexios_lints: \.+/d" pubspec.yaml + sed -i "/rexios_lints: \.+/d" pubspec.yaml - name: Install dependencies run: dart pub get - name: Run tests From 73a56a302d51607deb02ad8c354261f2c0955e3d Mon Sep 17 00:00:00 2001 From: Rexios Date: Fri, 11 Oct 2024 12:53:37 -0400 Subject: [PATCH 05/12] Update sed command --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 31480fc5..76237e0e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -53,7 +53,7 @@ jobs: # rexios_lints requires a newer SDK - name: Remove rexios_lints run: | - sed -i "/rexios_lints: \.+/d" pubspec.yaml + sed -i "/rexios_lints: .*$/d" pubspec.yaml - name: Install dependencies run: dart pub get - name: Run tests From 2351509b3278d4ff0f8d6764c41531c6e8695939 Mon Sep 17 00:00:00 2001 From: Rexios Date: Fri, 11 Oct 2024 12:55:53 -0400 Subject: [PATCH 06/12] ... --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 76237e0e..83490149 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -53,7 +53,7 @@ jobs: # rexios_lints requires a newer SDK - name: Remove rexios_lints run: | - sed -i "/rexios_lints: .*$/d" pubspec.yaml + sed -i '/rexios_lints: \^.*$/d' pubspec.yaml - name: Install dependencies run: dart pub get - name: Run tests From 39249631e70171ca25b19f1347d4d16b9fb5e4df Mon Sep 17 00:00:00 2001 From: Rexios Date: Fri, 11 Oct 2024 12:57:33 -0400 Subject: [PATCH 07/12] Only setup chrome in chrome tests --- .github/workflows/test.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 83490149..2dbdec9f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -47,6 +47,7 @@ jobs: steps: - uses: actions/checkout@v4 - uses: browser-actions/setup-chrome@v1 + if: ${{ matrix.test-platform == 'chrome' }} - uses: dart-lang/setup-dart@v1 with: sdk: 3.4.0 From ab191e69e6bee02cb01a076b279f4a7e132ddec1 Mon Sep 17 00:00:00 2001 From: Rexios Date: Fri, 11 Oct 2024 13:00:10 -0400 Subject: [PATCH 08/12] ... --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 2dbdec9f..893e419b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -54,7 +54,7 @@ jobs: # rexios_lints requires a newer SDK - name: Remove rexios_lints run: | - sed -i '/rexios_lints: \^.*$/d' pubspec.yaml + sed -i '/^ rexios_lints: .*$/d' pubspec.yaml - name: Install dependencies run: dart pub get - name: Run tests From ccc34eeeac2c865c7b5fd30d25b8cf5a2e3b4ae8 Mon Sep 17 00:00:00 2001 From: Rexios Date: Fri, 11 Oct 2024 13:01:21 -0400 Subject: [PATCH 09/12] ... --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 893e419b..f9ccb4f8 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -54,7 +54,7 @@ jobs: # rexios_lints requires a newer SDK - name: Remove rexios_lints run: | - sed -i '/^ rexios_lints: .*$/d' pubspec.yaml + sed -i '/^[[:space:]]*rexios_lints: \^.*$/d' pubspec.yaml - name: Install dependencies run: dart pub get - name: Run tests From 4646ce9e34eea1d59af7ac2aaff4f3f9dde75b86 Mon Sep 17 00:00:00 2001 From: Rexios Date: Fri, 11 Oct 2024 13:02:29 -0400 Subject: [PATCH 10/12] ... --- .github/workflows/test.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f9ccb4f8..60e38afc 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -53,8 +53,7 @@ jobs: sdk: 3.4.0 # rexios_lints requires a newer SDK - name: Remove rexios_lints - run: | - sed -i '/^[[:space:]]*rexios_lints: \^.*$/d' pubspec.yaml + run: dart pub remove rexios_lints - name: Install dependencies run: dart pub get - name: Run tests From cab8e1d65397237d139291ad33eba3e3d46b8953 Mon Sep 17 00:00:00 2001 From: Rexios Date: Fri, 11 Oct 2024 13:04:59 -0400 Subject: [PATCH 11/12] ... --- .github/workflows/test.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 60e38afc..31845537 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -53,7 +53,8 @@ jobs: sdk: 3.4.0 # rexios_lints requires a newer SDK - name: Remove rexios_lints - run: dart pub remove rexios_lints + run: | + sed -i '/rexios_lints:/d' pubspec.yaml - name: Install dependencies run: dart pub get - name: Run tests From dd8c7f78f39946a3d938ceccce6404ce82703b37 Mon Sep 17 00:00:00 2001 From: Rexios Date: Fri, 11 Oct 2024 16:24:25 -0400 Subject: [PATCH 12/12] Better method of handling prefixes --- hive_generator/lib/src/class_builder.dart | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/hive_generator/lib/src/class_builder.dart b/hive_generator/lib/src/class_builder.dart index 03da22d0..a13255f3 100644 --- a/hive_generator/lib/src/class_builder.dart +++ b/hive_generator/lib/src/class_builder.dart @@ -230,12 +230,10 @@ extension on DartType { if (definingLibrary == currentLibrary) return getDisplayString(); for (final import in currentLibrary.libraryImports) { - if (import.importedLibrary == definingLibrary) { - final prefix = import.prefix; - if (prefix != null) { - return '${prefix.element.name}.${getDisplayString()}'; - } else { - return getDisplayString(); + for (final MapEntry(:key, :value) + in import.namespace.definedNames.entries) { + if (value == element) { + return '$key${_suffixFromType(this)}'; } } }