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

Support named imports #33

Merged
merged 13 commits into from
Oct 11, 2024
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.7.0

- Supports named imports

## 1.6.0

- Adds `.freezed.dart` to `required_inputs` to support `freezed`
Expand Down
3 changes: 3 additions & 0 deletions hive_generator/example/lib/hive_registrar.g.dart
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -9,6 +10,8 @@ extension HiveRegistrar on HiveInterface {
registerAdapter(EmptyClassAdapter());
registerAdapter(Enum1Adapter());
registerAdapter(IterableClassAdapter());
registerAdapter(NamedImportTypeAdapter());
registerAdapter(NamedImportsAdapter());
registerAdapter(NullableTypesAdapter());
}
}
6 changes: 6 additions & 0 deletions hive_generator/example/lib/named_import.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import 'package:hive_ce/hive.dart';

part 'named_import.g.dart';

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

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

23 changes: 23 additions & 0 deletions hive_generator/example/lib/types.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:hive_ce/hive.dart';
import 'package:example/named_import.dart' as named;

part 'types.g.dart';

Expand Down Expand Up @@ -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<named.NamedImportType> namedImportTypeList;

@HiveField(2)
final named.NamedImportType? namedImportTypeNullable;

@HiveField(3)
final Map<named.NamedImportType, named.NamedImportType> namedImportTypeMap;
}
43 changes: 43 additions & 0 deletions hive_generator/example/lib/types.g.dart

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

30 changes: 26 additions & 4 deletions hive_generator/lib/src/class_builder.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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)}';
}
}

Expand Down Expand Up @@ -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)}>()';
}
}

Expand All @@ -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)}>()';
}
}

Expand Down Expand Up @@ -219,3 +220,24 @@ 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) {
for (final MapEntry(:key, :value)
in import.namespace.definedNames.entries) {
if (value == element) {
return '$key${_suffixFromType(this)}';
}
}
}

return getDisplayString();
}
}
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.6.0
version: 1.7.0
homepage: https://github.com/IO-Design-Team/hive_ce/tree/main/hive_generator
documentation: https://docs.hivedb.dev/

Expand Down