Skip to content

Commit

Permalink
Migrate hive_generator dependencies to null-safety
Browse files Browse the repository at this point in the history
  • Loading branch information
themisir committed Mar 25, 2021
1 parent 266fb5e commit 19d0976
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 23 deletions.
6 changes: 6 additions & 0 deletions hive/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 2.0.1

### Fixes

- `BoxEvent` value is `null` when watching a `LazyBox` - [#592](https://github.com/hivedb/hive/pull/592)

# 2.0.0

### Fixes
Expand Down
2 changes: 1 addition & 1 deletion hive_generator/lib/src/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ abstract class Builder {
final List<AdapterField> getters;
final List<AdapterField> setters;

Builder(this.cls, this.getters, this.setters) : assert(cls != null);
Builder(this.cls, this.getters, [this.setters = const <AdapterField>[]]);

String buildRead();

Expand Down
25 changes: 11 additions & 14 deletions hive_generator/lib/src/class_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ClassBuilder extends Builder {
// The remaining fields to initialize.
var fields = setters.toList();

for (var param in constr.parameters) {
for (var param in constr!.parameters) {
var field = fields.firstOrNullWhere((it) => it.name == param.name);
// Final fields
field ??= getters.firstOrNullWhere((it) => it.name == param.name);
Expand Down Expand Up @@ -157,8 +157,14 @@ class ClassBuilder extends Builder {
}

extension _FirstOrNullWhere<T> on Iterable<T> {
T /*?*/ firstOrNullWhere(bool Function(T) predicate) =>
firstWhere(predicate, orElse: () => null);
T? firstOrNullWhere(bool Function(T) predicate) {
for (var it in this) {
if (predicate(it)) {
return it;
}
}
return null;
}
}

/// Suffix to use when accessing a field in [type].
Expand Down Expand Up @@ -186,15 +192,6 @@ String _suffixFromType(DartType type) {
}

String _displayString(DartType e) {
try {
var suffix = _suffixFromType(e);
return '${e.getDisplayString(withNullability: false) as String}$suffix';
} catch (error) {
if (error is TypeError) {
var suffix = _suffixFromType(e);
return '${e.getDisplayString() as String}$suffix';
} else {
rethrow;
}
}
var suffix = _suffixFromType(e);
return '${e.getDisplayString(withNullability: false)}$suffix';
}
2 changes: 1 addition & 1 deletion hive_generator/lib/src/enum_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:hive_generator/src/helper.dart';

class EnumBuilder extends Builder {
EnumBuilder(ClassElement cls, List<AdapterField> getters)
: super(cls, getters, null);
: super(cls, getters);

@override
String buildRead() {
Expand Down
9 changes: 5 additions & 4 deletions hive_generator/lib/src/helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import 'package:source_gen/source_gen.dart';

final _hiveFieldChecker = const TypeChecker.fromRuntime(HiveField);

HiveField getHiveFieldAnn(Element element) {
HiveField? getHiveFieldAnn(Element element) {
var obj = _hiveFieldChecker.firstAnnotationOfExact(element);
if (obj == null) return null;
return HiveField(
obj.getField('index').toIntValue(),
obj.getField('index')!.toIntValue()!,
);
}

bool isLibraryNNBD(Element element) {
final dartVersion = element.library.languageVersion.effective;
final dartVersion = element.library!.languageVersion.effective;
// Libraries with the dart version >= 2.12 are nnbd
if (dartVersion.major >= 2 && dartVersion.minor >= 12) {
return true;
Expand All @@ -29,8 +29,9 @@ Iterable<ClassElement> getTypeAndAllSupertypes(ClassElement cls) {
return types;
}

void check(bool condition, dynamic error) {
void check(bool condition, Object error) {
if (!condition) {
// ignore: only_throw_errors
throw error;
}
}
6 changes: 3 additions & 3 deletions hive_generator/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ homepage: https://github.com/hivedb/hive/tree/master/hive_generator
documentation: https://docs.hivedb.dev/

environment:
sdk: '>=2.10.0 <3.0.0'
sdk: ">=2.12.0 <3.0.0"

dependencies:
build: ^1.5.2
source_gen: ">=0.9.10+1 <2.0.0"
build: ^2.0.0
source_gen: ^1.0.0
hive: ^2.0.0
analyzer: ">=0.40.0 <2.0.0"

Expand Down

0 comments on commit 19d0976

Please sign in to comment.