From 104a8e2a3f2d53f0ad3bf533690ec1dab05525bf Mon Sep 17 00:00:00 2001 From: Simon Leier Date: Sun, 29 Sep 2019 19:12:14 +0200 Subject: [PATCH] Fix bug with Uint8Lists --- .gitignore | 102 +++++++++++++++++++++++++++++++++++++ CHANGELOG.md | 3 ++ analysis_options.yaml | 81 ++++++++++++++++++++++++++++- lib/src/class_builder.dart | 11 +++- pubspec.yaml | 2 +- 5 files changed, 195 insertions(+), 4 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ff30832 --- /dev/null +++ b/.gitignore @@ -0,0 +1,102 @@ +*.hive +.test_coverage.dart +coverage_badge.svg + + +# Miscellaneous +*.class +*.lock +*.log +*.pyc +*.swp +.DS_Store +.atom/ +.buildlog/ +.history +.svn/ + +# IntelliJ related +*.iml +*.ipr +*.iws +.idea/ + +# Visual Studio Code related +.vscode/ + +# Flutter repo-specific +/bin/cache/ +/bin/mingit/ +/dev/benchmarks/mega_gallery/ +/dev/bots/.recipe_deps +/dev/bots/android_tools/ +/dev/docs/doc/ +/dev/docs/flutter.docs.zip +/dev/docs/lib/ +/dev/docs/pubspec.yaml +/packages/flutter/coverage/ +version + +# packages file containing multi-root paths +.packages.generated + +# Flutter/Dart/Pub related +**/doc/api/ +.dart_tool/ +.flutter-plugins +.packages +.pub-cache/ +.pub/ +build/ +flutter_*.png +linked_*.ds +unlinked.ds +unlinked_spec.ds + +# Android related +**/android/**/gradle-wrapper.jar +**/android/.gradle +**/android/captures/ +**/android/gradlew +**/android/gradlew.bat +**/android/local.properties +**/android/**/GeneratedPluginRegistrant.java +**/android/key.properties +*.jks + +# iOS/XCode related +**/ios/**/*.mode1v3 +**/ios/**/*.mode2v3 +**/ios/**/*.moved-aside +**/ios/**/*.pbxuser +**/ios/**/*.perspectivev3 +**/ios/**/*sync/ +**/ios/**/.sconsign.dblite +**/ios/**/.tags* +**/ios/**/.vagrant/ +**/ios/**/DerivedData/ +**/ios/**/Icon? +**/ios/**/Pods/ +**/ios/**/.symlinks/ +**/ios/**/profile +**/ios/**/xcuserdata +**/ios/.generated/ +**/ios/Flutter/App.framework +**/ios/Flutter/Flutter.framework +**/ios/Flutter/Generated.xcconfig +**/ios/Flutter/app.flx +**/ios/Flutter/app.zip +**/ios/Flutter/flutter_assets/ +**/ios/Flutter/flutter_export_environment.sh +**/ios/ServiceDefinitions.json +**/ios/Runner/GeneratedPluginRegistrant.* + +# Coverage +coverage/ + +# Exceptions to above rules. +!**/ios/**/default.mode1v3 +!**/ios/**/default.mode2v3 +!**/ios/**/default.pbxuser +!**/ios/**/default.perspectivev3 +!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index a305192..9f5903c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 0.5.2 +- Fix bug with Uint8Lists + ## 0.5.1 - Bump Hive version diff --git a/analysis_options.yaml b/analysis_options.yaml index 0caccc8..e9a15ec 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -1 +1,80 @@ -include: ../analysis_options.yaml \ No newline at end of file +include: package:pedantic/analysis_options.yaml +analyzer: + exclude: + - "**/*.g.dart" + strong-mode: + implicit-casts: false + errors: + include_file_not_found: ignore +linter: + rules: + #- public_member_api_docs + - always_declare_return_types + - annotate_overrides + - avoid_empty_else + - avoid_function_literals_in_foreach_calls + - avoid_init_to_null + - avoid_null_checks_in_equality_operators + - avoid_relative_lib_imports + - avoid_renaming_method_parameters + - avoid_return_types_on_setters + - avoid_returning_null + - avoid_types_as_parameter_names + - avoid_unused_constructor_parameters + - await_only_futures + - camel_case_types + - cancel_subscriptions + - comment_references + - constant_identifier_names + - control_flow_in_finally + - directives_ordering + - empty_catches + - empty_constructor_bodies + - empty_statements + - implementation_imports + - invariant_booleans + - iterable_contains_unrelated_type + - library_names + - library_prefixes + - list_remove_unrelated_type + - lines_longer_than_80_chars + - no_adjacent_strings_in_list + - no_duplicate_case_values + - non_constant_identifier_names + - null_closures + - omit_local_variable_types + - only_throw_errors + - overridden_fields + - package_api_docs + - package_names + - package_prefixed_library_names + - prefer_adjacent_string_concatenation + - prefer_collection_literals + - prefer_conditional_assignment + - prefer_const_constructors + - prefer_contains + - prefer_equal_for_default_values + - prefer_final_fields + - prefer_initializing_formals + - prefer_interpolation_to_compose_strings + - prefer_is_empty + - prefer_is_not_empty + - prefer_single_quotes + - prefer_typing_uninitialized_variables + - recursive_getters + - slash_for_doc_comments + - test_types_in_equals + - throw_in_finally + - type_init_formals + - unawaited_futures + - unnecessary_brace_in_string_interps + - unnecessary_const + - unnecessary_getters_setters + - unnecessary_lambdas + - unnecessary_new + - unnecessary_null_aware_assignments + - unnecessary_statements + - unnecessary_this + - unrelated_type_equality_checks + - use_rethrow_when_possible + - valid_regexps \ No newline at end of file diff --git a/lib/src/class_builder.dart b/lib/src/class_builder.dart index 1ad1795..f8d44d3 100644 --- a/lib/src/class_builder.dart +++ b/lib/src/class_builder.dart @@ -1,3 +1,5 @@ +import 'dart:typed_data'; + import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/dart/element/type.dart'; import 'package:hive_generator/src/builder.dart'; @@ -8,6 +10,7 @@ class ClassBuilder extends Builder { var mapChecker = const TypeChecker.fromRuntime(Map); var setChecker = const TypeChecker.fromRuntime(Set); var iterableChecker = const TypeChecker.fromRuntime(Iterable); + var uint8ListChecker = const TypeChecker.fromRuntime(Uint8List); ClassBuilder(ClassElement cls, Map fields) : super(cls, fields); @@ -65,7 +68,7 @@ class ClassBuilder extends Builder { } String _cast(DartType type, String variable) { - if (iterableChecker.isAssignableFromType(type)) { + if (iterableChecker.isAssignableFromType(type) && !isUint8List(type)) { return '($variable as List)${_castIterable(type)}'; } else if (mapChecker.isExactlyType(type)) { return '($variable as Map)${_castMap(type)}'; @@ -81,10 +84,14 @@ class ClassBuilder extends Builder { mapChecker.isExactlyType(type); } + bool isUint8List(DartType type) { + return uint8ListChecker.isExactlyType(type); + } + String _castIterable(DartType type) { var paramType = type as ParameterizedType; var arg = paramType.typeArguments[0]; - if (isMapOrIterable(arg)) { + if (isMapOrIterable(arg) && !isUint8List(arg)) { var cast = ''; if (listChecker.isExactlyType(type)) { cast = '?.toList()'; diff --git a/pubspec.yaml b/pubspec.yaml index c239b12..eb0eacd 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: hive_generator description: Extension for Hive. Automatically generates TypeAdapters to store any class. -version: 0.5.1 +version: 0.5.2 author: Simon Leier homepage: https://github.com/leisim/hive